Skip to content

Commit

Permalink
Merge pull request #1 from mattia-sanfilippo/feat-old-architecture-su…
Browse files Browse the repository at this point in the history
…pport

feat: old architecture support
  • Loading branch information
mattia-sanfilippo authored Jan 14, 2025
2 parents c22628e + 1456f72 commit 6902fc7
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 30 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ pod install

See the [Apple Pay documentation](https://developer.apple.com/documentation/passkit/setting-up-apple-pay) for more information.


## Usage

### Apple Pay
Expand Down Expand Up @@ -84,7 +83,7 @@ const handleApplePay = async () => {
// Do any additional processing before confirming the payment

// Confirm the payment to complete the transaction and show a success message on the Payment Sheet
confirmPayment(paymentResponse.paymentData);
confirmPayment();
} catch (error) {
// Handle errors by showing an error on the Payment Sheet and rejecting the payment
rejectPayment();
Expand Down
7 changes: 6 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ android {
if (isNewArchitectureEnabled()) {
java.srcDirs += [
"generated/java",
"generated/jni"
"generated/jni",
"src/newarch"
]
} else {
java.srcDirs += [
"src/oldarch"
]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.walletpayments

import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.module.annotations.ReactModule
Expand All @@ -14,26 +15,32 @@ class WalletPaymentsModule(reactContext: ReactApplicationContext) :
return NAME
}

@ReactMethod
override fun canMakePayments(promise: Promise) {
promise.resolve(false)
}

@ReactMethod
override fun showPaymentSheet(data: ReadableMap?, promise: Promise) {
promise.reject("E_NOT_IMPLEMENTED", "Apple Pay is not available on Android")
}

@ReactMethod
override fun updateShippingMethods(shippingMethods: ReadableArray?) {
// No-op
}

@ReactMethod
override fun updateSummaryItems(summaryItems: ReadableArray?) {
// No-op
}

@ReactMethod
override fun confirmPayment() {
// No-op
}

@ReactMethod
override fun rejectPayment() {
// No-op
}
Expand Down
5 changes: 5 additions & 0 deletions android/src/newarch/WalletPaymentsSpec.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.walletpayments

import com.facebook.react.bridge.ReactApplicationContext

abstract class WalletPaymentsSpec(context: ReactApplicationContext) : NativeWalletPaymentsSpec(context)
16 changes: 16 additions & 0 deletions android/src/oldarch/WalletPaymentsSpec.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.walletpayments

import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.module.ReactContextBaseJavaModule

abstract class WalletPaymentsSpec(context: ReactApplicationContext) : ReactContextBaseJavaModule(context) {
abstract fun canMakePayments(promise: Promise)
abstract fun showPaymentSheet(data: ReadableMap?, promise: Promise)
abstract fun updateShippingMethods(shippingMethods: ReadableArray?)
abstract fun updateSummaryItems(summaryItems: ReadableArray?)
abstract fun confirmPayment()
abstract fun rejectPayment()
}
2 changes: 1 addition & 1 deletion example/ios/Podfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ENV['RCT_NEW_ARCH_ENABLED'] = '1'
# ENV['RCT_NEW_ARCH_ENABLED'] = '1'

# Resolve react_native_pods.rb with node to allow for hoisting
require Pod::Executable.execute_command('node', ['-p',
Expand Down
6 changes: 3 additions & 3 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,7 @@ PODS:
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- Yoga
- react-native-wallet-payments (0.1.0):
- react-native-wallet-payments (0.2.1):
- DoubleConversion
- glog
- hermes-engine
Expand Down Expand Up @@ -1772,7 +1772,7 @@ SPEC CHECKSUMS:
React-logger: d42a53754a7252cc7a851315f0da2e46b450ea92
React-Mapbuffer: 89885d1518433a462fe64b68bf5e097997380090
React-microtasksnativemodule: 36341e09dcd1df535503e6ed2ddf88f10da56d52
react-native-wallet-payments: f0c42ee3e48f0ad56003baebb0278448888d9a36
react-native-wallet-payments: 8ef94e3f05e41f093eec23edc6b35e2783617f02
React-nativeconfig: 539ff4de6ce3b694e8e751080568c281c84903ce
React-NativeModulesApple: 702246817c286d057e23fe4b1302019796e62521
React-perflogger: f260e2de95f9dbd002035251559c13bf1f0643d4
Expand Down Expand Up @@ -1803,6 +1803,6 @@ SPEC CHECKSUMS:
SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748
Yoga: be6f55a028e86c83ae066f018e9b5d24ffc45436

PODFILE CHECKSUM: 8d87a6f4d9136cc04c7824018094441005ab77e5
PODFILE CHECKSUM: 1ae2ee2d1d107c5c5e73d9503648ff2e760dcb54

COCOAPODS: 1.15.2
13 changes: 12 additions & 1 deletion ios/WalletPayments.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@

#import <PassKit/PassKit.h>

#ifdef RCT_NEW_ARCH_ENABLED
#import "generated/RNWalletPaymentsSpec/RNWalletPaymentsSpec.h"

// New Architecture Interface
@interface WalletPayments : NSObject <NativeWalletPaymentsSpec, PKPaymentAuthorizationControllerDelegate>

#else

#import <React/RCTBridgeModule.h>

// Old Architecture Interface
@interface WalletPayments : NSObject <RCTBridgeModule, PKPaymentAuthorizationControllerDelegate>

#endif

@property (nonatomic, copy) RCTPromiseResolveBlock _Nullable resolveBlock;
@property (nonatomic, copy) RCTPromiseRejectBlock _Nullable rejectBlock;

Expand Down
29 changes: 15 additions & 14 deletions ios/WalletPayments.mm
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,15 @@ @implementation WalletPayments

RCT_EXPORT_MODULE()

- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params
{
return std::make_shared<facebook::react::NativeWalletPaymentsSpecJSI>(params);
}


- (void)canMakePayments:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject
RCT_EXPORT_METHOD(canMakePayments:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)
{
resolve(@([PKPaymentAuthorizationController canMakePayments]));
}

- (void)showPaymentSheet:(NSDictionary *)data
RCT_EXPORT_METHOD(showPaymentSheet:(NSDictionary *)data
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject
reject:(RCTPromiseRejectBlock)reject)
{


Expand Down Expand Up @@ -148,7 +141,7 @@ - (void)showPaymentSheet:(NSDictionary *)data
}];
}

- (void)updateShippingMethods:(NSArray *)shippingMethods
RCT_EXPORT_METHOD(updateShippingMethods:(NSArray *)shippingMethods)
{
NSMutableArray<PKShippingMethod *> *updatedMethods = [NSMutableArray new];

Expand All @@ -172,7 +165,7 @@ - (void)updateShippingMethods:(NSArray *)shippingMethods
}
}

- (void)updateSummaryItems:(NSArray *)summaryItems
RCT_EXPORT_METHOD(updateSummaryItems:(NSArray *)summaryItems)
{
NSMutableArray<PKPaymentSummaryItem *> *updatedItems = [NSMutableArray new];

Expand All @@ -191,7 +184,7 @@ - (void)updateSummaryItems:(NSArray *)summaryItems
}
}

- (void)confirmPayment
RCT_EXPORT_METHOD(confirmPayment)
{

NSLog(@"Confirming payment");
Expand Down Expand Up @@ -472,4 +465,12 @@ - (PKMerchantCapability)mapMerchantCapabilities:(NSArray<NSString *> *)capabilit
return merchantCapabilities;
}

#ifdef RCT_NEW_ARCH_ENABLED
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params
{
return std::make_shared<facebook::react::NativeWalletPaymentsSpecJSI>(params);
}
#endif

@end
2 changes: 1 addition & 1 deletion src/NativeWalletPayments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ export interface Spec extends TurboModule {
rejectPayment(): void;
}

export default TurboModuleRegistry.getEnforcing<Spec>('WalletPayments');
export default TurboModuleRegistry.get<Spec>('WalletPayments');
21 changes: 14 additions & 7 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import WalletPayments from './NativeWalletPayments';
import NativeWalletPayments from './NativeWalletPayments';

export { useApplePay } from './useApplePay';
export { default as ApplePayButton } from './ApplePayButton';
Expand Down Expand Up @@ -125,13 +125,20 @@ export type PaymentResult = {
shippingMethod?: ShippingMethod;
};

// @ts-expect-error This applies the turbo module version only when turbo is enabled for backwards compatibility.
const isTurboModuleEnabled = global?.__turboModuleProxy != null;

const WalletPaymentsModule = isTurboModuleEnabled
? require('./NativeWalletPayments').default
: NativeWalletPayments;

/**
* canMakePayments
* This function checks if the device supports Apple Pay and if the user has any cards added.
* @returns {Promise<boolean>} - A promise that resolves with a boolean value.
*/
export function canMakePayments(): Promise<boolean> {
return WalletPayments.canMakePayments();
return WalletPaymentsModule.canMakePayments();
}

/**
Expand All @@ -141,7 +148,7 @@ export function canMakePayments(): Promise<boolean> {
* @returns {Promise<PaymentResult>} - A promise that resolves with the payment result.
*/
export function showPaymentSheet(data: PaymentRequest): Promise<string> {
return WalletPayments.showPaymentSheet(data);
return WalletPaymentsModule.showPaymentSheet(data);
}

/**
Expand All @@ -153,7 +160,7 @@ export function showPaymentSheet(data: PaymentRequest): Promise<string> {
export function updateShippingMethods(
shippingMethods: Array<ShippingMethod>
): void {
WalletPayments.updateShippingMethods(shippingMethods);
WalletPaymentsModule.updateShippingMethods(shippingMethods);
}

/**
Expand All @@ -163,7 +170,7 @@ export function updateShippingMethods(
* from the event listener.
*/
export function updateSummaryItems(summaryItems: Array<SummaryItem>): void {
WalletPayments.updateSummaryItems(summaryItems);
WalletPaymentsModule.updateSummaryItems(summaryItems);
}

/**
Expand All @@ -172,13 +179,13 @@ export function updateSummaryItems(summaryItems: Array<SummaryItem>): void {
* if the function is not called, the Apple Pay sheet will remain open and it will go into a timeout state (handled by Apple's internal logic).
*/
export function confirmPayment(): void {
WalletPayments.confirmPayment();
WalletPaymentsModule.confirmPayment();
}

/**
* rejectPayment
* This function rejects the payment.
*/
export function rejectPayment(): void {
WalletPayments.rejectPayment();
WalletPaymentsModule.rejectPayment();
}

0 comments on commit 6902fc7

Please sign in to comment.