react-native-mesmart 0.5.0 → 0.6.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 (38) hide show
  1. package/README.md +39 -56
  2. package/android/build.gradle +4 -3
  3. package/android/libs/NeptuneLiteApi_V4.08.00_20240701.jar +0 -0
  4. package/android/src/main/AndroidManifestNew.xml +0 -2
  5. package/android/src/main/java/com/mesmart/Packages.kt +3 -1
  6. package/android/src/main/java/com/{sunmi → mesmart}/utils/ESCUtil.kt +1 -1
  7. package/android/src/main/java/com/{sunmi/utils/SunmiUtils.kt → mesmart/utils/Utils.kt} +7 -3
  8. package/android/src/main/java/com/pax/PaxImpl.kt +102 -0
  9. package/android/src/main/java/com/pax/PaxModule.kt +62 -0
  10. package/android/src/main/java/com/pax/utils/PaxConverter.kt +505 -0
  11. package/android/src/main/java/com/plugpag/PlugpagModule.kt +5 -5
  12. package/android/src/main/java/com/sunmi/SunmiImpl.kt +2 -2
  13. package/android/src/main/java/com/sunmi/SunmiModule.kt +8 -8
  14. package/android/src/main/java/com/sunmi/{utils → helpers}/SunmiPrintHelper.kt +2 -1
  15. package/android/src/main/java/com/sunmi/{utils → helpers}/SunmiRfidHelper.kt +4 -3
  16. package/lib/commonjs/@types/index.js +11 -0
  17. package/lib/commonjs/@types/index.js.map +1 -1
  18. package/lib/commonjs/@types/pax.js +2 -0
  19. package/lib/commonjs/@types/pax.js.map +1 -0
  20. package/lib/commonjs/index.js +29 -30
  21. package/lib/commonjs/index.js.map +1 -1
  22. package/lib/module/@types/index.js +1 -0
  23. package/lib/module/@types/index.js.map +1 -1
  24. package/lib/module/@types/pax.js +2 -0
  25. package/lib/module/@types/pax.js.map +1 -0
  26. package/lib/module/index.js +29 -31
  27. package/lib/module/index.js.map +1 -1
  28. package/lib/typescript/src/@types/index.d.ts +1 -0
  29. package/lib/typescript/src/@types/index.d.ts.map +1 -1
  30. package/lib/typescript/src/@types/pax.d.ts +7 -0
  31. package/lib/typescript/src/@types/pax.d.ts.map +1 -0
  32. package/lib/typescript/src/index.d.ts +7 -1
  33. package/lib/typescript/src/index.d.ts.map +1 -1
  34. package/package.json +1 -1
  35. package/src/@types/index.ts +1 -0
  36. package/src/@types/pax.ts +6 -0
  37. package/src/index.ts +90 -0
  38. package/src/index.tsx +0 -85
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # react-native-mesmart
2
2
 
3
- Sunmi RFID reader and Plugpag wrapper for React Native
3
+ Native methods to integrate with Sunmi P2, Pax A910 and Plugpag payment SDK
4
4
 
5
5
  ## Installation
6
6
 
@@ -14,7 +14,7 @@ OR
14
14
  yarn add react-native-mesmart
15
15
  ```
16
16
 
17
- Now, you just need to add that code line below in your build.gradle in your react native application
17
+ Add those lines below in your android/build.gradle
18
18
 
19
19
  ```
20
20
  allprojects {
@@ -27,75 +27,58 @@ allprojects {
27
27
  }
28
28
  ```
29
29
 
30
+ In your android/app/build.gradle, you must add those configs below
31
+
32
+ ```
33
+ android {
34
+ ...
35
+ defaultConfig {
36
+ ...
37
+ ndk {
38
+ abiFilters "armeabi-v7a"
39
+ }
40
+ }
41
+ ...
42
+ packagingOptions {
43
+ jniLibs {
44
+ useLegacyPackaging true
45
+ }
46
+ }
47
+ }
48
+ ```
49
+
50
+ Create a new directory at android/app/src/main/jniLibs/armeabi-v7a and add the file 'libDeviceConfig.so' to this new folder
51
+
52
+ ## Permissions
53
+
54
+ Add those permissions in your AndroidManifest.xml
55
+
56
+ ```
57
+ <uses-permission android:name="com.sunmi.perm.CONTACTLESS_CARD" />
58
+ <uses-permission android:name="br.com.uol.pagseguro.permission.MANAGE_PAYMENTS" />
59
+ <uses-permission android:name="com.pax.permission.PICC" />
60
+ <uses-permission android:name="com.pax.permission.PRINTER" />
61
+ ```
62
+
30
63
  ## Usage
31
64
 
32
65
  ```ts
33
- import {
34
- readRfidCardInSunmi,
35
- cancelRfidCardSearchInSunmi,
36
- initPlugpag,
37
- destroyPlugpag,
38
- abortPayment,
39
- printEstablishmentReceipt,
40
- printClientReceipt,
41
- customDialogClientViaPrinter,
42
- pay,
43
- resolveTransactionEvent,
44
- } from 'react-native-mesmart';
45
-
46
- // ...
47
-
48
- // Read rfid asynchronously
49
- const rfid: string = await readRfidCardInSunmi();
50
-
51
- // Cancel rfid read
52
- cancelRfidCardSearchInSunmi();
53
-
54
- // Init PlugPag wrapper
55
- const activationCode: string = '9999999'
56
- initPlugpag(activationCode)
57
-
58
- // Destroy PlugPag instance
59
- destroyPlugpag()
60
-
61
- // Abort payment process
62
- abortPayment()
63
-
64
- // Print establishment receipt
65
- printEstablishmentReceipt()
66
-
67
- // Print client receipt
68
- printClientReceipt()
69
-
70
- // Custom client modal to print receipt
71
- const color: string = "#000"
72
- customDialogClientViaPrinter(color)
73
-
74
- // Make transaction process with PlugPag wrapper
75
- const value: string = "2000" // In cents, 2000 means R$ 20,00
76
- const paymentMethod: TransactionTypes = "creditCard" // TransactionTypes = 'debitCard' | 'creditCard' | 'pix' | 'voucher'
77
- const result = await pay(value, paymentMethod)
78
-
79
- // Return a message based on a PlugPagEvent
80
- const plugpagEvent: PlugPagEventData = {
81
- customMessage: 'customMessage'
82
- eventCode: 0 // PlugPagEvent
83
- }
84
- const message: string = resolveTransactionEvent(plugpagEvent)
66
+ import { Sunmi, Plugpag, Pax } from 'react-native-mesmart';
85
67
 
86
68
  // Register event listener to catch onPlugPagEventData
87
69
  useEffect(() => {
88
70
  const eventEmitter = new NativeEventEmitter(NativeModules.Plugpag);
89
71
  let eventListener = eventEmitter.addListener(
90
72
  'onPlugPagEventData',
91
- (plugpagEvent: PlugPagEventData) => { console.log(plugpagEvent) },
73
+ (plugpagEvent: PlugPagEventData) => {
74
+ console.log(plugpagEvent);
75
+ }
92
76
  );
93
77
 
94
78
  return () => {
95
79
  eventListener.remove();
96
80
  };
97
81
  }, []);
98
-
99
82
  ```
100
83
 
101
84
  ## Contributing
@@ -80,14 +80,15 @@ android {
80
80
  repositories {
81
81
  mavenCentral()
82
82
  google()
83
- // if you open in Android Studio, uncomment the line below
84
- // but leave it commented cause it bugs the main project build
85
- // maven { url 'https://github.com/pagseguro/PlugPagServiceWrapper/raw/master' }
83
+ // if you open in Android Studio uncomment the line below to code using Plugpag library
84
+ // but leave it commented cause it needs to be imported in the main project
85
+ // maven { url 'https://github.com/pagseguro/PlugPagServiceWrapper/raw/master' }
86
86
  }
87
87
 
88
88
  def kotlin_version = getExtOrDefault("kotlinVersion")
89
89
 
90
90
  dependencies {
91
+ implementation fileTree(dir: 'libs', include: ['*.jar'])
91
92
  implementation "com.facebook.react:react-android:0.75.0-rc.5"
92
93
  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
93
94
  implementation "com.sunmi:PayLib-release:2.0.07"
@@ -1,4 +1,2 @@
1
1
  <manifest xmlns:android="http://schemas.android.com/apk/res/android">
2
- <uses-permission android:name="com.sunmi.perm.CONTACTLESS_CARD" />
3
- <uses-permission android:name="br.com.uol.pagseguro.permission.MANAGE_PAYMENTS"/>
4
2
  </manifest>
@@ -4,6 +4,7 @@ import com.facebook.react.ReactPackage
4
4
  import com.facebook.react.bridge.NativeModule
5
5
  import com.facebook.react.bridge.ReactApplicationContext
6
6
  import com.facebook.react.uimanager.ViewManager
7
+ import com.pax.PaxModule
7
8
  import com.plugpag.PlugpagModule
8
9
  import com.sunmi.SunmiModule
9
10
 
@@ -11,7 +12,8 @@ class Packages : ReactPackage {
11
12
  override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
12
13
  return listOf(
13
14
  PlugpagModule(reactContext),
14
- SunmiModule(reactContext)
15
+ SunmiModule(reactContext),
16
+ PaxModule(reactContext)
15
17
  )
16
18
  }
17
19
 
@@ -1,4 +1,4 @@
1
- package com.sunmi.utils
1
+ package com.mesmart.utils
2
2
 
3
3
  object ESCUtil {
4
4
  const val ESC: Byte = 0x1B // Escape
@@ -1,14 +1,18 @@
1
- package com.sunmi.utils
1
+ package com.mesmart.utils
2
2
 
3
3
  import android.os.Build
4
4
 
5
- class SunmiUtils {
5
+ class Utils {
6
6
  companion object {
7
7
  fun isSunmiP2(): Boolean {
8
8
  return Build.MODEL.equals("P2-B", true)
9
9
  }
10
10
 
11
- fun resolveRFIDBySerialNumber(value: String): String {
11
+ fun isA910(): Boolean {
12
+ return Build.MODEL.equals("A910", true)
13
+ }
14
+
15
+ fun resolveRFID(value: String): String {
12
16
  val length = value.length
13
17
 
14
18
  val n = StringBuilder()
@@ -0,0 +1,102 @@
1
+ package com.pax
2
+
3
+ import android.graphics.Bitmap
4
+ import android.graphics.BitmapFactory
5
+ import android.os.Handler
6
+ import android.os.Looper
7
+ import com.facebook.react.bridge.Promise
8
+ import com.facebook.react.bridge.ReactApplicationContext
9
+ import com.mesmart.R
10
+ import com.mesmart.utils.Utils
11
+ import com.pax.dal.IPicc
12
+ import com.pax.dal.IPrinter
13
+ import com.pax.dal.entity.EDetectMode
14
+ import com.pax.dal.entity.EFontTypeAscii
15
+ import com.pax.dal.entity.EFontTypeExtCode
16
+ import com.pax.dal.entity.EPiccType
17
+ import com.pax.dal.entity.PiccCardInfo
18
+ import com.pax.neptunelite.api.NeptuneLiteUser
19
+ import com.pax.utils.PaxConverter
20
+
21
+ class PaxImpl(
22
+ private val reactContext: ReactApplicationContext
23
+ ) {
24
+ private val picc: IPicc = NeptuneLiteUser
25
+ .getInstance()
26
+ .getDal(reactContext)
27
+ .getPicc(EPiccType.INTERNAL)
28
+ private val printer: IPrinter = NeptuneLiteUser
29
+ .getInstance()
30
+ .getDal(reactContext)
31
+ .printer
32
+ private val handler = Handler(Looper.getMainLooper())
33
+ private var isSearchingCard = false
34
+
35
+ fun searchRfid(promise: Promise) {
36
+ isSearchingCard = true
37
+ val timeoutRunnable = Runnable {
38
+ if (isSearchingCard) {
39
+ isSearchingCard = false
40
+ promise.reject("TimeoutException", "Card not found within 30 seconds!")
41
+ }
42
+ }
43
+
44
+ handler.postDelayed(timeoutRunnable, 30000)
45
+
46
+ Thread {
47
+ while (isSearchingCard) {
48
+ picc.open()
49
+ val cardInfo: PiccCardInfo? = picc.detect(EDetectMode.ONLY_M)
50
+ if (cardInfo != null) {
51
+ handler.removeCallbacks(timeoutRunnable)
52
+ isSearchingCard = false
53
+ promise.resolve(
54
+ PaxConverter.instance?.bcdToStr(
55
+ if ((cardInfo.serialInfo == null)) "".toByteArray()
56
+ else cardInfo.serialInfo
57
+ )?.let {
58
+ Utils.resolveRFID(it)
59
+ }
60
+ )
61
+ return@Thread
62
+ }
63
+ picc.close()
64
+ Thread.sleep(100)
65
+ }
66
+ }.start()
67
+ }
68
+
69
+ fun cancelCardSearch() {
70
+ isSearchingCard = false
71
+ handler.removeCallbacksAndMessages(null)
72
+ }
73
+
74
+ fun printText(
75
+ str: String,
76
+ fontSize: Int
77
+ ) {
78
+ printer.init()
79
+ printer.spaceSet(0, 0)
80
+ if (fontSize == 20) printer.fontSet(EFontTypeAscii.FONT_8_32, EFontTypeExtCode.FONT_16_16)
81
+ else printer.fontSet(EFontTypeAscii.FONT_12_24, EFontTypeExtCode.FONT_16_16)
82
+ printer.printStr(str, null)
83
+ printer.start()
84
+ }
85
+
86
+ fun printBpLogo() {
87
+ printer.init()
88
+ val options = BitmapFactory.Options().apply {
89
+ inTargetDensity = 160
90
+ inDensity = 160
91
+ }
92
+ val bitmap = BitmapFactory.decodeResource(
93
+ reactContext.resources,
94
+ R.drawable.logo_black,
95
+ options
96
+ )
97
+ printer.printBitmap(
98
+ Bitmap.createScaledBitmap(bitmap, 375, 100, true)
99
+ )
100
+ printer.start()
101
+ }
102
+ }
@@ -0,0 +1,62 @@
1
+ package com.pax
2
+
3
+ import com.facebook.react.bridge.Promise
4
+ import com.facebook.react.bridge.ReactApplicationContext
5
+ import com.facebook.react.bridge.ReactContextBaseJavaModule
6
+ import com.facebook.react.bridge.ReactMethod
7
+ import com.mesmart.utils.Utils
8
+ import com.pax.utils.PaxConverter
9
+
10
+ class PaxModule(
11
+ reactContext: ReactApplicationContext
12
+ ) : ReactContextBaseJavaModule(reactContext) {
13
+ var pax: PaxImpl? = null
14
+
15
+ init {
16
+ if (Utils.isA910()) {
17
+ pax = PaxImpl(reactContext)
18
+ PaxConverter.instance?.setContext(reactContext)
19
+ }
20
+ }
21
+
22
+ override fun getName(): String {
23
+ return "Pax"
24
+ }
25
+
26
+ private fun checkPax() {
27
+ if (pax == null)
28
+ throw Exception("Pax is null! You may not working in a Pax A910 or an error ocurred during the initialization!")
29
+ }
30
+
31
+ @ReactMethod
32
+ fun searchRfid(promise: Promise) {
33
+ try {
34
+ checkPax()
35
+ pax?.searchRfid(promise)
36
+ } catch (e: Exception) {
37
+ e.printStackTrace()
38
+ promise.reject(e)
39
+ }
40
+ }
41
+
42
+ @ReactMethod(isBlockingSynchronousMethod = true)
43
+ fun cancelCardSearch() {
44
+ checkPax()
45
+ pax?.cancelCardSearch()
46
+ }
47
+
48
+ @ReactMethod(isBlockingSynchronousMethod = true)
49
+ fun printText(
50
+ str: String,
51
+ fontSize: Int
52
+ ) {
53
+ checkPax()
54
+ pax?.printText(str, fontSize)
55
+ }
56
+
57
+ @ReactMethod(isBlockingSynchronousMethod = true)
58
+ fun printBpLogo() {
59
+ checkPax()
60
+ pax?.printBpLogo()
61
+ }
62
+ }