payme-mini-app 2.0.0 → 3.0.1
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 +53 -1
- package/android/build.gradle +1 -1
- package/android/src/main/java/com/paymeminiapp/PaymeMiniAppModule.kt +215 -224
- package/ios/PaymeMiniApp.mm +2 -2
- package/ios/PaymeMiniApp.swift +197 -87
- package/lib/module/index.js +69 -10
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/src/index.d.ts +31 -4
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/index.type.d.ts +38 -17
- package/lib/typescript/src/index.type.d.ts.map +1 -1
- package/package.json +1 -1
- package/payme-mini-app.podspec +2 -2
- package/src/index.tsx +114 -16
- package/src/index.type.ts +42 -16
package/README.md
CHANGED
|
@@ -1 +1,53 @@
|
|
|
1
|
-
Vui lòng xem tài liệu tích hợp đầy đủ [tại đây](https://github.com/PayME-Miniapp/payme-mini-app#readme)
|
|
1
|
+
Vui lòng xem tài liệu tích hợp đầy đủ [tại đây](https://github.com/PayME-Miniapp/payme-mini-app#readme)
|
|
2
|
+
## 3.0.1: sửa lỗi bridge (tương thích 3.0.0)
|
|
3
|
+
|
|
4
|
+
- Dùng PayME Miniapp SDK iOS `0.8.3`, Android `0.8.4` (SDK 1.0.24): `USER_CANCEL` chỉ báo một lần mỗi lần đóng; Android không còn ép app chuyển sang giao diện sáng và không đổi ngôn ngữ/resources của app.
|
|
5
|
+
- `mode` mặc định theo `env`: `SANDBOX` → `miniapp_sandbox`, môi trường khác → `miniapp_product`. Truyền `mode` thì giữ nguyên giá trị đó.
|
|
6
|
+
- Chỉ mở một miniapp mỗi lúc: gọi `open` khi đang có miniapp mở thì nhận `onError` code `MINIAPP_ALREADY_OPEN`, miniapp đang mở giữ nguyên. Gọi `open` lúc miniapp trước đang đóng thì SDK đợi đóng xong rồi mở (không báo lỗi).
|
|
7
|
+
- Export type: `PayMEOpeningParam`, `PayMEEnv`, `PayMEMode`, `PayMEOpeningType`, `PayMELocale`, các type `data` (`PayMEPaymentParam`, `PayMEPaymentDirectParam`, `PayMETransferQRParam`, `PayMEServiceParam`, `PayMEPayParam`) và payload sự kiện `PayMEErrorEvent`, `PayMEResponseEvent`.
|
|
8
|
+
- `ipnUrl` của `PAY` là tuỳ chọn.
|
|
9
|
+
- Lắng nghe sự kiện có kiểu (vẫn dùng được `PayMEMiniAppEvents`):
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
const sub = addErrorListener((e) => console.log(e.action, e.code, e.description));
|
|
13
|
+
const sub2 = addResponseListener((r) => console.log(r.action));
|
|
14
|
+
// huỷ: sub.remove(); sub2.remove();
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
- Mã lỗi do bridge tạo, giống nhau trên iOS và Android (`PayMEErrorCode`):
|
|
18
|
+
- `PAYME_NOT_INITIALIZED`: gọi `open`, `getBalance`, `getAccountInfo`, `setLanguage`, `close` khi chưa `init` (Android: hoặc `open` khi chưa có Activity). iOS trước đây crash, Android trước đây bỏ qua.
|
|
19
|
+
- `INVALID_PARAM`: thiếu/sai `data` bắt buộc của `PAY`, `PAYMENT`, `TRANSFER_QR`, `SERVICE`, hoặc action không hỗ trợ. Android trước đây mở ví PayME. `amount` của `DEPOSIT`, `WITHDRAW`, `TRANSFER` vẫn tuỳ chọn, nhưng nếu có mà không phải số nguyên thì báo `INVALID_PARAM` (trước đây bị bỏ qua).
|
|
20
|
+
- `PAYME_SDK_ERROR` (chỉ Android): SDK ném lỗi ngoài dự kiến; `description` là tên class exception. Trước đây bị bỏ qua, JS không nhận được gì.
|
|
21
|
+
- Mã lỗi SDK cũng có trong `PayMEErrorCode`:
|
|
22
|
+
- `USER_CANCEL`: người dùng đóng miniapp; trên iOS cả khi app gọi `close()` (Android: `close()` không báo).
|
|
23
|
+
- `SYSTEM_CANCEL` (iOS): SDK đóng miniapp vì không khởi động được (vd web server lỗi).
|
|
24
|
+
- `MINIAPP_ALREADY_OPEN`: `open` khi đang có miniapp mở.
|
|
25
|
+
- `MINIAPP_CANNOT_PRESENT`: không hiện được miniapp (iOS: không có màn hình để present; Android: mở `modal` khi Activity không phải `FragmentActivity`, hoặc hiện lỗi).
|
|
26
|
+
- `amount` phải là số nguyên (`20000` hoặc `20000.0`), không làm tròn số lẻ.
|
|
27
|
+
- Truyền sai kiểu từ JS (`null`, số thay cho chuỗi...) không còn làm crash app.
|
|
28
|
+
- `isShowResult`, `isBackToApp` chỉ nhận boolean; giá trị khác (vd `0`/`1`) bị bỏ qua và dùng mặc định `true`, giống nhau trên iOS và Android.
|
|
29
|
+
- `open` nhận `type` không phân biệt hoa thường (`'Modal'` → `modal`) trên cả iOS và Android.
|
|
30
|
+
- Android: mở miniapp bằng Activity hiện tại (trước đây giữ Activity lúc `init`, lỗi sau khi xoay màn hình hoặc mở lại app) và gọi SDK trên UI thread.
|
|
31
|
+
- Android: `getBalance`, `getAccountInfo` chạy được khi chưa có Activity (headless/nền), như iOS; `open` vẫn cần Activity.
|
|
32
|
+
- iOS: mở miniapp từ màn hình trên cùng của app (bỏ qua màn hình của SDK): chạy được với app dùng scene và khi đang có modal (vd RN `Modal`).
|
|
33
|
+
- iOS: `close()` đóng chính miniapp đang mở (kể cả khi đang bị camera/màn hình khác che), không dismiss nhầm màn hình của app.
|
|
34
|
+
- iOS: gọi lại `init` với cùng `appId`/`publicKey`/`privateKey` chỉ cập nhật `env`/`mode`/`locale`, miniapp đang mở vẫn `close()` được; đổi key thì đóng miniapp đang mở trước. Reload JS đóng miniapp đang mở.
|
|
35
|
+
- Nên đăng ký `addErrorListener`/`addResponseListener` trước khi gọi `init`/`open`: event gửi khi chưa có listener sẽ bị bỏ.
|
|
36
|
+
|
|
37
|
+
## 3.0.0: identityToken thay cho phone (không tương thích với 2.x)
|
|
38
|
+
|
|
39
|
+
Dùng PayME Miniapp SDK iOS `0.8.1` và Android `0.8.2` (SDK 1.0.24). App không còn truyền số điện thoại: server của đối tác gọi API cấp chuỗi của BE PayME để lấy `identityToken` cho người dùng, rồi app truyền chuỗi này vào SDK.
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
open('modal', { action: 'OPEN', identityToken });
|
|
43
|
+
open('modal', { action: 'PAY', identityToken, data: { amount: 10000, transactionId: 'TXN', ipnUrl: '' } });
|
|
44
|
+
open('modal', { action: 'PAYMENT', data: { transaction: 'TXN' } }); // không có identityToken: khách vãng lai (thẻ, QR)
|
|
45
|
+
open('screen', { action: 'PAYME' }); // ví PayME: người dùng tự nhập số và PIN
|
|
46
|
+
getBalance(identityToken);
|
|
47
|
+
getAccountInfo(identityToken);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
- `identityToken` bắt buộc với mọi action, trừ `PAYMENT` (tuỳ chọn) và `PAYME` (không dùng).
|
|
51
|
+
- Chuỗi sai hoặc hết hạn: `onError` trả code `401`. Chuỗi rỗng ở action bắt buộc, `getBalance`, `getAccountInfo`: `onError` trả code `IDENTITY_TOKEN_REQUIRED`.
|
|
52
|
+
- Chỉ giữ `identityToken` trong bộ nhớ: không log, không lưu, không đưa lên URL.
|
|
53
|
+
- iOS cần Xcode 26.2 trở lên.
|
package/android/build.gradle
CHANGED
|
@@ -73,5 +73,5 @@ def kotlin_version = getExtOrDefault("kotlinVersion")
|
|
|
73
73
|
dependencies {
|
|
74
74
|
implementation "com.facebook.react:react-android"
|
|
75
75
|
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
|
76
|
-
implementation 'com.github.PayME-Miniapp:native-android:0.
|
|
76
|
+
implementation 'com.github.PayME-Miniapp:native-android:0.8.4'
|
|
77
77
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
package com.paymeminiapp
|
|
2
2
|
|
|
3
3
|
import android.app.Activity
|
|
4
|
+
import android.content.Context
|
|
4
5
|
import android.util.Log
|
|
5
6
|
import com.facebook.react.bridge.Arguments
|
|
6
7
|
import com.facebook.react.bridge.ReactApplicationContext
|
|
@@ -8,6 +9,8 @@ import com.facebook.react.bridge.ReactContext
|
|
|
8
9
|
import com.facebook.react.bridge.ReactContextBaseJavaModule
|
|
9
10
|
import com.facebook.react.bridge.ReactMethod
|
|
10
11
|
import com.facebook.react.bridge.ReadableMap
|
|
12
|
+
import com.facebook.react.bridge.ReadableType
|
|
13
|
+
import com.facebook.react.bridge.UiThreadUtil
|
|
11
14
|
import com.facebook.react.bridge.WritableMap
|
|
12
15
|
import com.facebook.react.modules.core.DeviceEventManagerModule
|
|
13
16
|
import com.payme.sdk.PayMEMiniApp
|
|
@@ -33,6 +36,7 @@ import com.payme.sdk.models.PaymentDirectData
|
|
|
33
36
|
import com.payme.sdk.models.ServiceData
|
|
34
37
|
import com.payme.sdk.models.TransferQRData
|
|
35
38
|
import org.json.JSONObject
|
|
39
|
+
import java.lang.ref.WeakReference
|
|
36
40
|
|
|
37
41
|
|
|
38
42
|
class PaymeMiniAppModule(reactContext: ReactApplicationContext) :
|
|
@@ -63,6 +67,37 @@ class PaymeMiniAppModule(reactContext: ReactApplicationContext) :
|
|
|
63
67
|
}
|
|
64
68
|
}
|
|
65
69
|
|
|
70
|
+
// Cấu hình init; giữ lại để tạo SDK khi có Activity (currentActivity có thể null lúc init).
|
|
71
|
+
// Chỉ đọc/ghi trên UI thread (xem onUi).
|
|
72
|
+
private var initConfig: InitConfig? = null
|
|
73
|
+
|
|
74
|
+
// Context mà SDK hiện tại dùng (Activity, hoặc applicationContext khi chạy nền); weak để không giữ Activity đã huỷ.
|
|
75
|
+
private var sdkContext: WeakReference<Context>? = null
|
|
76
|
+
|
|
77
|
+
private data class InitConfig(
|
|
78
|
+
val appId: String,
|
|
79
|
+
val publicKey: String,
|
|
80
|
+
val privateKey: String,
|
|
81
|
+
val locale: String,
|
|
82
|
+
val env: String,
|
|
83
|
+
val mode: String
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* SDK đụng UI (show dialog, startActivity, resources): chạy tuần tự trên UI thread, giữ thứ tự gọi từ JS.
|
|
88
|
+
* Lỗi ném ra không làm crash app mà báo onError(action, errorCode) để JS luôn nhận phản hồi.
|
|
89
|
+
*/
|
|
90
|
+
private fun onUi(action: String, errorCode: String = ERROR_SDK, block: () -> Unit) {
|
|
91
|
+
UiThreadUtil.runOnUiThread {
|
|
92
|
+
try {
|
|
93
|
+
block()
|
|
94
|
+
} catch (e: Exception) {
|
|
95
|
+
Log.w("PAYMELOG", "PaymeMiniApp $action: ${e.javaClass.simpleName}")
|
|
96
|
+
sendError(action, errorCode, e.javaClass.name)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
66
101
|
@ReactMethod
|
|
67
102
|
fun initPayME(
|
|
68
103
|
appId: String,
|
|
@@ -72,173 +107,201 @@ class PaymeMiniAppModule(reactContext: ReactApplicationContext) :
|
|
|
72
107
|
env: String,
|
|
73
108
|
mode: String
|
|
74
109
|
) {
|
|
75
|
-
|
|
110
|
+
onUi("INIT") {
|
|
111
|
+
initConfig = InitConfig(appId, publicKey, privateKey, locale, env, mode)
|
|
112
|
+
payMEMiniApp = null
|
|
113
|
+
sdkContext = null
|
|
114
|
+
createPayMEIfNeeded(forOpen = false)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
76
117
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
118
|
+
/**
|
|
119
|
+
* Tạo SDK từ cấu hình init; null khi chưa init, hoặc open mà chưa có Activity.
|
|
120
|
+
* SDK giữ context lúc tạo và ép kiểu FragmentActivity khi mở modal, nên open luôn dùng Activity
|
|
121
|
+
* hiện tại (tạo lại khi Activity đổi: xoay màn hình, config change, mở lại app).
|
|
122
|
+
* Hàm khác (getBalance, getAccountInfo...) không cần Activity: chạy nền thì dùng applicationContext
|
|
123
|
+
* (SDK 0.8.4 không đổi resources/locale của context truyền vào).
|
|
124
|
+
* Tạo lại an toàn: phiên miniapp, config, callback nằm ở PayMERuntime (static);
|
|
125
|
+
* close() của instance mới tự lấy phiên đang mở từ PayMERuntime.
|
|
126
|
+
*/
|
|
127
|
+
private fun createPayMEIfNeeded(forOpen: Boolean): PayMEMiniApp? {
|
|
128
|
+
val config = initConfig ?: return null
|
|
129
|
+
val current: Activity? = reactApplicationContext.currentActivity
|
|
130
|
+
val sdk = payMEMiniApp
|
|
131
|
+
val old = sdkContext?.get()
|
|
132
|
+
val oldDead = old == null || (old is Activity && (old.isFinishing || old.isDestroyed))
|
|
133
|
+
if (sdk != null) {
|
|
134
|
+
if (forOpen) {
|
|
135
|
+
if (current === old) return sdk
|
|
136
|
+
// Chạy nền: dùng lại SDK nếu nó đang giữ Activity còn sống.
|
|
137
|
+
if (current == null) return if (old is Activity && !oldDead) sdk else null
|
|
138
|
+
} else if (!oldDead) {
|
|
139
|
+
return sdk
|
|
140
|
+
}
|
|
81
141
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
142
|
+
val context: Context = current ?: if (forOpen) return null else reactApplicationContext.applicationContext
|
|
143
|
+
val newSdk = PayMEMiniApp(
|
|
144
|
+
context, config.appId, config.publicKey, config.privateKey,
|
|
145
|
+
getEnv(config.env), getLocale(config.locale)
|
|
146
|
+
)
|
|
147
|
+
newSdk.setMode(config.mode)
|
|
148
|
+
newSdk.setUpListener(
|
|
149
|
+
onResponse = { action: ActionOpenMiniApp, jsonObject: JSONObject? -> onResponse(action, jsonObject) },
|
|
150
|
+
onError = { action: ActionOpenMiniApp, payMEError: PayMEError -> onError(action, payMEError) }
|
|
151
|
+
)
|
|
152
|
+
payMEMiniApp = newSdk
|
|
153
|
+
sdkContext = WeakReference(context)
|
|
154
|
+
return newSdk
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Lấy SDK; chưa init (hoặc chưa có Activity) thì báo onError PAYME_NOT_INITIALIZED. */
|
|
158
|
+
private fun requirePayME(action: String, forOpen: Boolean = false): PayMEMiniApp? {
|
|
159
|
+
val sdk = createPayMEIfNeeded(forOpen)
|
|
160
|
+
if (sdk == null) {
|
|
161
|
+
val reason = if (initConfig == null) "Chưa gọi init (initPayME)" else "Chưa có Activity để khởi tạo PayME"
|
|
162
|
+
sendError(action, ERROR_NOT_INITIALIZED, "$reason trước $action")
|
|
163
|
+
}
|
|
164
|
+
return sdk
|
|
90
165
|
}
|
|
91
166
|
|
|
92
167
|
@ReactMethod
|
|
93
|
-
fun getBalance(
|
|
94
|
-
|
|
168
|
+
fun getBalance(identityToken: String) {
|
|
169
|
+
val action = ActionOpenMiniApp.GET_BALANCE.name
|
|
170
|
+
onUi(action) { requirePayME(action)?.getBalance(identityToken) }
|
|
95
171
|
}
|
|
96
172
|
|
|
97
173
|
@ReactMethod
|
|
98
|
-
fun getAccountInfo(
|
|
99
|
-
|
|
174
|
+
fun getAccountInfo(identityToken: String) {
|
|
175
|
+
val action = ActionOpenMiniApp.GET_ACCOUNT_INFO.name
|
|
176
|
+
onUi(action) { requirePayME(action)?.getAccountInformation(identityToken) }
|
|
100
177
|
}
|
|
101
178
|
|
|
102
179
|
@ReactMethod
|
|
103
180
|
fun setLanguage(language: String) {
|
|
104
|
-
|
|
105
|
-
|
|
181
|
+
onUi("SET_LANGUAGE") {
|
|
182
|
+
// Ghi lại để SDK tạo lại (khi đổi Activity) không quay về ngôn ngữ lúc init.
|
|
183
|
+
initConfig = initConfig?.copy(locale = language)
|
|
184
|
+
requirePayME("SET_LANGUAGE")?.setLanguage(getLocale(language))
|
|
185
|
+
}
|
|
106
186
|
}
|
|
107
187
|
|
|
108
188
|
@ReactMethod
|
|
109
189
|
fun closeMiniApp() {
|
|
110
|
-
|
|
190
|
+
onUi("CLOSE") { requirePayME("CLOSE")?.close() }
|
|
111
191
|
}
|
|
112
192
|
|
|
113
193
|
@ReactMethod
|
|
114
194
|
fun open(type: String, openingParam: ReadableMap) {
|
|
115
|
-
val action = openingParam.
|
|
116
|
-
|
|
117
|
-
val
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
if (data?.hasKey("extraData") == true) data.getMap("extraData")?.toHashMap()
|
|
124
|
-
?.filterValues { it != null }?.mapValues { it.value!! } else null
|
|
125
|
-
val isBackToApp =
|
|
126
|
-
if (data?.hasKey("isBackToApp") == true) data.getBoolean("isBackToApp") else true
|
|
127
|
-
val isShowResult =
|
|
128
|
-
if (data?.hasKey("isShowResult") == true) data.getBoolean("isShowResult") else true
|
|
129
|
-
val amount = if (data?.hasKey("amount") == true) data.getInt("amount") else null
|
|
130
|
-
val additionalData =
|
|
131
|
-
DepositWithdrawTransferData(description, amount, extraData, isBackToApp, isShowResult)
|
|
132
|
-
val openMiniAppData = getOpenMiniAppData(action, phone, additionalData)
|
|
133
|
-
payMEMiniApp?.openMiniApp(
|
|
134
|
-
getTypeOpenMiniApp(type),
|
|
135
|
-
openMiniAppData
|
|
136
|
-
)
|
|
137
|
-
return
|
|
138
|
-
}
|
|
139
|
-
if (action == "SERVICE") {
|
|
140
|
-
val service = data?.getString("service") ?: ""
|
|
141
|
-
val extraData =
|
|
142
|
-
if (data?.hasKey("extraData") == true) data.getMap("extraData")?.toHashMap()
|
|
143
|
-
?.filterValues { it != null }?.mapValues { it.value!! } else null
|
|
144
|
-
val isBackToApp =
|
|
145
|
-
if (data?.hasKey("isBackToApp") == true) data.getBoolean("isBackToApp") else true
|
|
146
|
-
val isShowResult =
|
|
147
|
-
if (data?.hasKey("isShowResult") == true) data.getBoolean("isShowResult") else true
|
|
148
|
-
val additionalData = ServiceData(service, extraData, isBackToApp, isShowResult)
|
|
149
|
-
val openMiniAppData = getOpenMiniAppData(action, phone, additionalData)
|
|
150
|
-
payMEMiniApp?.openMiniApp(
|
|
151
|
-
getTypeOpenMiniApp(type),
|
|
152
|
-
openMiniAppData
|
|
153
|
-
)
|
|
154
|
-
return
|
|
195
|
+
val action = openingParam.optString("action") ?: ""
|
|
196
|
+
// identityToken do BE PayME ký; chỉ giữ trong bộ nhớ, không log.
|
|
197
|
+
val identityToken = openingParam.optString("identityToken") ?: ""
|
|
198
|
+
val data = openingParam.optMap("data")
|
|
199
|
+
val openData = try {
|
|
200
|
+
buildOpenData(action, identityToken, data)
|
|
201
|
+
} catch (e: Exception) {
|
|
202
|
+
null // sai kiểu dữ liệu: coi như INVALID_PARAM
|
|
155
203
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
val extraData =
|
|
162
|
-
if (data?.hasKey("extraData") == true) data.getMap("extraData")?.toHashMap()
|
|
163
|
-
?.filterValues { it != null }?.mapValues { it.value!! } else null
|
|
164
|
-
val isShowResult =
|
|
165
|
-
if (data?.hasKey("isShowResult") == true) data.getBoolean("isShowResult") else true
|
|
166
|
-
var paymentData: PaymentData? = null
|
|
167
|
-
if (amount != null && transactionId != null) {
|
|
168
|
-
paymentData = PaymentData(transactionId, amount, note, ipnUrl, extraData, isShowResult)
|
|
204
|
+
onUi(action, ERROR_CANNOT_PRESENT) {
|
|
205
|
+
if (openData == null) {
|
|
206
|
+
sendError(action, ERROR_INVALID_PARAM, "Thiếu hoặc sai data cho action $action")
|
|
207
|
+
} else {
|
|
208
|
+
requirePayME(action, forOpen = true)?.openMiniApp(getTypeOpenMiniApp(type), openData)
|
|
169
209
|
}
|
|
170
|
-
val openMiniAppData = getOpenMiniAppData(action, phone, paymentData)
|
|
171
|
-
payMEMiniApp?.openMiniApp(
|
|
172
|
-
getTypeOpenMiniApp(type),
|
|
173
|
-
openMiniAppData
|
|
174
|
-
)
|
|
175
|
-
return
|
|
176
210
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Dựng data theo action; null khi action không hỗ trợ hoặc thiếu/sai field bắt buộc (như iOS). */
|
|
214
|
+
private fun buildOpenData(
|
|
215
|
+
action: String,
|
|
216
|
+
identityToken: String,
|
|
217
|
+
data: ReadableMap?
|
|
218
|
+
): OpenMiniAppDataInterface? {
|
|
219
|
+
val extraData = data?.optMap("extraData")?.toHashMap()
|
|
220
|
+
?.filterValues { it != null }?.mapValues { it.value!! }
|
|
221
|
+
val isShowResult = data?.optBoolean("isShowResult") ?: true
|
|
222
|
+
val isBackToApp = data?.optBoolean("isBackToApp") ?: true
|
|
223
|
+
return when (action) {
|
|
224
|
+
"PAYME" -> OpenMiniAppPayMEData()
|
|
225
|
+
"OPEN" -> OpenMiniAppOpenData(identityToken)
|
|
226
|
+
"KYC" -> OpenMiniAppKYCData(identityToken)
|
|
227
|
+
"PAY" -> {
|
|
228
|
+
val transactionId = data?.optString("transactionId") ?: return null
|
|
229
|
+
val amount = data.optInt("amount") ?: return null
|
|
230
|
+
OpenMiniAppPaymentData(
|
|
231
|
+
identityToken,
|
|
232
|
+
PaymentData(
|
|
233
|
+
transactionId, amount, data.optString("note") ?: "",
|
|
234
|
+
data.optString("ipnUrl") ?: "", extraData, isShowResult
|
|
235
|
+
)
|
|
236
|
+
)
|
|
187
237
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
return
|
|
194
|
-
}
|
|
195
|
-
if (action == "TRANSFER_QR") {
|
|
196
|
-
val amount = data?.getInt("amount")
|
|
197
|
-
val bankNumber = data?.getString("bankNumber")
|
|
198
|
-
val swiftCode = data?.getString("swiftCode")
|
|
199
|
-
val cardHolder = data?.getString("cardHolder")
|
|
200
|
-
val extraData =
|
|
201
|
-
if (data?.hasKey("extraData") == true) data.getMap("extraData")?.toHashMap()
|
|
202
|
-
?.filterValues { it != null }?.mapValues { it.value!! } else null
|
|
203
|
-
val partnerTransaction =
|
|
204
|
-
if (data?.hasKey("partnerTransaction") == true) data.getString("partnerTransaction") else ""
|
|
205
|
-
val note = if (data?.hasKey("note") == true) data.getString("note") else ""
|
|
206
|
-
val isShowResult =
|
|
207
|
-
if (data?.hasKey("isShowResult") == true) data.getBoolean("isShowResult") else true
|
|
208
|
-
var transferQRData: TransferQRData? = null
|
|
209
|
-
if (amount != null && bankNumber != null && swiftCode != null && cardHolder != null) {
|
|
210
|
-
transferQRData = TransferQRData(
|
|
211
|
-
amount,
|
|
212
|
-
bankNumber,
|
|
213
|
-
swiftCode,
|
|
214
|
-
cardHolder,
|
|
215
|
-
note,
|
|
216
|
-
partnerTransaction,
|
|
217
|
-
extraData,
|
|
218
|
-
isShowResult
|
|
238
|
+
"PAYMENT" -> {
|
|
239
|
+
val transaction = data?.optString("transaction") ?: return null
|
|
240
|
+
OpenMiniAppPaymentDirectData(
|
|
241
|
+
identityToken = identityToken.ifEmpty { null },
|
|
242
|
+
paymentDirectData = PaymentDirectData(transaction, extraData, isShowResult)
|
|
219
243
|
)
|
|
220
244
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
245
|
+
"TRANSFER_QR" -> {
|
|
246
|
+
val amount = data?.optInt("amount") ?: return null
|
|
247
|
+
val bankNumber = data.optString("bankNumber") ?: return null
|
|
248
|
+
val swiftCode = data.optString("swiftCode") ?: return null
|
|
249
|
+
val cardHolder = data.optString("cardHolder") ?: return null
|
|
250
|
+
OpenMiniAppTransferQRData(
|
|
251
|
+
identityToken,
|
|
252
|
+
TransferQRData(
|
|
253
|
+
amount, bankNumber, swiftCode, cardHolder,
|
|
254
|
+
data.optString("note") ?: "", data.optString("partnerTransaction") ?: "",
|
|
255
|
+
extraData, isShowResult
|
|
256
|
+
)
|
|
257
|
+
)
|
|
258
|
+
}
|
|
259
|
+
"SERVICE" -> {
|
|
260
|
+
val service = data?.optString("service") ?: return null
|
|
261
|
+
OpenMiniAppServiceData(identityToken, ServiceData(service, extraData, isBackToApp, isShowResult))
|
|
262
|
+
}
|
|
263
|
+
"DEPOSIT", "WITHDRAW", "TRANSFER" -> {
|
|
264
|
+
// Giữ hành vi 3.0.0: data tuỳ chọn ở native.
|
|
265
|
+
// amount tuỳ chọn, nhưng có mà không phải số nguyên (hoặc sai kiểu) thì INVALID_PARAM.
|
|
266
|
+
val amount = data?.optInt("amount")
|
|
267
|
+
if (amount == null && data != null && data.hasKey("amount") && !data.isNull("amount")) return null
|
|
268
|
+
val additionalData = DepositWithdrawTransferData(
|
|
269
|
+
data?.optString("description") ?: "", amount,
|
|
270
|
+
extraData, isBackToApp, isShowResult
|
|
271
|
+
)
|
|
272
|
+
when (action) {
|
|
273
|
+
"DEPOSIT" -> OpenMiniAppDepositData(identityToken, additionalData)
|
|
274
|
+
"WITHDRAW" -> OpenMiniAppWithdrawData(identityToken, additionalData)
|
|
275
|
+
else -> OpenMiniAppTransferData(identityToken, additionalData)
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
else -> null
|
|
235
279
|
}
|
|
236
280
|
}
|
|
237
281
|
|
|
282
|
+
private fun ReadableMap.has(key: String, type: ReadableType) =
|
|
283
|
+
hasKey(key) && !isNull(key) && getType(key) == type
|
|
284
|
+
|
|
285
|
+
private fun ReadableMap.optString(key: String): String? =
|
|
286
|
+
if (has(key, ReadableType.String)) getString(key) else null
|
|
287
|
+
|
|
288
|
+
// Chỉ nhận số nguyên (20000 hoặc 20000.0); số lẻ coi như sai tham số, không làm tròn.
|
|
289
|
+
private fun ReadableMap.optInt(key: String): Int? {
|
|
290
|
+
if (!has(key, ReadableType.Number)) return null
|
|
291
|
+
val value = getDouble(key)
|
|
292
|
+
return if (value % 1.0 == 0.0 && value in Int.MIN_VALUE.toDouble()..Int.MAX_VALUE.toDouble()) value.toInt() else null
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
private fun ReadableMap.optBoolean(key: String): Boolean? =
|
|
296
|
+
if (has(key, ReadableType.Boolean)) getBoolean(key) else null
|
|
297
|
+
|
|
298
|
+
private fun ReadableMap.optMap(key: String): ReadableMap? =
|
|
299
|
+
if (has(key, ReadableType.Map)) getMap(key) else null
|
|
300
|
+
|
|
238
301
|
private fun onResponse(actionOpenMiniApp: ActionOpenMiniApp, jsonObject: JSONObject?) {
|
|
239
302
|
if (jsonObject != null) {
|
|
240
303
|
try {
|
|
241
|
-
|
|
304
|
+
// Không log payload: có dữ liệu tài khoản.
|
|
242
305
|
val payload = JsonConvert.jsonToReact(jsonObject)
|
|
243
306
|
if (!payload.hasKey("action")) {
|
|
244
307
|
payload.putString("action", actionOpenMiniApp.toString())
|
|
@@ -248,16 +311,20 @@ class PaymeMiniAppModule(reactContext: ReactApplicationContext) :
|
|
|
248
311
|
"onResponse", payload
|
|
249
312
|
)
|
|
250
313
|
} catch (e: Exception) {
|
|
251
|
-
Log.
|
|
314
|
+
Log.w("PAYMELOG", "onResponse: không chuyển được payload (${e.javaClass.simpleName})")
|
|
252
315
|
}
|
|
253
316
|
}
|
|
254
317
|
}
|
|
255
318
|
|
|
256
319
|
private fun onError(actionOpenMiniApp: ActionOpenMiniApp, payMEError: PayMEError) {
|
|
320
|
+
sendError(actionOpenMiniApp.toString(), payMEError.code, payMEError.description)
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
private fun sendError(action: String, code: String?, description: String?) {
|
|
257
324
|
val payload = Arguments.createMap()
|
|
258
|
-
payload.putString("action",
|
|
259
|
-
payload.putString("code",
|
|
260
|
-
payload.putString("description",
|
|
325
|
+
payload.putString("action", action)
|
|
326
|
+
payload.putString("code", code)
|
|
327
|
+
payload.putString("description", description)
|
|
261
328
|
sendEvent(
|
|
262
329
|
reactApplicationContext,
|
|
263
330
|
"onError", payload
|
|
@@ -298,92 +365,9 @@ class PaymeMiniAppModule(reactContext: ReactApplicationContext) :
|
|
|
298
365
|
// return OpenMiniAppType.screen
|
|
299
366
|
}
|
|
300
367
|
|
|
301
|
-
private fun getActionOpenMiniApp(action: String): ActionOpenMiniApp {
|
|
302
|
-
return try {
|
|
303
|
-
ActionOpenMiniApp.valueOf(action.uppercase())
|
|
304
|
-
} catch (e: Exception) {
|
|
305
|
-
ActionOpenMiniApp.PAYME
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
private fun getOpenMiniAppData(
|
|
310
|
-
action: String,
|
|
311
|
-
phone: String,
|
|
312
|
-
): OpenMiniAppDataInterface {
|
|
313
|
-
if (getActionOpenMiniApp(action) == ActionOpenMiniApp.OPEN) {
|
|
314
|
-
return OpenMiniAppOpenData(phone)
|
|
315
|
-
}
|
|
316
|
-
if (getActionOpenMiniApp(action) == ActionOpenMiniApp.PAYME) {
|
|
317
|
-
return OpenMiniAppPayMEData()
|
|
318
|
-
}
|
|
319
|
-
if (getActionOpenMiniApp(action) == ActionOpenMiniApp.KYC) {
|
|
320
|
-
return OpenMiniAppKYCData(phone)
|
|
321
|
-
}
|
|
322
|
-
return OpenMiniAppPayMEData()
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
private fun getOpenMiniAppData(
|
|
326
|
-
action: String,
|
|
327
|
-
phone: String,
|
|
328
|
-
additionalData: DepositWithdrawTransferData? = null
|
|
329
|
-
): OpenMiniAppDataInterface {
|
|
330
|
-
if (getActionOpenMiniApp(action) == ActionOpenMiniApp.DEPOSIT && additionalData != null) {
|
|
331
|
-
return OpenMiniAppDepositData(phone, additionalData)
|
|
332
|
-
}
|
|
333
|
-
if (getActionOpenMiniApp(action) == ActionOpenMiniApp.WITHDRAW && additionalData != null) {
|
|
334
|
-
return OpenMiniAppWithdrawData(phone, additionalData)
|
|
335
|
-
}
|
|
336
|
-
if (getActionOpenMiniApp(action) == ActionOpenMiniApp.TRANSFER && additionalData != null) {
|
|
337
|
-
return OpenMiniAppTransferData(phone, additionalData)
|
|
338
|
-
}
|
|
339
|
-
return OpenMiniAppPayMEData()
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
private fun getOpenMiniAppData(
|
|
343
|
-
action: String,
|
|
344
|
-
phone: String,
|
|
345
|
-
paymentData: PaymentData? = null,
|
|
346
|
-
): OpenMiniAppDataInterface {
|
|
347
|
-
if (getActionOpenMiniApp(action) == ActionOpenMiniApp.PAY && paymentData != null) {
|
|
348
|
-
return OpenMiniAppPaymentData(phone, paymentData)
|
|
349
|
-
}
|
|
350
|
-
return OpenMiniAppPayMEData()
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
private fun getOpenMiniAppData(
|
|
354
|
-
action: String,
|
|
355
|
-
phone: String,
|
|
356
|
-
paymentData: PaymentDirectData? = null,
|
|
357
|
-
): OpenMiniAppDataInterface {
|
|
358
|
-
if (getActionOpenMiniApp(action) == ActionOpenMiniApp.PAYMENT && paymentData != null) {
|
|
359
|
-
return OpenMiniAppPaymentDirectData(phone, paymentData)
|
|
360
|
-
}
|
|
361
|
-
return OpenMiniAppPayMEData()
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
private fun getOpenMiniAppData(
|
|
365
|
-
action: String,
|
|
366
|
-
phone: String,
|
|
367
|
-
transferQRData: TransferQRData? = null,
|
|
368
|
-
): OpenMiniAppDataInterface {
|
|
369
|
-
if (getActionOpenMiniApp(action) == ActionOpenMiniApp.TRANSFER_QR && transferQRData != null) {
|
|
370
|
-
return OpenMiniAppTransferQRData(phone, transferQRData)
|
|
371
|
-
}
|
|
372
|
-
return OpenMiniAppPayMEData()
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
private fun getOpenMiniAppData(
|
|
376
|
-
action: String,
|
|
377
|
-
phone: String,
|
|
378
|
-
additionalData: ServiceData? = null
|
|
379
|
-
): OpenMiniAppDataInterface {
|
|
380
|
-
if (getActionOpenMiniApp(action) == ActionOpenMiniApp.SERVICE && additionalData != null) {
|
|
381
|
-
return OpenMiniAppServiceData(phone, additionalData)
|
|
382
|
-
}
|
|
383
|
-
return OpenMiniAppPayMEData()
|
|
384
|
-
}
|
|
385
|
-
|
|
386
368
|
private fun sendEvent(reactContext: ReactContext, eventName: String, params: WritableMap?) {
|
|
369
|
+
// Bỏ qua khi React instance đã huỷ (reload, thoát app): emit lúc đó lỗi/crash.
|
|
370
|
+
if (!reactContext.hasActiveReactInstance()) return
|
|
387
371
|
if (eventNames.contains(eventName)) {
|
|
388
372
|
reactContext
|
|
389
373
|
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
|
|
@@ -393,5 +377,12 @@ class PaymeMiniAppModule(reactContext: ReactApplicationContext) :
|
|
|
393
377
|
|
|
394
378
|
companion object {
|
|
395
379
|
const val NAME = "PaymeMiniApp"
|
|
380
|
+
// Mã lỗi do bridge tạo, trùng với iOS.
|
|
381
|
+
const val ERROR_NOT_INITIALIZED = "PAYME_NOT_INITIALIZED"
|
|
382
|
+
const val ERROR_INVALID_PARAM = "INVALID_PARAM"
|
|
383
|
+
// Không hiện được miniapp (trùng mã SDK 0.8.4).
|
|
384
|
+
const val ERROR_CANNOT_PRESENT = "MINIAPP_CANNOT_PRESENT"
|
|
385
|
+
// Lỗi ngoài dự kiến khi gọi SDK (chỉ Android).
|
|
386
|
+
const val ERROR_SDK = "PAYME_SDK_ERROR"
|
|
396
387
|
}
|
|
397
388
|
}
|
package/ios/PaymeMiniApp.mm
CHANGED
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
|
|
6
6
|
RCT_EXTERN_METHOD(initPayME:(NSString*)appId publicKey:(NSString)publicKey privateKey:(NSString)privateKey locale:(NSString)locale env:(NSString)env mode:(NSString)mode)
|
|
7
7
|
RCT_EXTERN_METHOD(open:(NSString*)type param:(NSDictionary)param)
|
|
8
|
-
RCT_EXTERN_METHOD(getBalance:(NSString*)
|
|
9
|
-
RCT_EXTERN_METHOD(getAccountInfo:(NSString*)
|
|
8
|
+
RCT_EXTERN_METHOD(getBalance:(NSString*)identityToken)
|
|
9
|
+
RCT_EXTERN_METHOD(getAccountInfo:(NSString*)identityToken)
|
|
10
10
|
RCT_EXTERN_METHOD(setLanguage:(NSString*)language)
|
|
11
11
|
RCT_EXTERN_METHOD(closeMiniApp)
|
|
12
12
|
|