payme-mini-app 3.0.0 → 3.0.2
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 +41 -0
- package/android/build.gradle +8 -1
- package/android/src/main/java/com/paymeminiapp/PaymeMiniAppModule.kt +212 -222
- package/ios/PaymeMiniApp.swift +149 -38
- package/lib/module/index.js +63 -8
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/src/index.d.ts +27 -2
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/index.type.d.ts +24 -7
- 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 +110 -14
- package/src/index.type.ts +27 -6
package/README.md
CHANGED
|
@@ -1,4 +1,45 @@
|
|
|
1
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.2: sửa lỗi build Android (tương thích 3.0.1)
|
|
3
|
+
|
|
4
|
+
- Android: module tự áp plugin Kotlin khi ứng dụng đặt `android.builtInKotlin=false` (template React Native 0.88). Trước đây build lỗi `cannot find symbol PaymeMiniAppPackage`.
|
|
5
|
+
- Dùng PayME Miniapp SDK Android `0.8.5`: manifest của SDK chỉ đặt `networkSecurityConfig` (tên `payme_miniapp_network_security_config`, cleartext tới `localhost`) lên `<application>`. Ứng dụng có `networkSecurityConfig` riêng: thêm `tools:replace="android:networkSecurityConfig"` và cho phép cleartext tới `localhost`.
|
|
6
|
+
- Bản debug trên emulator tải JS bundle từ `10.0.2.2`: thêm `android/app/src/debug/res/xml/payme_miniapp_network_security_config.xml` cho phép cleartext tới `localhost` và `10.0.2.2`.
|
|
7
|
+
|
|
8
|
+
## 3.0.1: sửa lỗi bridge (tương thích 3.0.0)
|
|
9
|
+
|
|
10
|
+
- 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.
|
|
11
|
+
- `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ị đó.
|
|
12
|
+
- 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).
|
|
13
|
+
- Export type: `PayMEOpeningParam`, `PayMEEnv`, `PayMEMode`, `PayMEOpeningType`, `PayMELocale`, các type `data` (`PayMEPaymentParam`, `PayMEPaymentDirectParam`, `PayMETransferQRParam`, `PayMEServiceParam`, `PayMEPayParam`) và payload sự kiện `PayMEErrorEvent`, `PayMEResponseEvent`.
|
|
14
|
+
- `ipnUrl` của `PAY` là tuỳ chọn.
|
|
15
|
+
- Lắng nghe sự kiện có kiểu (vẫn dùng được `PayMEMiniAppEvents`):
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
const sub = addErrorListener((e) => console.log(e.action, e.code, e.description));
|
|
19
|
+
const sub2 = addResponseListener((r) => console.log(r.action));
|
|
20
|
+
// huỷ: sub.remove(); sub2.remove();
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
- Mã lỗi do bridge tạo, giống nhau trên iOS và Android (`PayMEErrorCode`):
|
|
24
|
+
- `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.
|
|
25
|
+
- `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).
|
|
26
|
+
- `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ì.
|
|
27
|
+
- Mã lỗi SDK cũng có trong `PayMEErrorCode`:
|
|
28
|
+
- `USER_CANCEL`: người dùng đóng miniapp; trên iOS cả khi app gọi `close()` (Android: `close()` không báo).
|
|
29
|
+
- `SYSTEM_CANCEL` (iOS): SDK đóng miniapp vì không khởi động được (vd web server lỗi).
|
|
30
|
+
- `MINIAPP_ALREADY_OPEN`: `open` khi đang có miniapp mở.
|
|
31
|
+
- `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).
|
|
32
|
+
- `amount` phải là số nguyên (`20000` hoặc `20000.0`), không làm tròn số lẻ.
|
|
33
|
+
- Truyền sai kiểu từ JS (`null`, số thay cho chuỗi...) không còn làm crash app.
|
|
34
|
+
- `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.
|
|
35
|
+
- `open` nhận `type` không phân biệt hoa thường (`'Modal'` → `modal`) trên cả iOS và Android.
|
|
36
|
+
- 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.
|
|
37
|
+
- Android: `getBalance`, `getAccountInfo` chạy được khi chưa có Activity (headless/nền), như iOS; `open` vẫn cần Activity.
|
|
38
|
+
- 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`).
|
|
39
|
+
- 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.
|
|
40
|
+
- 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ở.
|
|
41
|
+
- Nên đăng ký `addErrorListener`/`addResponseListener` trước khi gọi `init`/`open`: event gửi khi chưa có listener sẽ bị bỏ.
|
|
42
|
+
|
|
2
43
|
## 3.0.0: identityToken thay cho phone (không tương thích với 2.x)
|
|
3
44
|
|
|
4
45
|
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.
|
package/android/build.gradle
CHANGED
|
@@ -10,11 +10,18 @@ buildscript {
|
|
|
10
10
|
|
|
11
11
|
dependencies {
|
|
12
12
|
classpath "com.android.tools.build:gradle:9.1.1"
|
|
13
|
+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${getExtOrDefault('kotlinVersion')}"
|
|
13
14
|
}
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
apply plugin: "com.android.library"
|
|
17
18
|
|
|
19
|
+
// Mã của module là Kotlin. AGP 9 tự biên dịch Kotlin (built-in Kotlin); ứng dụng tắt tính năng này
|
|
20
|
+
// (android.builtInKotlin=false, template React Native 0.88) thì module phải tự áp plugin Kotlin.
|
|
21
|
+
if (project.findProperty("android.builtInKotlin")?.toString()?.toBoolean() == false) {
|
|
22
|
+
apply plugin: "org.jetbrains.kotlin.android"
|
|
23
|
+
}
|
|
24
|
+
|
|
18
25
|
def getExtOrIntegerDefault(name) {
|
|
19
26
|
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["PaymeMiniApp_" + name]).toInteger()
|
|
20
27
|
}
|
|
@@ -73,5 +80,5 @@ def kotlin_version = getExtOrDefault("kotlinVersion")
|
|
|
73
80
|
dependencies {
|
|
74
81
|
implementation "com.facebook.react:react-android"
|
|
75
82
|
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
|
76
|
-
implementation 'com.github.PayME-Miniapp:native-android:0.8.
|
|
83
|
+
implementation 'com.github.PayME-Miniapp:native-android:0.8.5'
|
|
77
84
|
}
|
|
@@ -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,174 +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
|
+
}
|
|
117
|
+
|
|
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
|
+
}
|
|
141
|
+
}
|
|
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
|
+
}
|
|
76
156
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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")
|
|
81
163
|
}
|
|
82
|
-
|
|
83
|
-
payMEMiniApp?.setMode(mode)
|
|
84
|
-
payMEMiniApp?.setUpListener(onResponse = { actionOpenMiniApp: ActionOpenMiniApp, jsonObject: JSONObject? ->
|
|
85
|
-
onResponse(actionOpenMiniApp, jsonObject)
|
|
86
|
-
},
|
|
87
|
-
onError = { actionOpenMiniApp: ActionOpenMiniApp, payMEError: PayMEError ->
|
|
88
|
-
onError(actionOpenMiniApp, payMEError)
|
|
89
|
-
})
|
|
164
|
+
return sdk
|
|
90
165
|
}
|
|
91
166
|
|
|
92
167
|
@ReactMethod
|
|
93
168
|
fun getBalance(identityToken: String) {
|
|
94
|
-
|
|
169
|
+
val action = ActionOpenMiniApp.GET_BALANCE.name
|
|
170
|
+
onUi(action) { requirePayME(action)?.getBalance(identityToken) }
|
|
95
171
|
}
|
|
96
172
|
|
|
97
173
|
@ReactMethod
|
|
98
174
|
fun getAccountInfo(identityToken: String) {
|
|
99
|
-
|
|
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.
|
|
195
|
+
val action = openingParam.optString("action") ?: ""
|
|
116
196
|
// identityToken do BE PayME ký; chỉ giữ trong bộ nhớ, không log.
|
|
117
|
-
val identityToken =
|
|
118
|
-
val data = openingParam.
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
val extraData =
|
|
124
|
-
if (data?.hasKey("extraData") == true) data.getMap("extraData")?.toHashMap()
|
|
125
|
-
?.filterValues { it != null }?.mapValues { it.value!! } else null
|
|
126
|
-
val isBackToApp =
|
|
127
|
-
if (data?.hasKey("isBackToApp") == true) data.getBoolean("isBackToApp") else true
|
|
128
|
-
val isShowResult =
|
|
129
|
-
if (data?.hasKey("isShowResult") == true) data.getBoolean("isShowResult") else true
|
|
130
|
-
val amount = if (data?.hasKey("amount") == true) data.getInt("amount") else null
|
|
131
|
-
val additionalData =
|
|
132
|
-
DepositWithdrawTransferData(description, amount, extraData, isBackToApp, isShowResult)
|
|
133
|
-
val openMiniAppData = getOpenMiniAppData(action, identityToken, additionalData)
|
|
134
|
-
payMEMiniApp?.openMiniApp(
|
|
135
|
-
getTypeOpenMiniApp(type),
|
|
136
|
-
openMiniAppData
|
|
137
|
-
)
|
|
138
|
-
return
|
|
139
|
-
}
|
|
140
|
-
if (action == "SERVICE") {
|
|
141
|
-
val service = data?.getString("service") ?: ""
|
|
142
|
-
val extraData =
|
|
143
|
-
if (data?.hasKey("extraData") == true) data.getMap("extraData")?.toHashMap()
|
|
144
|
-
?.filterValues { it != null }?.mapValues { it.value!! } else null
|
|
145
|
-
val isBackToApp =
|
|
146
|
-
if (data?.hasKey("isBackToApp") == true) data.getBoolean("isBackToApp") else true
|
|
147
|
-
val isShowResult =
|
|
148
|
-
if (data?.hasKey("isShowResult") == true) data.getBoolean("isShowResult") else true
|
|
149
|
-
val additionalData = ServiceData(service, extraData, isBackToApp, isShowResult)
|
|
150
|
-
val openMiniAppData = getOpenMiniAppData(action, identityToken, additionalData)
|
|
151
|
-
payMEMiniApp?.openMiniApp(
|
|
152
|
-
getTypeOpenMiniApp(type),
|
|
153
|
-
openMiniAppData
|
|
154
|
-
)
|
|
155
|
-
return
|
|
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
|
|
156
203
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
val extraData =
|
|
163
|
-
if (data?.hasKey("extraData") == true) data.getMap("extraData")?.toHashMap()
|
|
164
|
-
?.filterValues { it != null }?.mapValues { it.value!! } else null
|
|
165
|
-
val isShowResult =
|
|
166
|
-
if (data?.hasKey("isShowResult") == true) data.getBoolean("isShowResult") else true
|
|
167
|
-
var paymentData: PaymentData? = null
|
|
168
|
-
if (amount != null && transactionId != null) {
|
|
169
|
-
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)
|
|
170
209
|
}
|
|
171
|
-
val openMiniAppData = getOpenMiniAppData(action, identityToken, paymentData)
|
|
172
|
-
payMEMiniApp?.openMiniApp(
|
|
173
|
-
getTypeOpenMiniApp(type),
|
|
174
|
-
openMiniAppData
|
|
175
|
-
)
|
|
176
|
-
return
|
|
177
210
|
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
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
|
+
)
|
|
188
237
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
return
|
|
195
|
-
}
|
|
196
|
-
if (action == "TRANSFER_QR") {
|
|
197
|
-
val amount = data?.getInt("amount")
|
|
198
|
-
val bankNumber = data?.getString("bankNumber")
|
|
199
|
-
val swiftCode = data?.getString("swiftCode")
|
|
200
|
-
val cardHolder = data?.getString("cardHolder")
|
|
201
|
-
val extraData =
|
|
202
|
-
if (data?.hasKey("extraData") == true) data.getMap("extraData")?.toHashMap()
|
|
203
|
-
?.filterValues { it != null }?.mapValues { it.value!! } else null
|
|
204
|
-
val partnerTransaction =
|
|
205
|
-
if (data?.hasKey("partnerTransaction") == true) data.getString("partnerTransaction") else ""
|
|
206
|
-
val note = if (data?.hasKey("note") == true) data.getString("note") else ""
|
|
207
|
-
val isShowResult =
|
|
208
|
-
if (data?.hasKey("isShowResult") == true) data.getBoolean("isShowResult") else true
|
|
209
|
-
var transferQRData: TransferQRData? = null
|
|
210
|
-
if (amount != null && bankNumber != null && swiftCode != null && cardHolder != null) {
|
|
211
|
-
transferQRData = TransferQRData(
|
|
212
|
-
amount,
|
|
213
|
-
bankNumber,
|
|
214
|
-
swiftCode,
|
|
215
|
-
cardHolder,
|
|
216
|
-
note,
|
|
217
|
-
partnerTransaction,
|
|
218
|
-
extraData,
|
|
219
|
-
isShowResult
|
|
238
|
+
"PAYMENT" -> {
|
|
239
|
+
val transaction = data?.optString("transaction") ?: return null
|
|
240
|
+
OpenMiniAppPaymentDirectData(
|
|
241
|
+
identityToken = identityToken.ifEmpty { null },
|
|
242
|
+
paymentDirectData = PaymentDirectData(transaction, extraData, isShowResult)
|
|
220
243
|
)
|
|
221
244
|
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
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
|
|
236
279
|
}
|
|
237
280
|
}
|
|
238
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
|
+
|
|
239
301
|
private fun onResponse(actionOpenMiniApp: ActionOpenMiniApp, jsonObject: JSONObject?) {
|
|
240
302
|
if (jsonObject != null) {
|
|
241
303
|
try {
|
|
242
|
-
|
|
304
|
+
// Không log payload: có dữ liệu tài khoản.
|
|
243
305
|
val payload = JsonConvert.jsonToReact(jsonObject)
|
|
244
306
|
if (!payload.hasKey("action")) {
|
|
245
307
|
payload.putString("action", actionOpenMiniApp.toString())
|
|
@@ -249,16 +311,20 @@ class PaymeMiniAppModule(reactContext: ReactApplicationContext) :
|
|
|
249
311
|
"onResponse", payload
|
|
250
312
|
)
|
|
251
313
|
} catch (e: Exception) {
|
|
252
|
-
Log.
|
|
314
|
+
Log.w("PAYMELOG", "onResponse: không chuyển được payload (${e.javaClass.simpleName})")
|
|
253
315
|
}
|
|
254
316
|
}
|
|
255
317
|
}
|
|
256
318
|
|
|
257
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?) {
|
|
258
324
|
val payload = Arguments.createMap()
|
|
259
|
-
payload.putString("action",
|
|
260
|
-
payload.putString("code",
|
|
261
|
-
payload.putString("description",
|
|
325
|
+
payload.putString("action", action)
|
|
326
|
+
payload.putString("code", code)
|
|
327
|
+
payload.putString("description", description)
|
|
262
328
|
sendEvent(
|
|
263
329
|
reactApplicationContext,
|
|
264
330
|
"onError", payload
|
|
@@ -299,92 +365,9 @@ class PaymeMiniAppModule(reactContext: ReactApplicationContext) :
|
|
|
299
365
|
// return OpenMiniAppType.screen
|
|
300
366
|
}
|
|
301
367
|
|
|
302
|
-
private fun getActionOpenMiniApp(action: String): ActionOpenMiniApp {
|
|
303
|
-
return try {
|
|
304
|
-
ActionOpenMiniApp.valueOf(action.uppercase())
|
|
305
|
-
} catch (e: Exception) {
|
|
306
|
-
ActionOpenMiniApp.PAYME
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
private fun getOpenMiniAppData(
|
|
311
|
-
action: String,
|
|
312
|
-
identityToken: String,
|
|
313
|
-
): OpenMiniAppDataInterface {
|
|
314
|
-
if (getActionOpenMiniApp(action) == ActionOpenMiniApp.OPEN) {
|
|
315
|
-
return OpenMiniAppOpenData(identityToken)
|
|
316
|
-
}
|
|
317
|
-
if (getActionOpenMiniApp(action) == ActionOpenMiniApp.PAYME) {
|
|
318
|
-
return OpenMiniAppPayMEData()
|
|
319
|
-
}
|
|
320
|
-
if (getActionOpenMiniApp(action) == ActionOpenMiniApp.KYC) {
|
|
321
|
-
return OpenMiniAppKYCData(identityToken)
|
|
322
|
-
}
|
|
323
|
-
return OpenMiniAppPayMEData()
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
private fun getOpenMiniAppData(
|
|
327
|
-
action: String,
|
|
328
|
-
identityToken: String,
|
|
329
|
-
additionalData: DepositWithdrawTransferData? = null
|
|
330
|
-
): OpenMiniAppDataInterface {
|
|
331
|
-
if (getActionOpenMiniApp(action) == ActionOpenMiniApp.DEPOSIT && additionalData != null) {
|
|
332
|
-
return OpenMiniAppDepositData(identityToken, additionalData)
|
|
333
|
-
}
|
|
334
|
-
if (getActionOpenMiniApp(action) == ActionOpenMiniApp.WITHDRAW && additionalData != null) {
|
|
335
|
-
return OpenMiniAppWithdrawData(identityToken, additionalData)
|
|
336
|
-
}
|
|
337
|
-
if (getActionOpenMiniApp(action) == ActionOpenMiniApp.TRANSFER && additionalData != null) {
|
|
338
|
-
return OpenMiniAppTransferData(identityToken, additionalData)
|
|
339
|
-
}
|
|
340
|
-
return OpenMiniAppPayMEData()
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
private fun getOpenMiniAppData(
|
|
344
|
-
action: String,
|
|
345
|
-
identityToken: String,
|
|
346
|
-
paymentData: PaymentData? = null,
|
|
347
|
-
): OpenMiniAppDataInterface {
|
|
348
|
-
if (getActionOpenMiniApp(action) == ActionOpenMiniApp.PAY && paymentData != null) {
|
|
349
|
-
return OpenMiniAppPaymentData(identityToken, paymentData)
|
|
350
|
-
}
|
|
351
|
-
return OpenMiniAppPayMEData()
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
private fun getOpenMiniAppData(
|
|
355
|
-
action: String,
|
|
356
|
-
identityToken: String,
|
|
357
|
-
paymentData: PaymentDirectData? = null,
|
|
358
|
-
): OpenMiniAppDataInterface {
|
|
359
|
-
if (getActionOpenMiniApp(action) == ActionOpenMiniApp.PAYMENT && paymentData != null) {
|
|
360
|
-
return OpenMiniAppPaymentDirectData(identityToken = identityToken.ifEmpty { null }, paymentDirectData = paymentData)
|
|
361
|
-
}
|
|
362
|
-
return OpenMiniAppPayMEData()
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
private fun getOpenMiniAppData(
|
|
366
|
-
action: String,
|
|
367
|
-
identityToken: String,
|
|
368
|
-
transferQRData: TransferQRData? = null,
|
|
369
|
-
): OpenMiniAppDataInterface {
|
|
370
|
-
if (getActionOpenMiniApp(action) == ActionOpenMiniApp.TRANSFER_QR && transferQRData != null) {
|
|
371
|
-
return OpenMiniAppTransferQRData(identityToken, transferQRData)
|
|
372
|
-
}
|
|
373
|
-
return OpenMiniAppPayMEData()
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
private fun getOpenMiniAppData(
|
|
377
|
-
action: String,
|
|
378
|
-
identityToken: String,
|
|
379
|
-
additionalData: ServiceData? = null
|
|
380
|
-
): OpenMiniAppDataInterface {
|
|
381
|
-
if (getActionOpenMiniApp(action) == ActionOpenMiniApp.SERVICE && additionalData != null) {
|
|
382
|
-
return OpenMiniAppServiceData(identityToken, additionalData)
|
|
383
|
-
}
|
|
384
|
-
return OpenMiniAppPayMEData()
|
|
385
|
-
}
|
|
386
|
-
|
|
387
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
|
|
388
371
|
if (eventNames.contains(eventName)) {
|
|
389
372
|
reactContext
|
|
390
373
|
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
|
|
@@ -394,5 +377,12 @@ class PaymeMiniAppModule(reactContext: ReactApplicationContext) :
|
|
|
394
377
|
|
|
395
378
|
companion object {
|
|
396
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"
|
|
397
387
|
}
|
|
398
388
|
}
|