Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Android implementation, remove Android gateway validation, refactor canMakePayments #174

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions packages/react-native-payments/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
compileSdkVersion 28
buildToolsVersion "28.0.3"

defaultConfig {
minSdkVersion 16
targetSdkVersion 22
targetSdkVersion 28
versionCode 1
versionName "1.0"
ndk {
Expand All @@ -19,7 +19,7 @@ android {
}

dependencies {
compile 'com.facebook.react:react-native:+'
compile 'com.google.android.gms:play-services-wallet:11.0.4'
compile 'com.android.support:support-v4:23.0.1'
}
implementation 'com.facebook.react:react-native:+'
implementation 'com.google.android.gms:play-services-wallet:17.0.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.reactnativepayments">

<application android:label="@string/app_name">
<meta-data
android:name="com.google.android.gms.wallet.api.enabled"
android:value="true" />
</application>
</manifest>

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">react-native-payments</string>
</resources>
37 changes: 6 additions & 31 deletions packages/react-native-payments/lib/js/NativePayments.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
// @flow

import type { PaymentDetailsBase, PaymentComplete } from './types';
import type { CanMakePayments, PaymentDetailsBase, PaymentComplete } from './types';

import { NativeModules, Platform } from 'react-native';
const { ReactNativePayments } = NativeModules;

const IS_ANDROID = Platform.OS === 'android';

const NativePayments: {
canMakePayments: boolean,
canMakePayments: CanMakePayments => Promise<boolean>,
supportedGateways: Array<string>,
createPaymentRequest: PaymentDetailsBase => Promise<any>,
handleDetailsUpdate: PaymentDetailsBase => Promise<any>,
show: () => Promise<any>,
abort: () => Promise<any>,
complete: PaymentComplete => Promise<any>,
getFullWalletAndroid: string => Promise<any>
complete: PaymentComplete => Promise<any>
} = {
supportedGateways: IS_ANDROID
? ['stripe', 'braintree'] // On Android, Payment Gateways are supported out of the gate.
? [] // On Android, Payment Gateways are supported out of the gate.
: ReactNativePayments ? ReactNativePayments.supportedGateways : [],

canMakePayments(methodData: object) {
canMakePayments(methodData?: CanMakePayments) {
return new Promise((resolve, reject) => {
if (IS_ANDROID) {
ReactNativePayments.canMakePayments(
methodData,
(err) => reject(err),
(canMakePayments) => resolve(true)
() => resolve(true)
);

return;
Expand Down Expand Up @@ -133,30 +132,6 @@ const NativePayments: {
resolve(true);
});
});
},

getFullWalletAndroid(googleTransactionId: string, paymentMethodData: object, details: object): Promise<string> {
return new Promise((resolve, reject) => {
if (!IS_ANDROID) {
reject(new Error('This method is only available on Android.'));

return;
}

ReactNativePayments.getFullWalletAndroid(
googleTransactionId,
paymentMethodData,
details,
(err) => reject(err),
(serializedPaymentToken) => resolve({
serializedPaymentToken,
paymentToken: JSON.parse(serializedPaymentToken),
/** Leave previous typo in order not to create a breaking change **/
serializedPaymenToken: serializedPaymentToken,
paymenToken: JSON.parse(serializedPaymentToken)
})
);
});
}
};

Expand Down
39 changes: 15 additions & 24 deletions packages/react-native-payments/lib/js/PaymentRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export default class PaymentRequest {
const normalizedDetails = convertDetailAmountsToString(details);

// Validate gateway config if present
if (hasGatewayConfig(platformMethodData)) {
if (IS_IOS && hasGatewayConfig(platformMethodData)) {
validateGateway(
getGatewayName(platformMethodData),
NativePayments.supportedGateways
Expand Down Expand Up @@ -312,35 +312,33 @@ export default class PaymentRequest {
}

_getPlatformDetailsAndroid(details: {
cardInfo: Object,
googleTransactionId: string,
payerEmail: string,
paymentDescription: string,
shippingAddress: Object,
paymentToken: Object,
shippingAddress?: Object,
}) {
const {
cardInfo,
googleTransactionId,
paymentDescription
paymentToken,
} = details;

return {
cardInfo,
googleTransactionId,
paymentDescription,
// On Android, the recommended flow is to have user's confirm prior to
// retrieving the full wallet.
getPaymentToken: () => NativePayments.getFullWalletAndroid(
googleTransactionId,
getPlatformMethodData(JSON.parse(this._serializedMethodData, Platform.OS)),
convertDetailAmountsToString(this._details)
)
paymentToken,
};
}

_handleUserAccept(details: {
transactionIdentifier: string,
paymentData: string,
shippingAddress: Object,
payerEmail: string,
paymentToken?: string,
cardInfo?: Object,
googleTransactionId?: string,
transactionIdentifier?: string,
paymentData?: Object,
shippingAddress?: Object,
payerEmail?: string,
paymentToken: Object | string,
}) {
// On Android, we don't have `onShippingAddressChange` events, so we
// set the shipping address when the user accepts.
Expand Down Expand Up @@ -469,12 +467,5 @@ export default class PaymentRequest {
});
});
}

// https://www.w3.org/TR/payment-request/#canmakepayment-method
canMakePayments(): Promise<boolean> {
return NativePayments.canMakePayments(
getPlatformMethodData(JSON.parse(this._serializedMethodData), Platform.OS)
);
}
}

2 changes: 2 additions & 0 deletions packages/react-native-payments/lib/js/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// @flow

import _PaymentRequest from './PaymentRequest';
import _NativePayments from './NativePayments';
import { PKPaymentButton } from './PKPaymentButton';

export const ApplePayButton = PKPaymentButton;
export const PaymentRequest = _PaymentRequest;
export const canMakePayments = _NativePayments.canMakePayments;
6 changes: 6 additions & 0 deletions packages/react-native-payments/lib/js/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,9 @@ export type PaymentDetailsIOSRaw = {
paymentToken?: string,
transactionIdentifier: string,
};

export type CanMakePayments = {
supportedNetworks: Array<string>,
allowedPaymentMethods: Array<number>,
environment: string
};