taiwan-payment-skill 1.5.1 → 1.5.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.
|
@@ -136,7 +136,7 @@ class PAYUNiPaymentService:
|
|
|
136
136
|
data: 交易資料字典
|
|
137
137
|
|
|
138
138
|
Returns:
|
|
139
|
-
str: AES-GCM 加密後的 hex 字串 (
|
|
139
|
+
str: AES-GCM 加密後的 hex 字串 (base64(密文) + ":::" + base64(tag))
|
|
140
140
|
|
|
141
141
|
Example:
|
|
142
142
|
>>> data = {'MerID': 'MS123', 'TradeAmt': 100}
|
|
@@ -144,6 +144,7 @@ class PAYUNiPaymentService:
|
|
|
144
144
|
>>> len(encrypted) > 0
|
|
145
145
|
True
|
|
146
146
|
"""
|
|
147
|
+
import base64
|
|
147
148
|
# 步驟 1: 轉換為查詢字串
|
|
148
149
|
query_string = urllib.parse.urlencode(data)
|
|
149
150
|
|
|
@@ -151,15 +152,17 @@ class PAYUNiPaymentService:
|
|
|
151
152
|
cipher = AES.new(self.hash_key, AES.MODE_GCM, nonce=self.hash_iv)
|
|
152
153
|
encrypted, tag = cipher.encrypt_and_digest(query_string.encode('utf-8'))
|
|
153
154
|
|
|
154
|
-
# 步驟 3:
|
|
155
|
-
|
|
155
|
+
# 步驟 3: 分別 base64 編碼,用 ":::" 分隔
|
|
156
|
+
encrypted_b64 = base64.b64encode(encrypted).decode('utf-8')
|
|
157
|
+
tag_b64 = base64.b64encode(tag).decode('utf-8')
|
|
158
|
+
return encrypted_b64 + ':::' + tag_b64
|
|
156
159
|
|
|
157
160
|
def decrypt_data(self, encrypted_data: str) -> Dict[str, any]:
|
|
158
161
|
"""
|
|
159
162
|
解密資料 (AES-256-GCM)
|
|
160
163
|
|
|
161
164
|
Args:
|
|
162
|
-
encrypted_data: AES-GCM
|
|
165
|
+
encrypted_data: AES-GCM 加密的字串 (base64(密文) + ":::" + base64(tag))
|
|
163
166
|
|
|
164
167
|
Returns:
|
|
165
168
|
Dict: 解密後的資料字典
|
|
@@ -168,12 +171,17 @@ class PAYUNiPaymentService:
|
|
|
168
171
|
ValueError: 解密失敗或驗證失敗
|
|
169
172
|
"""
|
|
170
173
|
try:
|
|
171
|
-
|
|
172
|
-
|
|
174
|
+
import base64
|
|
175
|
+
# 步驟 1: 分離 base64 編碼的密文和 tag
|
|
176
|
+
parts = encrypted_data.split(':::')
|
|
177
|
+
if len(parts) != 2:
|
|
178
|
+
raise ValueError('格式錯誤: 缺少 ":::" 分隔符')
|
|
173
179
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
180
|
+
encrypted_b64, tag_b64 = parts
|
|
181
|
+
|
|
182
|
+
# 步驟 2: Base64 解碼
|
|
183
|
+
encrypted = base64.b64decode(encrypted_b64)
|
|
184
|
+
tag = base64.b64decode(tag_b64)
|
|
177
185
|
|
|
178
186
|
# 步驟 3: AES-256-GCM 解密並驗證
|
|
179
187
|
decipher = AES.new(self.hash_key, AES.MODE_GCM, nonce=self.hash_iv)
|
|
@@ -204,8 +212,8 @@ class PAYUNiPaymentService:
|
|
|
204
212
|
>>> len(checksum)
|
|
205
213
|
64
|
|
206
214
|
"""
|
|
207
|
-
# 組合字串:
|
|
208
|
-
raw =
|
|
215
|
+
# 組合字串: HashKey + EncryptInfo + HashIV (Key 在前)
|
|
216
|
+
raw = self.hash_key.decode('utf-8') + encrypt_info + self.hash_iv.decode('utf-8')
|
|
209
217
|
|
|
210
218
|
# SHA256 雜湊並轉大寫
|
|
211
219
|
return hashlib.sha256(raw.encode('utf-8')).hexdigest().upper()
|
|
@@ -157,8 +157,8 @@ class PayuniEncryption
|
|
|
157
157
|
16
|
|
158
158
|
);
|
|
159
159
|
|
|
160
|
-
// 3. 組合加密結果 (
|
|
161
|
-
$encryptInfo =
|
|
160
|
+
// 3. 組合加密結果 (base64(密文) + ":::" + base64(tag))
|
|
161
|
+
$encryptInfo = base64_encode($encrypted) . ':::' . base64_encode($tag);
|
|
162
162
|
|
|
163
163
|
return $encryptInfo;
|
|
164
164
|
}
|
|
@@ -168,12 +168,15 @@ class PayuniEncryption
|
|
|
168
168
|
*/
|
|
169
169
|
public function decrypt(string $encryptInfo): array
|
|
170
170
|
{
|
|
171
|
-
// 1.
|
|
172
|
-
$
|
|
171
|
+
// 1. 分離 base64 編碼的密文和 tag
|
|
172
|
+
$parts = explode(':::', $encryptInfo);
|
|
173
|
+
if (count($parts) !== 2) {
|
|
174
|
+
throw new Exception('格式錯誤: 缺少 ":::" 分隔符');
|
|
175
|
+
}
|
|
173
176
|
|
|
174
|
-
// 2.
|
|
175
|
-
$encrypted =
|
|
176
|
-
$tag =
|
|
177
|
+
// 2. Base64 解碼
|
|
178
|
+
$encrypted = base64_decode($parts[0]);
|
|
179
|
+
$tag = base64_decode($parts[1]);
|
|
177
180
|
|
|
178
181
|
// 3. AES-256-GCM 解密
|
|
179
182
|
$decrypted = openssl_decrypt(
|
|
@@ -193,10 +196,11 @@ class PayuniEncryption
|
|
|
193
196
|
|
|
194
197
|
/**
|
|
195
198
|
* 產生 HashInfo (SHA256)
|
|
199
|
+
* 注意: Key 必須在前面 (HashKey + EncryptInfo + HashIV)
|
|
196
200
|
*/
|
|
197
201
|
public function hashInfo(string $encryptInfo): string
|
|
198
202
|
{
|
|
199
|
-
$raw = $
|
|
203
|
+
$raw = $this->merKey . $encryptInfo . $this->merIV;
|
|
200
204
|
return strtoupper(hash('sha256', $raw));
|
|
201
205
|
}
|
|
202
206
|
}
|
|
@@ -219,6 +223,7 @@ class PayuniEncryption:
|
|
|
219
223
|
|
|
220
224
|
def encrypt(self, params: dict) -> str:
|
|
221
225
|
"""AES-256-GCM 加密"""
|
|
226
|
+
import base64
|
|
222
227
|
# 1. 組合 Query String
|
|
223
228
|
query_string = urlencode(params)
|
|
224
229
|
|
|
@@ -226,19 +231,24 @@ class PayuniEncryption:
|
|
|
226
231
|
cipher = AES.new(self.mer_key, AES.MODE_GCM, nonce=self.mer_iv)
|
|
227
232
|
encrypted, tag = cipher.encrypt_and_digest(query_string.encode('utf-8'))
|
|
228
233
|
|
|
229
|
-
# 3. 組合加密結果
|
|
230
|
-
|
|
234
|
+
# 3. 組合加密結果 (base64(密文) + ":::" + base64(tag))
|
|
235
|
+
encrypted_b64 = base64.b64encode(encrypted).decode('utf-8')
|
|
236
|
+
tag_b64 = base64.b64encode(tag).decode('utf-8')
|
|
237
|
+
encrypt_info = encrypted_b64 + ':::' + tag_b64
|
|
231
238
|
|
|
232
239
|
return encrypt_info
|
|
233
240
|
|
|
234
241
|
def decrypt(self, encrypt_info: str) -> dict:
|
|
235
242
|
"""AES-256-GCM 解密"""
|
|
236
|
-
|
|
237
|
-
|
|
243
|
+
import base64
|
|
244
|
+
# 1. 分離 base64 編碼的密文和 tag
|
|
245
|
+
parts = encrypt_info.split(':::')
|
|
246
|
+
if len(parts) != 2:
|
|
247
|
+
raise ValueError('格式錯誤: 缺少 ":::" 分隔符')
|
|
238
248
|
|
|
239
|
-
# 2.
|
|
240
|
-
encrypted =
|
|
241
|
-
tag =
|
|
249
|
+
# 2. Base64 解碼
|
|
250
|
+
encrypted = base64.b64decode(parts[0])
|
|
251
|
+
tag = base64.b64decode(parts[1])
|
|
242
252
|
|
|
243
253
|
# 3. AES-256-GCM 解密
|
|
244
254
|
cipher = AES.new(self.mer_key, AES.MODE_GCM, nonce=self.mer_iv)
|
|
@@ -249,8 +259,8 @@ class PayuniEncryption:
|
|
|
249
259
|
return {k: v[0] for k, v in result.items()}
|
|
250
260
|
|
|
251
261
|
def hash_info(self, encrypt_info: str) -> str:
|
|
252
|
-
"""產生 HashInfo (SHA256)"""
|
|
253
|
-
raw =
|
|
262
|
+
"""產生 HashInfo (SHA256) - 注意: Key 必須在前面"""
|
|
263
|
+
raw = self.mer_key.decode() + encrypt_info + self.mer_iv.decode()
|
|
254
264
|
return hashlib.sha256(raw.encode('utf-8')).hexdigest().upper()
|
|
255
265
|
```
|
|
256
266
|
|
|
@@ -929,7 +939,7 @@ echo 'SUCCESS';
|
|
|
929
939
|
**檢查項目**:
|
|
930
940
|
1. Hash Key 和 Hash IV 是否正確
|
|
931
941
|
2. AES-256-GCM 加密是否正確實作
|
|
932
|
-
3. SHA256
|
|
942
|
+
3. **⚠️ SHA256 計算順序**: 必須是 `SHA256(HashKey + EncryptInfo + HashIV)` 而非 `SHA256(EncryptInfo + HashKey + HashIV)` — **Key 必須在前**
|
|
933
943
|
4. 測試/正式環境金鑰是否混用
|
|
934
944
|
|
|
935
945
|
### 訂單編號重複
|
|
@@ -960,7 +970,7 @@ order_id = f"ORD{int(time.time())}{random.randint(100, 999)}"
|
|
|
960
970
|
**檢查項目**:
|
|
961
971
|
1. AES-256-GCM 參數是否正確 (Key 32 bytes, IV 16 bytes)
|
|
962
972
|
2. Tag 長度是否為 16 bytes
|
|
963
|
-
3.
|
|
973
|
+
3. **⚠️ EncryptInfo 格式**: 必須是 `base64(密文) + ":::" + base64(Tag)` 而非 `hex(密文 + tag)` — 密文和 tag **分別 base64 編碼並用 ":::" 分隔**
|
|
964
974
|
|
|
965
975
|
---
|
|
966
976
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "taiwan-payment-skill",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.2",
|
|
4
4
|
"description": "AI-powered Taiwan Payment Gateway toolkit. Supports 10 providers: ECPay, NewebPay, PAYUNi, SmilePay, PChomePay, ezPay, PayNow, Shopline Payments, LINE Pay v4, and TapPay.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"taiwan",
|