@capgo/capacitor-pay 7.0.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.
@@ -0,0 +1,245 @@
1
+ package app.capgo.pay;
2
+
3
+ import android.app.Activity;
4
+ import android.content.Intent;
5
+ import androidx.annotation.Nullable;
6
+ import com.getcapacitor.JSObject;
7
+ import com.getcapacitor.Logger;
8
+ import com.getcapacitor.Plugin;
9
+ import com.getcapacitor.PluginCall;
10
+ import com.getcapacitor.PluginMethod;
11
+ import com.getcapacitor.annotation.CapacitorPlugin;
12
+ import com.google.android.gms.common.api.ApiException;
13
+ import com.google.android.gms.common.api.Status;
14
+ import com.google.android.gms.tasks.Task;
15
+ import com.google.android.gms.wallet.AutoResolveHelper;
16
+ import com.google.android.gms.wallet.IsReadyToPayRequest;
17
+ import com.google.android.gms.wallet.PaymentData;
18
+ import com.google.android.gms.wallet.PaymentDataRequest;
19
+ import com.google.android.gms.wallet.PaymentsClient;
20
+ import com.google.android.gms.wallet.Wallet;
21
+ import com.google.android.gms.wallet.WalletConstants;
22
+ import org.json.JSONArray;
23
+ import org.json.JSONException;
24
+ import org.json.JSONObject;
25
+
26
+ @CapacitorPlugin(name = "Pay")
27
+ public class PayPlugin extends Plugin {
28
+
29
+ private static final int LOAD_PAYMENT_DATA_REQUEST_CODE = 8001;
30
+
31
+ private PluginCall pendingPaymentCall;
32
+
33
+ @PluginMethod
34
+ public void isPayAvailable(PluginCall call) {
35
+ JSObject googleOptions = call.getObject("google");
36
+ int environment = parseEnvironment(googleOptions != null ? googleOptions.getString("environment", "test") : "test");
37
+
38
+ JSONObject requestJson;
39
+ try {
40
+ requestJson = getIsReadyToPayRequest(googleOptions);
41
+ } catch (JSONException ex) {
42
+ call.reject("Invalid Google Pay `isReadyToPayRequest`.", ex);
43
+ return;
44
+ }
45
+
46
+ if (requestJson == null) {
47
+ JSObject result = buildAvailabilityResult(false);
48
+ call.resolve(result);
49
+ return;
50
+ }
51
+
52
+ PaymentsClient client = createPaymentsClient(environment);
53
+
54
+ try {
55
+ IsReadyToPayRequest request = IsReadyToPayRequest.fromJson(requestJson.toString());
56
+ Task<Boolean> readyTask = client.isReadyToPay(request);
57
+ readyTask.addOnCompleteListener(task -> {
58
+ boolean isReady = false;
59
+ if (task.isSuccessful()) {
60
+ Boolean value = task.getResult();
61
+ isReady = value != null && value;
62
+ } else {
63
+ Exception exception = task.getException();
64
+ if (exception instanceof ApiException) {
65
+ Logger.debug("PayPlugin", "Google Pay isReadyToPay ApiException: " + exception.getLocalizedMessage());
66
+ } else if (exception != null) {
67
+ Logger.debug("PayPlugin", "Google Pay isReadyToPay error: " + exception.getLocalizedMessage());
68
+ }
69
+ }
70
+
71
+ JSObject result = buildAvailabilityResult(isReady);
72
+ call.resolve(result);
73
+ });
74
+ } catch (Exception ex) {
75
+ call.reject("Failed to check Google Pay availability.", ex);
76
+ }
77
+ }
78
+
79
+ @PluginMethod
80
+ public void requestPayment(PluginCall call) {
81
+ if (pendingPaymentCall != null) {
82
+ call.reject("Another Google Pay request is already in progress.");
83
+ return;
84
+ }
85
+
86
+ JSObject googleOptions = call.getObject("google");
87
+ if (googleOptions == null) {
88
+ call.reject("Google Pay configuration is required on Android.");
89
+ return;
90
+ }
91
+
92
+ JSObject paymentDataRequestObj = googleOptions.getJSObject("paymentDataRequest");
93
+
94
+ if (paymentDataRequestObj == null || paymentDataRequestObj.length() == 0) {
95
+ call.reject("`paymentDataRequest` is required.");
96
+ return;
97
+ }
98
+
99
+ int environment = parseEnvironment(googleOptions.getString("environment", "test"));
100
+ PaymentsClient client = createPaymentsClient(environment);
101
+
102
+ JSONObject paymentRequestJson;
103
+ try {
104
+ paymentRequestJson = new JSONObject(paymentDataRequestObj.toString());
105
+ } catch (JSONException ex) {
106
+ call.reject("Invalid `paymentDataRequest`.", ex);
107
+ return;
108
+ }
109
+
110
+ try {
111
+ PaymentDataRequest request = PaymentDataRequest.fromJson(paymentRequestJson.toString());
112
+ if (request == null) {
113
+ call.reject("Invalid `paymentDataRequest`.");
114
+ return;
115
+ }
116
+
117
+ pendingPaymentCall = call;
118
+ Task<PaymentData> task = client.loadPaymentData(request);
119
+ Activity activity = getActivity();
120
+ if (activity == null) {
121
+ pendingPaymentCall = null;
122
+ call.reject("No active activity to present Google Pay.");
123
+ return;
124
+ }
125
+ AutoResolveHelper.resolveTask(task, activity, LOAD_PAYMENT_DATA_REQUEST_CODE);
126
+ } catch (Exception ex) {
127
+ pendingPaymentCall = null;
128
+ call.reject("Failed to launch Google Pay.", ex);
129
+ }
130
+ }
131
+
132
+ @Override
133
+ protected void handleOnActivityResult(int requestCode, int resultCode, Intent data) {
134
+ super.handleOnActivityResult(requestCode, resultCode, data);
135
+
136
+ if (requestCode != LOAD_PAYMENT_DATA_REQUEST_CODE) {
137
+ return;
138
+ }
139
+
140
+ if (pendingPaymentCall == null) {
141
+ return;
142
+ }
143
+
144
+ PluginCall call = pendingPaymentCall;
145
+ pendingPaymentCall = null;
146
+
147
+ if (resultCode == Activity.RESULT_OK) {
148
+ if (data == null) {
149
+ call.reject("Google Pay returned no data.");
150
+ return;
151
+ }
152
+
153
+ PaymentData paymentData = PaymentData.getFromIntent(data);
154
+ if (paymentData == null) {
155
+ call.reject("Google Pay returned empty payment data.");
156
+ return;
157
+ }
158
+
159
+ try {
160
+ String json = paymentData.toJson();
161
+ JSONObject paymentDataJson = json != null ? new JSONObject(json) : new JSONObject();
162
+ JSObject googleResult = new JSObject();
163
+ googleResult.put("paymentData", JSObject.fromJSONObject(paymentDataJson));
164
+
165
+ JSObject result = new JSObject();
166
+ result.put("platform", "android");
167
+ result.put("google", googleResult);
168
+
169
+ call.resolve(result);
170
+ } catch (JSONException ex) {
171
+ call.reject("Failed to parse Google Pay result.", ex);
172
+ }
173
+ } else if (resultCode == Activity.RESULT_CANCELED) {
174
+ call.reject("Payment canceled.");
175
+ } else if (resultCode == AutoResolveHelper.RESULT_ERROR) {
176
+ Status status = AutoResolveHelper.getStatusFromIntent(data);
177
+ String message = status != null && status.getStatusMessage() != null
178
+ ? status.getStatusMessage()
179
+ : "Google Pay returned an error.";
180
+ call.reject(message);
181
+ } else {
182
+ call.reject("Unexpected activity result: " + resultCode);
183
+ }
184
+ }
185
+
186
+ private JSObject buildAvailabilityResult(boolean isReady) {
187
+ JSObject google = new JSObject();
188
+ google.put("isReady", isReady);
189
+
190
+ JSObject result = new JSObject();
191
+ result.put("available", isReady);
192
+ result.put("platform", "android");
193
+ result.put("google", google);
194
+
195
+ return result;
196
+ }
197
+
198
+ private PaymentsClient createPaymentsClient(int environment) {
199
+ Wallet.WalletOptions options = new Wallet.WalletOptions.Builder()
200
+ .setEnvironment(environment)
201
+ .build();
202
+ return Wallet.getPaymentsClient(getContext(), options);
203
+ }
204
+
205
+ private int parseEnvironment(@Nullable String environment) {
206
+ if (environment != null && environment.equalsIgnoreCase("production")) {
207
+ return WalletConstants.ENVIRONMENT_PRODUCTION;
208
+ }
209
+ return WalletConstants.ENVIRONMENT_TEST;
210
+ }
211
+
212
+ @Nullable
213
+ private JSONObject getIsReadyToPayRequest(@Nullable JSObject googleOptions) throws JSONException {
214
+ if (googleOptions != null) {
215
+ JSObject requestObj = googleOptions.getJSObject("isReadyToPayRequest");
216
+ if (requestObj != null && requestObj.length() > 0) {
217
+ return new JSONObject(requestObj.toString());
218
+ }
219
+ }
220
+ return GooglePayRequestFactory.defaultIsReadyToPayRequest();
221
+ }
222
+
223
+ private static final class GooglePayRequestFactory {
224
+ private static JSONObject defaultIsReadyToPayRequest() throws JSONException {
225
+ JSONObject cardPaymentMethod = new JSONObject();
226
+ cardPaymentMethod.put("type", "CARD");
227
+
228
+ JSONObject parameters = new JSONObject();
229
+ parameters.put("allowedAuthMethods", new JSONArray()
230
+ .put("PAN_ONLY")
231
+ .put("CRYPTOGRAM_3DS"));
232
+ parameters.put("allowedCardNetworks", new JSONArray()
233
+ .put("AMEX")
234
+ .put("DISCOVER")
235
+ .put("MASTERCARD")
236
+ .put("VISA"));
237
+ cardPaymentMethod.put("parameters", parameters);
238
+
239
+ JSONObject request = new JSONObject();
240
+ request.put("allowedPaymentMethods", new JSONArray().put(cardPaymentMethod));
241
+
242
+ return request;
243
+ }
244
+ }
245
+ }
File without changes