@capawesome/capacitor-square-mobile-payments 0.1.3 → 0.1.5

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.
package/README.md CHANGED
@@ -40,6 +40,38 @@ npx cap sync
40
40
 
41
41
  ### Android
42
42
 
43
+ #### SDK Initialization
44
+
45
+ The Square Mobile Payments SDK must be initialized in your `Application` class before using the plugin. Create a custom `Application` class (if you don't already have one) and add the following code:
46
+
47
+ 1. Create a file `MainApplication.java` in your app's `android/app/src/main/java/<your-package>/` directory:
48
+
49
+ ```java
50
+ package com.example.app;
51
+
52
+ import android.app.Application;
53
+ import com.squareup.sdk.mobilepayments.MobilePaymentsSdk;
54
+
55
+ public class MainApplication extends Application {
56
+
57
+ @Override
58
+ public void onCreate() {
59
+ super.onCreate();
60
+ MobilePaymentsSdk.initialize("YOUR_SQUARE_APPLICATION_ID", this);
61
+ }
62
+ }
63
+ ```
64
+
65
+ **Note**: Replace `YOUR_SQUARE_APPLICATION_ID` with your actual Square Application ID.
66
+
67
+ 2. Register the custom `Application` class in your `AndroidManifest.xml`:
68
+
69
+ ```xml
70
+ <application
71
+ android:name=".MainApplication"
72
+ ...>
73
+ ```
74
+
43
75
  #### Variables
44
76
 
45
77
  If needed, you can define the following project variable in your app's `variables.gradle` file to change the default version of the dependency:
@@ -31,7 +31,7 @@ android {
31
31
  buildTypes {
32
32
  release {
33
33
  minifyEnabled false
34
- proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
34
+ proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
35
35
  }
36
36
  }
37
37
  lintOptions {
@@ -1,6 +1,7 @@
1
1
  package io.capawesome.capacitorjs.plugins.squaremobilepayments;
2
2
 
3
- import android.app.Application;
3
+ import android.os.Handler;
4
+ import android.os.Looper;
4
5
  import androidx.annotation.NonNull;
5
6
  import androidx.annotation.Nullable;
6
7
  import com.getcapacitor.JSObject;
@@ -43,9 +44,15 @@ public class SquareMobilePayments {
43
44
  @NonNull
44
45
  private final SquareMobilePaymentsPlugin plugin;
45
46
 
47
+ @NonNull
48
+ private final Handler mainHandler = new Handler(Looper.getMainLooper());
49
+
46
50
  private boolean isInitialized = false;
47
51
  private boolean isAuthorized = false;
48
52
 
53
+ @Nullable
54
+ private String locationId;
55
+
49
56
  @Nullable
50
57
  private Object pairingHandle;
51
58
 
@@ -58,18 +65,30 @@ public class SquareMobilePayments {
58
65
  @Nullable
59
66
  private CallbackReference availableCardInputMethodsCallbackReference;
60
67
 
68
+ @Nullable
69
+ private CallbackReference authorizationCallbackReference;
70
+
61
71
  public SquareMobilePayments(@NonNull SquareMobilePaymentsPlugin plugin) {
62
72
  this.plugin = plugin;
63
73
  }
64
74
 
65
- private String locationId;
66
-
67
75
  public void initialize(@NonNull InitializeOptions options, @NonNull EmptyCallback callback) {
68
76
  try {
69
77
  this.locationId = options.getLocationId();
70
- Application application = (Application) plugin.getContext().getApplicationContext();
71
78
 
72
- MobilePaymentsSdk.initialize(locationId, application);
79
+ // SDK must already be initialized in Application.onCreate() with the Square Application ID.
80
+ // Verify the SDK is ready by attempting to access the settings manager.
81
+ try {
82
+ MobilePaymentsSdk.settingsManager();
83
+ } catch (Exception e) {
84
+ callback.error(
85
+ new Exception(
86
+ "Square SDK not initialized. Make sure to call MobilePaymentsSdk.initialize() in your Application.onCreate().",
87
+ e
88
+ )
89
+ );
90
+ return;
91
+ }
73
92
 
74
93
  isInitialized = true;
75
94
  callback.success();
@@ -86,13 +105,33 @@ public class SquareMobilePayments {
86
105
 
87
106
  String accessToken = options.getAccessToken();
88
107
 
89
- AuthorizationManager authManager = MobilePaymentsSdk.authorizationManager();
90
- authManager.authorize(accessToken, locationId, result -> {
91
- if (result.isSuccess()) {
92
- isAuthorized = true;
93
- callback.success();
94
- } else {
95
- callback.error(new Exception(result.errorMessage()));
108
+ mainHandler.post(() -> {
109
+ try {
110
+ AuthorizationManager authManager = MobilePaymentsSdk.authorizationManager();
111
+
112
+ authorizationCallbackReference = authManager.authorize(accessToken, locationId, result -> {
113
+ if (result.isSuccess()) {
114
+ isAuthorized = true;
115
+ callback.success();
116
+ } else {
117
+ isAuthorized = false;
118
+ String errorMsg = result.errorMessage();
119
+ if (errorMsg == null || errorMsg.isEmpty()) {
120
+ errorMsg = "Authorization failed";
121
+ }
122
+ if (result.errorCode() != null) {
123
+ errorMsg = errorMsg + " (Error code: " + result.errorCode().toString() + ")";
124
+ }
125
+ callback.error(new Exception(errorMsg));
126
+ }
127
+ if (authorizationCallbackReference != null) {
128
+ authorizationCallbackReference.clear();
129
+ authorizationCallbackReference = null;
130
+ }
131
+ });
132
+ } catch (Exception exception) {
133
+ isAuthorized = false;
134
+ callback.error(exception);
96
135
  }
97
136
  });
98
137
  } catch (Exception exception) {
@@ -107,9 +146,11 @@ public class SquareMobilePayments {
107
146
  }
108
147
 
109
148
  AuthorizationManager authManager = MobilePaymentsSdk.authorizationManager();
110
- boolean authorized = authManager.getAuthorizationState().isAuthorized();
149
+ boolean sdkAuthorized = authManager.getAuthorizationState().isAuthorized();
111
150
 
112
- IsAuthorizedResult result = new IsAuthorizedResult(authorized);
151
+ isAuthorized = sdkAuthorized;
152
+
153
+ IsAuthorizedResult result = new IsAuthorizedResult(sdkAuthorized);
113
154
  callback.success(result);
114
155
  } catch (Exception exception) {
115
156
  callback.error(exception);
@@ -122,10 +163,16 @@ public class SquareMobilePayments {
122
163
  throw CustomExceptions.NOT_INITIALIZED;
123
164
  }
124
165
 
125
- AuthorizationManager authManager = MobilePaymentsSdk.authorizationManager();
126
- authManager.deauthorize();
127
- isAuthorized = false;
128
- callback.success();
166
+ mainHandler.post(() -> {
167
+ try {
168
+ AuthorizationManager authManager = MobilePaymentsSdk.authorizationManager();
169
+ authManager.deauthorize();
170
+ isAuthorized = false;
171
+ callback.success();
172
+ } catch (Exception exception) {
173
+ callback.error(exception);
174
+ }
175
+ });
129
176
  } catch (Exception exception) {
130
177
  callback.error(exception);
131
178
  }
@@ -140,14 +187,20 @@ public class SquareMobilePayments {
140
187
  throw CustomExceptions.NOT_AUTHORIZED;
141
188
  }
142
189
 
143
- SettingsManager settingsManager = MobilePaymentsSdk.settingsManager();
144
- settingsManager.showSettings(result -> {
145
- if (result.isSuccess()) {
146
- callback.success();
147
- } else {
148
- callback.error(new Exception(result.errorMessage()));
190
+ mainHandler.post(() -> {
191
+ try {
192
+ SettingsManager settingsManager = MobilePaymentsSdk.settingsManager();
193
+ settingsManager.showSettings(result -> {
194
+ if (result.isSuccess()) {
195
+ callback.success();
196
+ } else {
197
+ callback.error(new Exception(result.errorMessage()));
198
+ }
199
+ return null;
200
+ });
201
+ } catch (Exception exception) {
202
+ callback.error(exception);
149
203
  }
150
- return null;
151
204
  });
152
205
  } catch (Exception exception) {
153
206
  callback.error(exception);
@@ -182,31 +235,34 @@ public class SquareMobilePayments {
182
235
  throw CustomExceptions.NOT_AUTHORIZED;
183
236
  }
184
237
 
185
- ReaderManager readerManager = MobilePaymentsSdk.readerManager();
186
-
187
- if (readerManager.isPairingInProgress()) {
188
- throw CustomExceptions.PAIRING_ALREADY_IN_PROGRESS;
189
- }
190
-
191
- // Notify that pairing has begun
192
- plugin.notifyReaderPairingDidBeginListeners();
238
+ mainHandler.post(() -> {
239
+ try {
240
+ ReaderManager readerManager = MobilePaymentsSdk.readerManager();
241
+
242
+ if (readerManager.isPairingInProgress()) {
243
+ throw CustomExceptions.PAIRING_ALREADY_IN_PROGRESS;
244
+ }
245
+
246
+ plugin.notifyReaderPairingDidBeginListeners();
247
+
248
+ pairingHandle = readerManager.pairReader(result -> {
249
+ if (result.isSuccess()) {
250
+ plugin.notifyReaderPairingDidSucceedListeners();
251
+ } else {
252
+ ReaderPairingDidFailEvent event = new ReaderPairingDidFailEvent(
253
+ result.errorCode() != null ? result.errorCode().toString() : null,
254
+ result.errorMessage()
255
+ );
256
+ plugin.notifyReaderPairingDidFailListeners(event);
257
+ }
258
+ pairingHandle = null;
259
+ });
193
260
 
194
- pairingHandle = readerManager.pairReader(result -> {
195
- if (result.isSuccess()) {
196
- // Notify that pairing succeeded
197
- plugin.notifyReaderPairingDidSucceedListeners();
198
- } else {
199
- // Notify that pairing failed
200
- ReaderPairingDidFailEvent event = new ReaderPairingDidFailEvent(
201
- result.errorCode() != null ? result.errorCode().toString() : null,
202
- result.errorMessage()
203
- );
204
- plugin.notifyReaderPairingDidFailListeners(event);
261
+ callback.success();
262
+ } catch (Exception exception) {
263
+ callback.error(exception);
205
264
  }
206
- pairingHandle = null;
207
265
  });
208
-
209
- callback.success();
210
266
  } catch (Exception exception) {
211
267
  callback.error(exception);
212
268
  }
@@ -335,24 +391,20 @@ public class SquareMobilePayments {
335
391
 
336
392
  PaymentManager paymentManager = MobilePaymentsSdk.paymentManager();
337
393
 
338
- // Build Money object
339
394
  Money amountMoney = new Money(params.getAmountMoney().getAmount(), CurrencyCode.valueOf(params.getAmountMoney().getCurrency()));
340
395
 
341
- // Determine processing mode
342
396
  ProcessingMode processingMode = ProcessingMode.AUTO_DETECT;
343
397
  if (params.getProcessingMode() != null) {
344
398
  processingMode = ProcessingMode.valueOf(params.getProcessingMode());
345
399
  }
346
400
 
347
- // Determine autocomplete
348
401
  boolean autocomplete = params.getAutocomplete() != null ? params.getAutocomplete() : true;
349
402
 
350
- // Build PaymentParameters
351
403
  PaymentParameters.Builder paramsBuilder = new PaymentParameters.Builder(
352
404
  amountMoney,
353
405
  params.getPaymentAttemptId(),
354
406
  processingMode,
355
- false // allowCardSurcharge - not supported yet
407
+ false
356
408
  );
357
409
 
358
410
  paramsBuilder.autocomplete(autocomplete);
@@ -393,7 +445,6 @@ public class SquareMobilePayments {
393
445
 
394
446
  PaymentParameters sdkPaymentParams = paramsBuilder.build();
395
447
 
396
- // Build PromptParameters
397
448
  PromptMode promptMode = PromptMode.DEFAULT;
398
449
  if (promptParams.getMode() != null) {
399
450
  promptMode = PromptMode.valueOf(promptParams.getMode());
@@ -406,28 +457,32 @@ public class SquareMobilePayments {
406
457
 
407
458
  PromptParameters sdkPromptParams = new PromptParameters(promptMode, additionalMethods);
408
459
 
409
- // Start payment
410
- paymentHandle = paymentManager.startPaymentActivity(sdkPaymentParams, sdkPromptParams, result -> {
411
- if (result.isSuccess()) {
412
- Payment sdkPayment = result.value();
413
- io.capawesome.capacitorjs.plugins.squaremobilepayments.classes.results.Payment payment = convertSdkPaymentToPayment(
414
- sdkPayment
415
- );
460
+ mainHandler.post(() -> {
461
+ try {
462
+ paymentHandle = paymentManager.startPaymentActivity(sdkPaymentParams, sdkPromptParams, result -> {
463
+ if (result.isSuccess()) {
464
+ Payment sdkPayment = result.value();
465
+ io.capawesome.capacitorjs.plugins.squaremobilepayments.classes.results.Payment payment =
466
+ convertSdkPaymentToPayment(sdkPayment);
467
+
468
+ PaymentDidFinishEvent event = new PaymentDidFinishEvent(payment);
469
+ plugin.notifyPaymentDidFinishListeners(event);
470
+ } else {
471
+ PaymentDidFailEvent event = new PaymentDidFailEvent(
472
+ null,
473
+ result.errorCode() != null ? result.errorCode().toString() : null,
474
+ result.errorMessage()
475
+ );
476
+ plugin.notifyPaymentDidFailListeners(event);
477
+ }
478
+ paymentHandle = null;
479
+ });
416
480
 
417
- PaymentDidFinishEvent event = new PaymentDidFinishEvent(payment);
418
- plugin.notifyPaymentDidFinishListeners(event);
419
- } else {
420
- PaymentDidFailEvent event = new PaymentDidFailEvent(
421
- null,
422
- result.errorCode() != null ? result.errorCode().toString() : null,
423
- result.errorMessage()
424
- );
425
- plugin.notifyPaymentDidFailListeners(event);
481
+ callback.success();
482
+ } catch (Exception exception) {
483
+ callback.error(exception);
426
484
  }
427
- paymentHandle = null;
428
485
  });
429
-
430
- callback.success();
431
486
  } catch (Exception exception) {
432
487
  callback.error(exception);
433
488
  }
@@ -439,17 +494,23 @@ public class SquareMobilePayments {
439
494
  throw CustomExceptions.NOT_INITIALIZED;
440
495
  }
441
496
 
442
- if (paymentHandle != null) {
443
- paymentHandle.cancel();
444
- paymentHandle = null;
497
+ mainHandler.post(() -> {
498
+ try {
499
+ if (paymentHandle != null) {
500
+ paymentHandle.cancel();
501
+ paymentHandle = null;
445
502
 
446
- PaymentDidCancelEvent event = new PaymentDidCancelEvent(null);
447
- plugin.notifyPaymentDidCancelListeners(event);
448
- } else {
449
- throw CustomExceptions.NO_PAYMENT_IN_PROGRESS;
450
- }
503
+ PaymentDidCancelEvent event = new PaymentDidCancelEvent(null);
504
+ plugin.notifyPaymentDidCancelListeners(event);
505
+ } else {
506
+ throw CustomExceptions.NO_PAYMENT_IN_PROGRESS;
507
+ }
451
508
 
452
- callback.success();
509
+ callback.success();
510
+ } catch (Exception exception) {
511
+ callback.error(exception);
512
+ }
513
+ });
453
514
  } catch (Exception exception) {
454
515
  callback.error(exception);
455
516
  }
@@ -483,15 +544,21 @@ public class SquareMobilePayments {
483
544
  throw CustomExceptions.NOT_AUTHORIZED;
484
545
  }
485
546
 
486
- // Show MockReader UI - only available in Debug builds with mockreader-ui dependency
487
- // Use reflection to avoid compile-time dependency
488
- Class<?> mockReaderUIClass = Class.forName("com.squareup.sdk.mockreader.ui.MockReaderUI");
489
- mockReaderUIClass.getMethod("show").invoke(null);
490
- callback.success();
491
- } catch (ClassNotFoundException e) {
492
- callback.error(
493
- new Exception("MockReaderUI is only available in Debug builds. Please ensure you're using a Debug build configuration.")
494
- );
547
+ mainHandler.post(() -> {
548
+ try {
549
+ Class<?> mockReaderUIClass = Class.forName("com.squareup.sdk.mockreader.ui.MockReaderUI");
550
+ mockReaderUIClass.getMethod("show").invoke(null);
551
+ callback.success();
552
+ } catch (ClassNotFoundException e) {
553
+ callback.error(
554
+ new Exception(
555
+ "MockReaderUI is only available in Debug builds. Please ensure you're using a Debug build configuration."
556
+ )
557
+ );
558
+ } catch (Exception exception) {
559
+ callback.error(exception);
560
+ }
561
+ });
495
562
  } catch (Exception exception) {
496
563
  callback.error(exception);
497
564
  }
@@ -499,15 +566,21 @@ public class SquareMobilePayments {
499
566
 
500
567
  public void hideMockReader(@NonNull EmptyCallback callback) {
501
568
  try {
502
- // Hide MockReader UI
503
- // Use reflection to avoid compile-time dependency
504
- Class<?> mockReaderUIClass = Class.forName("com.squareup.sdk.mockreader.ui.MockReaderUI");
505
- mockReaderUIClass.getMethod("hide").invoke(null);
506
- callback.success();
507
- } catch (ClassNotFoundException e) {
508
- callback.error(
509
- new Exception("MockReaderUI is only available in Debug builds. Please ensure you're using a Debug build configuration.")
510
- );
569
+ mainHandler.post(() -> {
570
+ try {
571
+ Class<?> mockReaderUIClass = Class.forName("com.squareup.sdk.mockreader.ui.MockReaderUI");
572
+ mockReaderUIClass.getMethod("hide").invoke(null);
573
+ callback.success();
574
+ } catch (ClassNotFoundException e) {
575
+ callback.error(
576
+ new Exception(
577
+ "MockReaderUI is only available in Debug builds. Please ensure you're using a Debug build configuration."
578
+ )
579
+ );
580
+ } catch (Exception exception) {
581
+ callback.error(exception);
582
+ }
583
+ });
511
584
  } catch (Exception exception) {
512
585
  callback.error(exception);
513
586
  }
@@ -528,7 +601,6 @@ public class SquareMobilePayments {
528
601
  sdkReader
529
602
  );
530
603
 
531
- // Notify based on change type
532
604
  if (change == ReaderChangedEvent.Change.ADDED) {
533
605
  ReaderWasAddedEvent addedEvent = new ReaderWasAddedEvent(reader);
534
606
  plugin.notifyReaderWasAddedListeners(addedEvent);
@@ -537,7 +609,6 @@ public class SquareMobilePayments {
537
609
  plugin.notifyReaderWasRemovedListeners(removedEvent);
538
610
  }
539
611
 
540
- // Also notify general reader changed event
541
612
  ReaderDidChangeEvent didChangeEvent = new ReaderDidChangeEvent(reader, convertReaderChangeToString(change));
542
613
  plugin.notifyReaderDidChangeListeners(didChangeEvent);
543
614
  });
@@ -572,8 +643,6 @@ public class SquareMobilePayments {
572
643
  }
573
644
  }
574
645
 
575
- // Helper methods for converting SDK types to plugin types
576
-
577
646
  @Nullable
578
647
  private com.squareup.sdk.mobilepayments.cardreader.ReaderInfo findReaderBySerialNumber(@NonNull String serialNumber) {
579
648
  ReaderManager readerManager = MobilePaymentsSdk.readerManager();
@@ -596,8 +665,14 @@ public class SquareMobilePayments {
596
665
  String model = convertReaderModelToString(sdkReader.getModel());
597
666
  String status = convertReaderStatusToString(sdkReader.getStatus());
598
667
  String firmwareVersion = sdkReader.getFirmwareVersion();
599
- Integer batteryLevel = sdkReader.getBatteryStatus().getPercent();
600
- Boolean isCharging = sdkReader.getBatteryStatus().isCharging();
668
+
669
+ Integer batteryLevel = null;
670
+ Boolean isCharging = null;
671
+ com.squareup.sdk.mobilepayments.cardreader.ReaderInfo.BatteryStatus batteryStatus = sdkReader.getBatteryStatus();
672
+ if (batteryStatus != null) {
673
+ batteryLevel = batteryStatus.getPercent();
674
+ isCharging = batteryStatus.isCharging();
675
+ }
601
676
 
602
677
  List<String> supportedCardInputMethods = sdkReader
603
678
  .getSupportedCardEntryMethods()
@@ -638,7 +713,6 @@ public class SquareMobilePayments {
638
713
  type = "ONLINE";
639
714
  status = convertPaymentStatusToString(onlinePayment.getStatus());
640
715
 
641
- // Extract card details
642
716
  com.squareup.sdk.mobilepayments.payment.CardPaymentDetails sdkCardDetails = onlinePayment.getCardDetails();
643
717
  if (sdkCardDetails != null) {
644
718
  cardDetails = convertSdkCardDetailsToCardPaymentDetails(sdkCardDetails);
@@ -649,7 +723,6 @@ public class SquareMobilePayments {
649
723
  type = "OFFLINE";
650
724
  status = "PENDING";
651
725
 
652
- // Extract card details
653
726
  com.squareup.sdk.mobilepayments.payment.CardPaymentDetails sdkCardDetails = offlinePayment.getCardDetails();
654
727
  if (sdkCardDetails != null) {
655
728
  cardDetails = convertSdkCardDetailsToCardPaymentDetails(sdkCardDetails);
@@ -770,7 +843,6 @@ public class SquareMobilePayments {
770
843
  } else if (change == ReaderChangedEvent.Change.REMOVED) {
771
844
  return "REMOVED";
772
845
  }
773
- // Default for any other status changes
774
846
  return "STATUS_DID_CHANGE";
775
847
  }
776
848
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capawesome/capacitor-square-mobile-payments",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Unofficial Capacitor plugin for Square Mobile Payments SDK.",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",
@@ -63,7 +63,6 @@
63
63
  "@capacitor/docgen": "0.3.1",
64
64
  "@capacitor/ios": "8.0.0",
65
65
  "@ionic/eslint-config": "0.4.0",
66
- "@ionic/swiftlint-config": "2.0.0",
67
66
  "eslint": "8.57.0",
68
67
  "prettier": "3.4.2",
69
68
  "prettier-plugin-java": "2.6.7",
@@ -75,7 +74,6 @@
75
74
  "peerDependencies": {
76
75
  "@capacitor/core": ">=8.0.0"
77
76
  },
78
- "swiftlint": "@ionic/swiftlint-config",
79
77
  "eslintConfig": {
80
78
  "extends": "@ionic/eslint-config/recommended"
81
79
  },