@rnw-community/react-native-payments 0.83.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. package/LICENSE.md +1 -1
  2. package/dist/cjs/app.plugin.d.ts +3 -0
  3. package/dist/cjs/app.plugin.d.ts.map +1 -0
  4. package/dist/cjs/app.plugin.js +5 -0
  5. package/dist/cjs/app.plugin.js.map +1 -0
  6. package/dist/cjs/class/payment-request/payment-request.d.ts.map +1 -1
  7. package/dist/cjs/class/payment-request/payment-request.js +4 -3
  8. package/dist/cjs/class/payment-request/payment-request.js.map +1 -1
  9. package/dist/cjs/class/payment-request/payment-request.web.d.ts +5 -0
  10. package/dist/cjs/class/payment-request/payment-request.web.d.ts.map +1 -0
  11. package/dist/cjs/class/payment-request/payment-request.web.js +5 -0
  12. package/dist/cjs/class/payment-request/payment-request.web.js.map +1 -0
  13. package/dist/cjs/class/payment-response/payment-response.d.ts.map +1 -1
  14. package/dist/cjs/class/payment-response/payment-response.js +4 -3
  15. package/dist/cjs/class/payment-response/payment-response.js.map +1 -1
  16. package/dist/cjs/class/payment-response/payment-response.web.d.ts +5 -0
  17. package/dist/cjs/class/payment-response/payment-response.web.d.ts.map +1 -0
  18. package/dist/cjs/class/payment-response/payment-response.web.js +5 -0
  19. package/dist/cjs/class/payment-response/payment-response.web.js.map +1 -0
  20. package/dist/cjs/expo-plugins/plugin.props.d.ts +4 -0
  21. package/dist/cjs/expo-plugins/plugin.props.d.ts.map +1 -0
  22. package/dist/cjs/expo-plugins/plugin.props.js +3 -0
  23. package/dist/cjs/expo-plugins/plugin.props.js.map +1 -0
  24. package/dist/cjs/expo-plugins/with-apple-pay.d.ts +4 -0
  25. package/dist/cjs/expo-plugins/with-apple-pay.d.ts.map +1 -0
  26. package/dist/cjs/expo-plugins/with-apple-pay.js +24 -0
  27. package/dist/cjs/expo-plugins/with-apple-pay.js.map +1 -0
  28. package/dist/cjs/expo-plugins/with-google-pay.d.ts +4 -0
  29. package/dist/cjs/expo-plugins/with-google-pay.d.ts.map +1 -0
  30. package/dist/cjs/expo-plugins/with-google-pay.js +26 -0
  31. package/dist/cjs/expo-plugins/with-google-pay.js.map +1 -0
  32. package/dist/cjs/expo-plugins/with-payments.d.ts +4 -0
  33. package/dist/cjs/expo-plugins/with-payments.d.ts.map +1 -0
  34. package/dist/cjs/expo-plugins/with-payments.js +12 -0
  35. package/dist/cjs/expo-plugins/with-payments.js.map +1 -0
  36. package/dist/cjs/util/is-valid-decimal-monetary-value.util.js +2 -2
  37. package/dist/cjs/util/is-valid-decimal-monetary-value.util.js.map +1 -1
  38. package/dist/cjs/util/validate-payment-methods.util.js +1 -1
  39. package/dist/cjs/util/validate-payment-methods.util.js.map +1 -1
  40. package/dist/esm/app.plugin.d.ts +3 -0
  41. package/dist/esm/app.plugin.d.ts.map +1 -0
  42. package/dist/esm/app.plugin.js +3 -0
  43. package/dist/esm/app.plugin.js.map +1 -0
  44. package/dist/esm/class/payment-request/payment-request.d.ts.map +1 -1
  45. package/dist/esm/class/payment-request/payment-request.js +5 -4
  46. package/dist/esm/class/payment-request/payment-request.js.map +1 -1
  47. package/dist/esm/class/payment-request/payment-request.web.d.ts +5 -0
  48. package/dist/esm/class/payment-request/payment-request.web.d.ts.map +1 -0
  49. package/dist/esm/class/payment-request/payment-request.web.js +2 -0
  50. package/dist/esm/class/payment-request/payment-request.web.js.map +1 -0
  51. package/dist/esm/class/payment-response/payment-response.d.ts.map +1 -1
  52. package/dist/esm/class/payment-response/payment-response.js +4 -3
  53. package/dist/esm/class/payment-response/payment-response.js.map +1 -1
  54. package/dist/esm/class/payment-response/payment-response.web.d.ts +5 -0
  55. package/dist/esm/class/payment-response/payment-response.web.d.ts.map +1 -0
  56. package/dist/esm/class/payment-response/payment-response.web.js +2 -0
  57. package/dist/esm/class/payment-response/payment-response.web.js.map +1 -0
  58. package/dist/esm/expo-plugins/plugin.props.d.ts +4 -0
  59. package/dist/esm/expo-plugins/plugin.props.d.ts.map +1 -0
  60. package/dist/esm/expo-plugins/plugin.props.js +2 -0
  61. package/dist/esm/expo-plugins/plugin.props.js.map +1 -0
  62. package/dist/esm/expo-plugins/with-apple-pay.d.ts +4 -0
  63. package/dist/esm/expo-plugins/with-apple-pay.d.ts.map +1 -0
  64. package/dist/esm/expo-plugins/with-apple-pay.js +20 -0
  65. package/dist/esm/expo-plugins/with-apple-pay.js.map +1 -0
  66. package/dist/esm/expo-plugins/with-google-pay.d.ts +4 -0
  67. package/dist/esm/expo-plugins/with-google-pay.d.ts.map +1 -0
  68. package/dist/esm/expo-plugins/with-google-pay.js +22 -0
  69. package/dist/esm/expo-plugins/with-google-pay.js.map +1 -0
  70. package/dist/esm/expo-plugins/with-payments.d.ts +4 -0
  71. package/dist/esm/expo-plugins/with-payments.d.ts.map +1 -0
  72. package/dist/esm/expo-plugins/with-payments.js +8 -0
  73. package/dist/esm/expo-plugins/with-payments.js.map +1 -0
  74. package/dist/esm/util/is-valid-decimal-monetary-value.util.js +2 -2
  75. package/dist/esm/util/is-valid-decimal-monetary-value.util.js.map +1 -1
  76. package/dist/esm/util/validate-payment-methods.util.js +2 -2
  77. package/dist/esm/util/validate-payment-methods.util.js.map +1 -1
  78. package/ios/Payments.mm +9 -1
  79. package/package.json +11 -13
  80. package/react-native-payments.podspec +21 -18
  81. package/readme.md +33 -27
  82. package/src/app.plugin.ts +3 -0
  83. package/src/class/payment-request/payment-request.spec.ts +691 -0
  84. package/src/class/payment-request/payment-request.ts +5 -4
  85. package/src/class/payment-request/payment-request.web.ts +1 -0
  86. package/src/class/payment-response/payment-response.spec.ts +142 -0
  87. package/src/class/payment-response/payment-response.ts +4 -3
  88. package/src/class/payment-response/payment-response.web.ts +1 -0
  89. package/src/expo-plugins/plugin.props.ts +3 -0
  90. package/src/expo-plugins/with-apple-pay.ts +27 -0
  91. package/src/expo-plugins/with-google-pay.ts +31 -0
  92. package/src/expo-plugins/with-payments.ts +13 -0
  93. package/src/util/is-valid-decimal-monetary-value.util.ts +2 -2
  94. package/src/util/validate-payment-methods.util.ts +2 -2
  95. package/app.plugin.js +0 -1
  96. package/plugins/with-apple-pay.js +0 -24
  97. package/plugins/with-google-pay.js +0 -49
  98. package/plugins/with-payments.js +0 -12
@@ -2,7 +2,7 @@
2
2
  import { Platform } from 'react-native';
3
3
  import uuid from 'react-native-uuid';
4
4
 
5
- import { emptyFn, getErrorMessage, isDefined, isNotEmptyArray, isNotEmptyString } from '@rnw-community/shared';
5
+ import { emptyFn, isDefined, isNotEmptyArray, isNotEmptyString } from '@rnw-community/shared';
6
6
 
7
7
  import { AndroidPaymentMethodTokenizationType } from '../../@standard/android/enum/android-payment-method-tokenization-type.enum';
8
8
  import { defaultAndroidPaymentDataRequest } from '../../@standard/android/request/android-payment-data-request';
@@ -128,7 +128,7 @@ export class PaymentRequest {
128
128
  }
129
129
 
130
130
  await NativePayments.abort().catch(() => {
131
- throw new DOMException(PaymentsErrorEnum.InvalidStateError);
131
+ throw new PaymentsError(`Failed aborting PaymentRequest`);
132
132
  });
133
133
 
134
134
  this.state = 'closed';
@@ -141,8 +141,9 @@ export class PaymentRequest {
141
141
  return Platform.OS === 'android'
142
142
  ? new AndroidPaymentResponse(this.id, PaymentMethodNameEnum.AndroidPay, details)
143
143
  : new IosPaymentResponse(this.id, PaymentMethodNameEnum.ApplePay, details);
144
- } catch (e) {
145
- throw new PaymentsError(`Failed creating AndroidPaymentResponse: ${getErrorMessage(e)}`);
144
+ } catch (_e) {
145
+ // TODO: Is there an standard exception for this?
146
+ throw new PaymentsError(`Failed parsing PaymentRequest details`);
146
147
  }
147
148
  }
148
149
 
@@ -0,0 +1 @@
1
+ export const { PaymentRequest } = typeof window === 'undefined' ? { PaymentRequest: null } : window;
@@ -0,0 +1,142 @@
1
+ import { beforeEach, describe, expect, it, jest } from '@jest/globals';
2
+
3
+ import { IosPKPaymentMethodType } from '../../@standard/ios/enum/ios-pk-payment-method-type.enum';
4
+ import { PaymentComplete } from '../../enum/payment-complete.enum';
5
+ import { NativePayments } from '../native-payments/native-payments';
6
+
7
+ import { PaymentResponse } from './payment-response';
8
+
9
+ import type { PaymentResponseDetailsInterface } from '../../interface/payment-response-details.interface';
10
+
11
+ jest.mock('../native-payments/native-payments', () => ({
12
+ NativePayments: {
13
+ complete: jest.fn(),
14
+ },
15
+ }));
16
+
17
+ jest.mock('react-native', () => ({
18
+ Platform: {
19
+ OS: 'android',
20
+ },
21
+ }));
22
+
23
+ // eslint-disable-next-line max-lines-per-function
24
+ describe('PaymentResponse', () => {
25
+ const mockDetails: PaymentResponseDetailsInterface = {
26
+ androidPayToken: {
27
+ cardInfo: {
28
+ assuranceDetails: {
29
+ accountVerified: true,
30
+ cardHolderAuthenticated: false,
31
+ },
32
+ cardDetails: '****1234',
33
+ cardNetwork: 'VISA',
34
+ billingAddress: {
35
+ address1: '123 Test St',
36
+ locality: 'Test City',
37
+ administrativeArea: 'CA',
38
+ sortingCode: '123',
39
+ countryCode: 'US',
40
+ name: 'Test Name',
41
+ postalCode: '12345',
42
+ },
43
+ },
44
+ intermediateSigningKey: {
45
+ signedKey: {
46
+ keyExpiration: '',
47
+ keyValue: 'mockSignedKeyValue',
48
+ },
49
+ signatures: 'mockSignature1',
50
+ },
51
+ protocolVersion: 'EC_v1',
52
+ rawToken: 'mockRawToken',
53
+ signature: 'mockSignature',
54
+ signedMessage: {
55
+ encryptedMessage: 'mockEncryptedMessage',
56
+ ephemeralPublicKey: 'mockEphemeralKey',
57
+ tag: 'mockTag',
58
+ },
59
+ },
60
+ applePayToken: {
61
+ paymentData: {
62
+ version: 'EC_v1',
63
+ data: 'mockData',
64
+ signature: 'mockSignature',
65
+ header: {
66
+ ephemeralPublicKey: 'mockEphemeralPublicKey',
67
+ publicKeyHash: 'mockPublicKeyHash',
68
+ transactionId: 'mockTransactionId',
69
+ wrappedKey: 'mockWrappedKey',
70
+ },
71
+ },
72
+ paymentMethod: {
73
+ displayName: 'Test Card',
74
+ network: 'Visa',
75
+ type: IosPKPaymentMethodType.PKPaymentMethodTypeCredit,
76
+ },
77
+ transactionIdentifier: 'testTransactionId',
78
+ },
79
+ };
80
+
81
+ // eslint-disable-next-line jest/no-hooks
82
+ beforeEach(() => {
83
+ jest.clearAllMocks();
84
+ });
85
+
86
+ describe('constructor', () => {
87
+ it('should correctly initialize properties', () => {
88
+ expect.assertions(3);
89
+
90
+ const paymentResponse = new PaymentResponse('testRequestId', 'testMethodName', mockDetails);
91
+
92
+ expect(paymentResponse.requestId).toBe('testRequestId');
93
+ expect(paymentResponse.methodName).toBe('testMethodName');
94
+ expect(paymentResponse.details).toBe(mockDetails);
95
+ });
96
+ });
97
+
98
+ describe('complete', () => {
99
+ it('should call NativePayments.complete with the correct result', async () => {
100
+ expect.assertions(1);
101
+
102
+ const paymentResponse = new PaymentResponse('testRequestId', 'testMethodName', mockDetails);
103
+
104
+ jest.mocked(NativePayments.complete).mockResolvedValueOnce(undefined);
105
+ await paymentResponse.complete(PaymentComplete.SUCCESS);
106
+
107
+ expect(NativePayments.complete).toHaveBeenCalledWith(PaymentComplete.SUCCESS);
108
+ });
109
+
110
+ it('should throw an error if complete is called more than once', async () => {
111
+ expect.assertions(1);
112
+
113
+ const paymentResponse = new PaymentResponse('testRequestId', 'testMethodName', mockDetails);
114
+
115
+ jest.mocked(NativePayments.complete).mockResolvedValueOnce(undefined);
116
+ await paymentResponse.complete(PaymentComplete.SUCCESS);
117
+
118
+ await expect(paymentResponse.complete(PaymentComplete.SUCCESS)).rejects.toThrow(new Error('InvalidStateError'));
119
+ });
120
+ });
121
+
122
+ describe('retry', () => {
123
+ it('should resolve with undefined when called', async () => {
124
+ expect.assertions(1);
125
+
126
+ const paymentResponse = new PaymentResponse('testRequestId', 'testMethodName', mockDetails);
127
+
128
+ await expect(paymentResponse.retry()).resolves.toBeUndefined();
129
+ });
130
+
131
+ it('should throw an error if retry is called after complete', async () => {
132
+ expect.assertions(1);
133
+
134
+ const paymentResponse = new PaymentResponse('testRequestId', 'testMethodName', mockDetails);
135
+
136
+ jest.mocked(NativePayments.complete).mockResolvedValueOnce(undefined);
137
+ await paymentResponse.complete(PaymentComplete.SUCCESS);
138
+
139
+ await expect(paymentResponse.retry()).rejects.toThrow('InvalidStateError');
140
+ });
141
+ });
142
+ });
@@ -20,7 +20,7 @@ export class PaymentResponse {
20
20
  ) {}
21
21
 
22
22
  // https://www.w3.org/TR/payment-request/#complete-method
23
- complete(result: PaymentComplete): Promise<void> {
23
+ async complete(result: PaymentComplete): Promise<void> {
24
24
  if (this.completeCalled) {
25
25
  throw new Error('InvalidStateError');
26
26
  }
@@ -33,13 +33,14 @@ export class PaymentResponse {
33
33
  }
34
34
 
35
35
  // https://www.w3.org/TR/payment-request/#dom-paymentresponse-retry
36
- retry(_errorFields?: PaymentValidationErrors): Promise<undefined> {
36
+ // eslint-disable-next-line @typescript-eslint/require-await
37
+ async retry(_errorFields?: PaymentValidationErrors): Promise<undefined> {
37
38
  if (this.completeCalled) {
38
39
  throw new Error('InvalidStateError');
39
40
  }
40
41
 
41
42
  // TODO: Implement logic https://www.w3.org/TR/payment-request/#retry-method
42
43
 
43
- return Promise.resolve(undefined);
44
+ return undefined;
44
45
  }
45
46
  }
@@ -0,0 +1 @@
1
+ export const { PaymentResponse } = typeof window === 'undefined' ? { PaymentResponse: null } : window;
@@ -0,0 +1,3 @@
1
+ export interface ReactNativePaymentsPluginProps {
2
+ merchantIdentifier: string;
3
+ }
@@ -0,0 +1,27 @@
1
+ import { withEntitlementsPlist } from 'expo/config-plugins';
2
+
3
+ import { isDefined } from '@rnw-community/shared';
4
+
5
+ import type { ReactNativePaymentsPluginProps } from './plugin.props';
6
+ import type { ConfigPlugin } from 'expo/config-plugins';
7
+
8
+ export const withApplePay: ConfigPlugin<ReactNativePaymentsPluginProps> = (initialConfig, { merchantIdentifier }) => {
9
+ if (!isDefined(merchantIdentifier)) {
10
+ throw new Error(`Pleas provide "@rnw-community/react-native-payments" plugin option "merchantIdentifier"`);
11
+ }
12
+
13
+ return withEntitlementsPlist(initialConfig, configWithEntitlements => {
14
+ if (merchantIdentifier) {
15
+ if (!isDefined(configWithEntitlements.modResults['com.apple.developer.in-app-payments'])) {
16
+ configWithEntitlements.modResults['com.apple.developer.in-app-payments'] = [];
17
+ }
18
+
19
+ const applePayArray = configWithEntitlements.modResults['com.apple.developer.in-app-payments'] as string[];
20
+ if (!applePayArray.includes(merchantIdentifier)) {
21
+ applePayArray.push(merchantIdentifier);
22
+ }
23
+ }
24
+
25
+ return configWithEntitlements;
26
+ });
27
+ };
@@ -0,0 +1,31 @@
1
+ import { withAndroidManifest } from '@expo/config-plugins';
2
+
3
+ import type { ReactNativePaymentsPluginProps } from './plugin.props';
4
+ import type { ConfigPlugin } from '@expo/config-plugins';
5
+
6
+ export const withGooglePay: ConfigPlugin<ReactNativePaymentsPluginProps> = initialConfig =>
7
+ withAndroidManifest(initialConfig, config => {
8
+ const androidManifest = config.modResults;
9
+ const mainApplication = androidManifest.manifest.application?.[0];
10
+
11
+ if (mainApplication) {
12
+ const existingMetaData = mainApplication['meta-data']?.find(
13
+ metadata => metadata.$['android:name'] === 'com.google.android.gms.wallet.api.enabled'
14
+ );
15
+
16
+ if (!existingMetaData) {
17
+ if (!mainApplication['meta-data']) {
18
+ mainApplication['meta-data'] = [];
19
+ }
20
+ mainApplication['meta-data'].push({
21
+ // eslint-disable-next-line id-length
22
+ $: {
23
+ 'android:name': 'com.google.android.gms.wallet.api.enabled',
24
+ 'android:value': 'true',
25
+ },
26
+ });
27
+ }
28
+ }
29
+
30
+ return config;
31
+ });
@@ -0,0 +1,13 @@
1
+ import { withPlugins } from '@expo/config-plugins';
2
+
3
+ import { withApplePay } from './with-apple-pay';
4
+ import { withGooglePay } from './with-google-pay';
5
+
6
+ import type { ReactNativePaymentsPluginProps } from './plugin.props';
7
+ import type { ConfigPlugin } from '@expo/config-plugins';
8
+
9
+ export const withPayments: ConfigPlugin<ReactNativePaymentsPluginProps> = (config, props) =>
10
+ withPlugins(config, [
11
+ [withApplePay, props],
12
+ [withGooglePay, props],
13
+ ]);
@@ -1,4 +1,4 @@
1
- import isDecimal from 'validator/es/lib/isDecimal';
1
+ import validator from 'validator';
2
2
 
3
3
  import { isNumber, isString } from '@rnw-community/shared';
4
4
 
@@ -9,7 +9,7 @@ const isValidStringAmount = (stringAmount: string): boolean => {
9
9
  return false;
10
10
  }
11
11
 
12
- return isDecimal(stringAmount);
12
+ return validator.isDecimal(stringAmount);
13
13
  };
14
14
 
15
15
  export const isValidDecimalMonetaryValue = (amountValue: AmountValue): boolean => {
@@ -1,4 +1,4 @@
1
- import { isDefined } from '@rnw-community/shared';
1
+ import { isDefined, isNotEmptyArray } from '@rnw-community/shared';
2
2
 
3
3
  import { ConstructorError } from '../error/constructor.error';
4
4
 
@@ -7,7 +7,7 @@ import type { PaymentMethodData } from '../@standard/w3c/payment-method-data';
7
7
  /** @deprecated Move to PaymentRequest */
8
8
  export const validatePaymentMethods = (methodData: PaymentMethodData[]): void => {
9
9
  // Check that at least one payment method is passed in
10
- if (methodData.length < 1) {
10
+ if (!isNotEmptyArray(methodData)) {
11
11
  throw new ConstructorError(`At least one payment method is required`);
12
12
  }
13
13
 
package/app.plugin.js DELETED
@@ -1 +0,0 @@
1
- module.exports = require('./plugins/with-payments');
@@ -1,24 +0,0 @@
1
- const { withDangerousMod } = require("@expo/config-plugins");
2
- const fs = require("fs");
3
- const path = require("path");
4
-
5
- const withApplePay = (config) =>
6
- withDangerousMod(config, [
7
- "ios",
8
- async (config) => {
9
- const filePath = path.join(
10
- config.modRequest.platformProjectRoot,
11
- config.modRequest.projectName,
12
- "AppDelegate.h"
13
- );
14
-
15
- const content = fs.readFileSync(filePath, "utf-8");
16
- if (!content.includes("#import <PassKit/PassKit.h>")) {
17
- fs.writeFileSync(filePath, `#import <PassKit/PassKit.h>\n${content}`);
18
- }
19
-
20
- return config;
21
- },
22
- ]);
23
-
24
- module.exports = { withApplePay };
@@ -1,49 +0,0 @@
1
- const { withAppBuildGradle, withAndroidManifest } = require("@expo/config-plugins");
2
-
3
- const withGooglePay = (config) => {
4
- // Add Google Pay dependency to app/build.gradle
5
- config = withAppBuildGradle(config, (config) => {
6
- if (config.modResults.language === "groovy") {
7
- config.modResults.contents = config.modResults.contents.replace(
8
- /dependencies\s?{/,
9
- `dependencies {
10
- implementation 'com.google.android.gms:play-services-wallet:19.2.0'`,
11
- );
12
- } else {
13
- throw new Error(
14
- "Unable to add Google Pay dependency to build.gradle: Kotlin build files are not supported."
15
- );
16
- }
17
- return config;
18
- });
19
-
20
- // Add meta-data to AndroidManifest.xml
21
- config = withAndroidManifest(config, async (config) => {
22
- const androidManifest = config.modResults;
23
- const mainApplication = androidManifest.manifest.application[0];
24
-
25
- // Check if the meta-data already exists
26
- const existingMetaData = mainApplication["meta-data"]?.find(
27
- (metadata) => metadata.$["android:name"] === "com.google.android.gms.wallet.api.enabled"
28
- );
29
-
30
- if (!existingMetaData) {
31
- // Add the new meta-data
32
- if (!mainApplication["meta-data"]) {
33
- mainApplication["meta-data"] = [];
34
- }
35
- mainApplication["meta-data"].push({
36
- $: {
37
- "android:name": "com.google.android.gms.wallet.api.enabled",
38
- "android:value": "true",
39
- },
40
- });
41
- }
42
-
43
- return config;
44
- });
45
-
46
- return config;
47
- };
48
-
49
- module.exports = { withGooglePay };
@@ -1,12 +0,0 @@
1
- const { withPlugins } = require('@expo/config-plugins');
2
- const { withApplePay } = require('./with-apple-pay');
3
- const { withGooglePay } = require('./with-google-pay');
4
-
5
- const withPayments = (config) => {
6
- return withPlugins(config, [
7
- withApplePay,
8
- withGooglePay
9
- ]);
10
- };
11
-
12
- module.exports = withPayments;