react-native-mesmart 0.4.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 (53) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +111 -0
  3. package/android/build.gradle +96 -0
  4. package/android/gradle.properties +19 -0
  5. package/android/src/main/AndroidManifest.xml +5 -0
  6. package/android/src/main/AndroidManifestNew.xml +4 -0
  7. package/android/src/main/java/com/mesmart/Packages.kt +21 -0
  8. package/android/src/main/java/com/plugpag/PlugpagImpl.kt +112 -0
  9. package/android/src/main/java/com/plugpag/PlugpagModule.kt +77 -0
  10. package/android/src/main/java/com/plugpag/utils/PlugpagUtils.kt +40 -0
  11. package/android/src/main/java/com/sunmi/SunmiImpl.kt +75 -0
  12. package/android/src/main/java/com/sunmi/SunmiModule.kt +75 -0
  13. package/android/src/main/java/com/sunmi/utils/ESCUtil.kt +155 -0
  14. package/android/src/main/java/com/sunmi/utils/SunmiPrintHelper.kt +148 -0
  15. package/android/src/main/java/com/sunmi/utils/SunmiRfidHelper.kt +68 -0
  16. package/android/src/main/java/com/sunmi/utils/SunmiUtils.kt +61 -0
  17. package/android/src/main/res/drawable/logo_black.png +0 -0
  18. package/lib/commonjs/@types/index.js +28 -0
  19. package/lib/commonjs/@types/index.js.map +1 -0
  20. package/lib/commonjs/@types/plugpag.js +36 -0
  21. package/lib/commonjs/@types/plugpag.js.map +1 -0
  22. package/lib/commonjs/@types/sunmi.js +2 -0
  23. package/lib/commonjs/@types/sunmi.js.map +1 -0
  24. package/lib/commonjs/errorMessages.js +9 -0
  25. package/lib/commonjs/errorMessages.js.map +1 -0
  26. package/lib/commonjs/index.js +67 -0
  27. package/lib/commonjs/index.js.map +1 -0
  28. package/lib/module/@types/index.js +3 -0
  29. package/lib/module/@types/index.js.map +1 -0
  30. package/lib/module/@types/plugpag.js +30 -0
  31. package/lib/module/@types/plugpag.js.map +1 -0
  32. package/lib/module/@types/sunmi.js +2 -0
  33. package/lib/module/@types/sunmi.js.map +1 -0
  34. package/lib/module/errorMessages.js +3 -0
  35. package/lib/module/errorMessages.js.map +1 -0
  36. package/lib/module/index.js +50 -0
  37. package/lib/module/index.js.map +1 -0
  38. package/lib/typescript/src/@types/index.d.ts +3 -0
  39. package/lib/typescript/src/@types/index.d.ts.map +1 -0
  40. package/lib/typescript/src/@types/plugpag.d.ts +47 -0
  41. package/lib/typescript/src/@types/plugpag.d.ts.map +1 -0
  42. package/lib/typescript/src/@types/sunmi.d.ts +9 -0
  43. package/lib/typescript/src/@types/sunmi.d.ts.map +1 -0
  44. package/lib/typescript/src/errorMessages.d.ts +3 -0
  45. package/lib/typescript/src/errorMessages.d.ts.map +1 -0
  46. package/lib/typescript/src/index.d.ts +23 -0
  47. package/lib/typescript/src/index.d.ts.map +1 -0
  48. package/package.json +146 -0
  49. package/src/@types/index.ts +2 -0
  50. package/src/@types/plugpag.ts +60 -0
  51. package/src/@types/sunmi.ts +13 -0
  52. package/src/errorMessages.ts +7 -0
  53. package/src/index.tsx +85 -0
package/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Ian Langkammer Batista
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,111 @@
1
+ # react-native-mesmart
2
+
3
+ Sunmi RFID reader and Plugpag wrapper for React Native
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ npm install react-native-mesmart
9
+ ```
10
+
11
+ OR
12
+
13
+ ```sh
14
+ yarn add react-native-mesmart
15
+ ```
16
+
17
+ Now, you just need to add that code line below in your build.gradle in your react native application
18
+
19
+ ```
20
+ allprojects {
21
+ repositories {
22
+ ...
23
+ maven {
24
+ url 'https://github.com/pagseguro/PlugPagServiceWrapper/raw/master'
25
+ }
26
+ }
27
+ }
28
+ ```
29
+
30
+ ## Usage
31
+
32
+ ```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)
85
+
86
+ // Register event listener to catch onPlugPagEventData
87
+ useEffect(() => {
88
+ const eventEmitter = new NativeEventEmitter(NativeModules.Plugpag);
89
+ let eventListener = eventEmitter.addListener(
90
+ 'onPlugPagEventData',
91
+ (plugpagEvent: PlugPagEventData) => { console.log(plugpagEvent) },
92
+ );
93
+
94
+ return () => {
95
+ eventListener.remove();
96
+ };
97
+ }, []);
98
+
99
+ ```
100
+
101
+ ## Contributing
102
+
103
+ See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
104
+
105
+ ## License
106
+
107
+ MIT
108
+
109
+ ---
110
+
111
+ Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
@@ -0,0 +1,96 @@
1
+ import com.android.Version
2
+
3
+ buildscript {
4
+ // Buildscript is evaluated before everything else so we can't use getExtOrDefault
5
+ def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["Plugpag_kotlinVersion"]
6
+
7
+ repositories {
8
+ google()
9
+ mavenCentral()
10
+ }
11
+
12
+ dependencies {
13
+ classpath "com.android.tools.build:gradle:7.4.2"
14
+ classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
15
+ }
16
+ }
17
+
18
+ def isNewArchitectureEnabled() {
19
+ return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
20
+ }
21
+
22
+ apply plugin: "com.android.library"
23
+ apply plugin: "kotlin-android"
24
+
25
+ if (isNewArchitectureEnabled()) {
26
+ apply plugin: "com.facebook.react"
27
+ }
28
+
29
+ def getExtOrDefault(name) {
30
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["Plugpag_" + name]
31
+ }
32
+
33
+ def getExtOrIntegerDefault(name) {
34
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["Plugpag_" + name]).toInteger()
35
+ }
36
+
37
+ static def supportsNamespace() {
38
+ def parsed = Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
39
+ def major = parsed[0].toInteger()
40
+ def minor = parsed[1].toInteger()
41
+
42
+ // Namespace support was added in 7.3.0
43
+ return (major == 7 && minor >= 3) || major >= 8
44
+ }
45
+
46
+ android {
47
+ if (supportsNamespace()) {
48
+ namespace "com.mesmart"
49
+
50
+ sourceSets {
51
+ main {
52
+ manifest.srcFile "src/main/AndroidManifestNew.xml"
53
+ }
54
+ }
55
+ }
56
+
57
+ compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
58
+
59
+ defaultConfig {
60
+ minSdkVersion getExtOrIntegerDefault("minSdkVersion")
61
+ targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
62
+ }
63
+
64
+ buildTypes {
65
+ release {
66
+ minifyEnabled false
67
+ }
68
+ }
69
+
70
+ lintOptions {
71
+ disable "GradleCompatible"
72
+ }
73
+
74
+ compileOptions {
75
+ sourceCompatibility JavaVersion.VERSION_11
76
+ targetCompatibility JavaVersion.VERSION_11
77
+ }
78
+ }
79
+
80
+ repositories {
81
+ mavenCentral()
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' }
86
+ }
87
+
88
+ def kotlin_version = getExtOrDefault("kotlinVersion")
89
+
90
+ dependencies {
91
+ implementation "com.facebook.react:react-android:0.75.0-rc.5"
92
+ implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
93
+ implementation "com.sunmi:PayLib-release:2.0.07"
94
+ implementation "br.com.uol.pagseguro.plugpagservice.wrapper:wrapper:1.28.2"
95
+ implementation "com.sunmi:printerlibrary:1.0.23"
96
+ }
@@ -0,0 +1,19 @@
1
+ ## For more details on how to configure your build environment visit
2
+ # http://www.gradle.org/docs/current/userguide/build_environment.html
3
+ #
4
+ # Specifies the JVM arguments used for the daemon process.
5
+ # The setting is particularly useful for tweaking memory settings.
6
+ # Default value: -Xmx1024m -XX:MaxPermSize=256m
7
+ # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
8
+ #
9
+ # When configured, Gradle will run in incubating parallel mode.
10
+ # This option should only be used with decoupled projects. For more details, visit
11
+ # https://developer.android.com/r/tools/gradle-multi-project-decoupled-projects
12
+ # org.gradle.parallel=true
13
+ #Fri Jul 19 11:34:20 BRT 2024
14
+ Plugpag_compileSdkVersion=31
15
+ Plugpag_kotlinVersion=1.9.24
16
+ Plugpag_minSdkVersion=23
17
+ Plugpag_ndkversion=21.4.7075529
18
+ Plugpag_targetSdkVersion=31
19
+ android.useAndroidX=true
@@ -0,0 +1,5 @@
1
+ <manifest
2
+ xmlns:android="http://schemas.android.com/apk/res/android"
3
+ package="com.mesmart"
4
+ >
5
+ </manifest>
@@ -0,0 +1,4 @@
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
+ </manifest>
@@ -0,0 +1,21 @@
1
+ package com.mesmart
2
+
3
+ import com.facebook.react.ReactPackage
4
+ import com.facebook.react.bridge.NativeModule
5
+ import com.facebook.react.bridge.ReactApplicationContext
6
+ import com.facebook.react.uimanager.ViewManager
7
+ import com.plugpag.PlugpagModule
8
+ import com.sunmi.SunmiModule
9
+
10
+ class Packages : ReactPackage {
11
+ override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
12
+ return listOf(
13
+ PlugpagModule(reactContext),
14
+ SunmiModule(reactContext)
15
+ )
16
+ }
17
+
18
+ override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
19
+ return emptyList()
20
+ }
21
+ }
@@ -0,0 +1,112 @@
1
+ package com.plugpag
2
+
3
+ import br.com.uol.pagseguro.plugpagservice.wrapper.PlugPag
4
+ import br.com.uol.pagseguro.plugpagservice.wrapper.PlugPagActivationData
5
+ import br.com.uol.pagseguro.plugpagservice.wrapper.PlugPagCustomPrinterLayout
6
+ import br.com.uol.pagseguro.plugpagservice.wrapper.PlugPagEventData
7
+ import br.com.uol.pagseguro.plugpagservice.wrapper.PlugPagEventListener
8
+ import br.com.uol.pagseguro.plugpagservice.wrapper.PlugPagPaymentData
9
+ import com.facebook.react.bridge.Arguments
10
+ import com.facebook.react.bridge.Promise
11
+ import com.facebook.react.bridge.ReactApplicationContext
12
+ import com.facebook.react.bridge.ReactContext
13
+ import com.facebook.react.bridge.WritableMap
14
+ import com.facebook.react.modules.core.DeviceEventManagerModule
15
+ import com.plugpag.utils.PlugpagUtils
16
+
17
+ class PlugpagImpl(
18
+ private val reactContext: ReactApplicationContext,
19
+ val activationCode: String
20
+ ) {
21
+ private val plugPag = PlugPag(reactContext)
22
+
23
+ init {
24
+ setEventListener()
25
+ }
26
+
27
+ private fun setEventListener() {
28
+ plugPag.setEventListener(object : PlugPagEventListener {
29
+ override fun onEvent(data: PlugPagEventData) {
30
+ sendEvent(
31
+ reactContext,
32
+ Arguments.createMap().apply {
33
+ putInt("eventCode", data.eventCode)
34
+ putString("customMessage", data.customMessage)
35
+ }
36
+ )
37
+ }
38
+ })
39
+ }
40
+
41
+ private fun sendEvent(
42
+ reactContext: ReactContext,
43
+ params: WritableMap?
44
+ ) {
45
+ reactContext
46
+ .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
47
+ .emit("onPlugPagEventData", params)
48
+ }
49
+
50
+ fun pay(
51
+ rechargeValue: String,
52
+ transactionType: String,
53
+ promise: Promise
54
+ ) {
55
+ Thread {
56
+ run {
57
+ val paymentData = PlugpagUtils.resolveTransactionType(transactionType)?.let {
58
+ PlugPagPaymentData(
59
+ it,
60
+ rechargeValue.toInt(),
61
+ PlugPag.INSTALLMENT_TYPE_A_VISTA,
62
+ 1,
63
+ null
64
+ )
65
+ }
66
+
67
+ val terminalResult = plugPag.initializeAndActivatePinpad(
68
+ PlugPagActivationData(activationCode)
69
+ )
70
+
71
+ if (terminalResult.result == PlugPag.RET_OK) {
72
+ val paymentResult = paymentData?.let { plugPag.doPayment(it) }
73
+ promise.resolve(
74
+ Arguments.createMap().apply {
75
+ putString("message", paymentResult?.message)
76
+ putString("errorCode", paymentResult?.errorCode)
77
+ paymentResult?.result?.let { putInt("result", it) }
78
+ }
79
+ )
80
+ } else {
81
+ throw Exception("Error on initialize and activate pinpad. Maybe your activationCode is not valid!")
82
+ }
83
+ }
84
+ }.start()
85
+ }
86
+
87
+ fun abortTransaction() {
88
+ Thread {
89
+ run {
90
+ plugPag.abort()
91
+ }
92
+ }.start()
93
+ }
94
+
95
+ fun printEstablishmentReceipt() {
96
+ plugPag.reprintStablishmentReceipt()
97
+ }
98
+
99
+ fun printClientReceipt() {
100
+ plugPag.reprintCustomerReceipt()
101
+ }
102
+
103
+ fun customDialogClientViaPrinter(color: String) {
104
+ Thread {
105
+ run {
106
+ val customPrinter = PlugPagCustomPrinterLayout()
107
+ customPrinter.buttonBackgroundColor = color
108
+ plugPag.setPlugPagCustomPrinterLayout(customPrinter)
109
+ }
110
+ }.start()
111
+ }
112
+ }
@@ -0,0 +1,77 @@
1
+ package com.plugpag
2
+
3
+ import br.com.uol.pagseguro.plugpagservice.wrapper.PlugPagEventData
4
+ import com.facebook.react.bridge.Promise
5
+ import com.facebook.react.bridge.ReactApplicationContext
6
+ import com.facebook.react.bridge.ReactContextBaseJavaModule
7
+ import com.facebook.react.bridge.ReactMethod
8
+ import com.facebook.react.bridge.ReadableMap
9
+ import com.plugpag.utils.PlugpagUtils
10
+
11
+ class PlugpagModule(
12
+ private val reactContext: ReactApplicationContext
13
+ ) : ReactContextBaseJavaModule(reactContext) {
14
+ private var plugpag: PlugpagImpl? = null
15
+
16
+ override fun getName(): String {
17
+ return "Plugpag"
18
+ }
19
+
20
+ private fun checkPlugpag() {
21
+ if (plugpag == null)
22
+ throw Exception("Plugpag is not initiated! Did you use initPlugpag(code: string) method before calling this method?")
23
+ }
24
+
25
+ @ReactMethod(isBlockingSynchronousMethod = true)
26
+ fun init(code: String) {
27
+ if (plugpag != null)
28
+ throw Exception("Plugpag has already been initiated with code: '" + plugpag!!.activationCode + "'!")
29
+
30
+ plugpag = PlugpagImpl(reactContext, code)
31
+ }
32
+
33
+ @ReactMethod
34
+ fun pay(
35
+ rechargeValue: String,
36
+ transactionType: String,
37
+ promise: Promise
38
+ ) {
39
+ try {
40
+ checkPlugpag()
41
+ plugpag!!.pay(rechargeValue, transactionType, promise)
42
+ } catch (e: Exception) {
43
+ e.printStackTrace()
44
+ promise.reject(e)
45
+ }
46
+ }
47
+
48
+ @ReactMethod(isBlockingSynchronousMethod = true)
49
+ fun abortPayment() {
50
+ checkPlugpag()
51
+ plugpag!!.abortTransaction()
52
+ }
53
+
54
+ @ReactMethod(isBlockingSynchronousMethod = true)
55
+ fun printEstablishmentReceipt() {
56
+ checkPlugpag()
57
+ plugpag!!.printEstablishmentReceipt()
58
+ }
59
+
60
+ @ReactMethod(isBlockingSynchronousMethod = true)
61
+ fun printClientReceipt() {
62
+ checkPlugpag()
63
+ plugpag!!.printClientReceipt()
64
+ }
65
+
66
+ @ReactMethod(isBlockingSynchronousMethod = true)
67
+ fun customDialogClientViaPrinter(color: String) {
68
+ checkPlugpag()
69
+ plugpag!!.customDialogClientViaPrinter(color)
70
+ }
71
+
72
+ @ReactMethod(isBlockingSynchronousMethod = true)
73
+ fun resolveTransactionEvent(data: ReadableMap): String {
74
+ val plugPagEventData = PlugPagEventData(data.getInt("eventCode"))
75
+ return PlugpagUtils.resolveTransactionEvent(plugPagEventData)
76
+ }
77
+ }
@@ -0,0 +1,40 @@
1
+ package com.plugpag.utils
2
+
3
+ import br.com.uol.pagseguro.plugpagservice.wrapper.PlugPag
4
+ import br.com.uol.pagseguro.plugpagservice.wrapper.PlugPagEventData
5
+ import br.com.uol.pagseguro.plugpagservice.wrapper.PlugPagEventData.Companion.EVENT_CODE_INSERTED_CARD
6
+ import br.com.uol.pagseguro.plugpagservice.wrapper.PlugPagEventData.Companion.EVENT_CODE_PIN_OK
7
+ import br.com.uol.pagseguro.plugpagservice.wrapper.PlugPagEventData.Companion.EVENT_CODE_REMOVED_CARD
8
+ import br.com.uol.pagseguro.plugpagservice.wrapper.PlugPagEventData.Companion.EVENT_CODE_SALE_APPROVED
9
+ import br.com.uol.pagseguro.plugpagservice.wrapper.PlugPagEventData.Companion.EVENT_CODE_SALE_NOT_APPROVED
10
+ import br.com.uol.pagseguro.plugpagservice.wrapper.PlugPagEventData.Companion.EVENT_CODE_WAITING_CARD
11
+ import br.com.uol.pagseguro.plugpagservice.wrapper.PlugPagEventData.Companion.EVENT_CODE_WAITING_REMOVE_CARD
12
+
13
+ class PlugpagUtils {
14
+ companion object {
15
+ fun resolveTransactionType(type: String): Int? {
16
+ return when(type) {
17
+ "debitCard" -> PlugPag.TYPE_DEBITO
18
+ "creditCard" -> PlugPag.TYPE_CREDITO
19
+ "voucher" -> PlugPag.TYPE_VOUCHER
20
+ "pix" -> PlugPag.TYPE_PIX
21
+ else -> null
22
+ }
23
+ }
24
+ fun resolveTransactionEvent(data : PlugPagEventData): String {
25
+ return when(data.eventCode) {
26
+ EVENT_CODE_WAITING_CARD -> "Aproxime ou insira o cartão de pagamento"
27
+ EVENT_CODE_SALE_APPROVED -> "Processando..."
28
+ EVENT_CODE_SALE_NOT_APPROVED -> "Processando..."
29
+ EVENT_CODE_WAITING_REMOVE_CARD -> "Por favor, remova o seu cartão"
30
+ EVENT_CODE_INSERTED_CARD -> "Processando..."
31
+ EVENT_CODE_PIN_OK -> "Processando..."
32
+ EVENT_CODE_REMOVED_CARD -> "Processando..."
33
+ 18 -> "Transação aprovada"
34
+ 17 -> "Informe a senha do seu cartão"
35
+ 4 -> "Processando..."
36
+ else -> ""
37
+ }
38
+ }
39
+ }
40
+ }
@@ -0,0 +1,75 @@
1
+ package com.sunmi
2
+
3
+ import android.graphics.Bitmap
4
+ import android.graphics.BitmapFactory
5
+ import com.facebook.react.bridge.Promise
6
+ import com.facebook.react.bridge.ReactApplicationContext
7
+ import com.mesmart.R
8
+ import com.sunmi.utils.SunmiPrintHelper
9
+ import com.sunmi.utils.SunmiRfidHelper
10
+ import sunmi.paylib.SunmiPayKernel
11
+ import sunmi.paylib.SunmiPayKernel.ConnectCallback
12
+
13
+ class SunmiImpl(
14
+ private val reactContext: ReactApplicationContext
15
+ ) {
16
+ private val payKernel = SunmiPayKernel.getInstance()
17
+ private lateinit var sunmiRfidHelper: SunmiRfidHelper
18
+ private lateinit var sunmiPrintHelper: SunmiPrintHelper
19
+
20
+ fun bindService() {
21
+ payKernel.initPaySDK(reactContext, object : ConnectCallback {
22
+ override fun onConnectPaySDK() {
23
+ sunmiRfidHelper = SunmiRfidHelper(payKernel)
24
+ sunmiPrintHelper = SunmiPrintHelper(reactContext)
25
+ }
26
+
27
+ override fun onDisconnectPaySDK() {
28
+ }
29
+ })
30
+ }
31
+
32
+ fun searchRfid(promise: Promise) {
33
+ sunmiRfidHelper.searchRfid(promise)
34
+ }
35
+
36
+ fun cancelCardSearch() {
37
+ sunmiRfidHelper.cancelCardSearch()
38
+ }
39
+
40
+ fun printText(
41
+ str: String,
42
+ fontSize: Int,
43
+ isBold: Boolean,
44
+ isUnderline: Boolean
45
+ ) {
46
+ sunmiPrintHelper.printText(
47
+ str,
48
+ fontSize.toFloat(),
49
+ isBold,
50
+ isUnderline,
51
+ null)
52
+ }
53
+
54
+ fun printEmptyLine() {
55
+ sunmiPrintHelper.printEmptyLine()
56
+ }
57
+
58
+ fun printBpLogo() {
59
+ val options = BitmapFactory.Options().apply {
60
+ inTargetDensity = 160
61
+ inDensity = 160
62
+ }
63
+ val bitmap = BitmapFactory.decodeResource(reactContext.resources, R.drawable.logo_black, options)
64
+
65
+ sunmiPrintHelper.printBitmap(
66
+ Bitmap.createScaledBitmap(bitmap, 375, 100, true),
67
+ 1
68
+ )
69
+ }
70
+
71
+ fun setPrinterAlign(align: Int) {
72
+ sunmiPrintHelper.setPrinterAlign(align)
73
+ }
74
+ }
75
+
@@ -0,0 +1,75 @@
1
+ package com.sunmi
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.sunmi.utils.SunmiUtils
8
+
9
+ class SunmiModule(
10
+ reactContext: ReactApplicationContext
11
+ ) : ReactContextBaseJavaModule(reactContext) {
12
+ private var sunmi: SunmiImpl? = null
13
+
14
+ init {
15
+ if (SunmiUtils.isSunmiP2()) {
16
+ sunmi = SunmiImpl(reactContext)
17
+ sunmi!!.bindService()
18
+ }
19
+ }
20
+
21
+ override fun getName(): String {
22
+ return "Sunmi"
23
+ }
24
+
25
+ private fun checkSunmi() {
26
+ if (sunmi == null)
27
+ throw Exception("Sunmi is null! You may not working in a Sunmi P2 or an error ocurred during the initialization!")
28
+ }
29
+
30
+ @ReactMethod
31
+ fun searchRfid(promise: Promise) {
32
+ try {
33
+ checkSunmi()
34
+ this.sunmi!!.searchRfid(promise)
35
+ } catch (e: Exception) {
36
+ e.printStackTrace()
37
+ promise.reject(e)
38
+ }
39
+ }
40
+
41
+ @ReactMethod(isBlockingSynchronousMethod = true)
42
+ fun cancelCardSearch() {
43
+ checkSunmi()
44
+ this.sunmi!!.cancelCardSearch()
45
+ }
46
+
47
+ @ReactMethod(isBlockingSynchronousMethod = true)
48
+ fun printText(
49
+ str: String,
50
+ fontSize: Int,
51
+ isBold: Boolean,
52
+ isUnderline: Boolean
53
+ ) {
54
+ checkSunmi()
55
+ this.sunmi!!.printText(str, fontSize, isBold, isUnderline)
56
+ }
57
+
58
+ @ReactMethod(isBlockingSynchronousMethod = true)
59
+ fun printEmptyLine() {
60
+ checkSunmi()
61
+ sunmi!!.printEmptyLine()
62
+ }
63
+
64
+ @ReactMethod(isBlockingSynchronousMethod = true)
65
+ fun printBpLogo() {
66
+ checkSunmi()
67
+ sunmi!!.printBpLogo()
68
+ }
69
+
70
+ @ReactMethod(isBlockingSynchronousMethod = true)
71
+ fun setPrinterAlign(align: Int) {
72
+ checkSunmi()
73
+ sunmi!!.setPrinterAlign(align)
74
+ }
75
+ }