taiwan_payments 0.1.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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +24 -0
- data/LICENSE.txt +21 -0
- data/README.ja.md +293 -0
- data/README.md +291 -0
- data/README.zh-TW.md +293 -0
- data/docs/ecpay-invoices.md +202 -0
- data/docs/ecpay-payments.md +191 -0
- data/docs/glossary/concepts.ja.md +33 -0
- data/docs/glossary/concepts.md +34 -0
- data/docs/glossary/ecpay-invoice.ja.md +113 -0
- data/docs/glossary/ecpay-invoice.md +114 -0
- data/docs/glossary/ecpay-payment.ja.md +64 -0
- data/docs/glossary/ecpay-payment.md +65 -0
- data/docs/rails.md +184 -0
- data/docs/sandbox-setup.md +104 -0
- data/docs/taiwan-payments-101.ja.md +138 -0
- data/docs/taiwan-payments-101.md +138 -0
- data/docs/taiwan-payments-101.zh-TW.md +138 -0
- data/docs/testing.md +140 -0
- data/docs/webhooks.md +113 -0
- data/exe/taiwan_payments +6 -0
- data/lib/generators/taiwan_payments/install/install_generator.rb +41 -0
- data/lib/generators/taiwan_payments/install/templates/ecpay_controller.rb.tt +26 -0
- data/lib/generators/taiwan_payments/install/templates/initializer.rb.tt +38 -0
- data/lib/taiwan_payments/cli.rb +271 -0
- data/lib/taiwan_payments/configuration.rb +86 -0
- data/lib/taiwan_payments/credentials.rb +27 -0
- data/lib/taiwan_payments/decimal.rb +36 -0
- data/lib/taiwan_payments/ecpay/aes_cipher.rb +36 -0
- data/lib/taiwan_payments/ecpay/check_mac_value.rb +63 -0
- data/lib/taiwan_payments/ecpay/checkout.rb +154 -0
- data/lib/taiwan_payments/ecpay/invoice_client.rb +253 -0
- data/lib/taiwan_payments/ecpay/invoice_mapper.rb +222 -0
- data/lib/taiwan_payments/ecpay/payment_client.rb +188 -0
- data/lib/taiwan_payments/ecpay/payment_methods.rb +40 -0
- data/lib/taiwan_payments/ecpay/payment_notification.rb +127 -0
- data/lib/taiwan_payments/ecpay/response_body.rb +30 -0
- data/lib/taiwan_payments/ecpay/trade_info.rb +68 -0
- data/lib/taiwan_payments/ecpay.rb +19 -0
- data/lib/taiwan_payments/environment.rb +22 -0
- data/lib/taiwan_payments/error_codes/ecpay_invoice.yml +80 -0
- data/lib/taiwan_payments/error_codes/ecpay_payment.yml +59 -0
- data/lib/taiwan_payments/error_codes.rb +23 -0
- data/lib/taiwan_payments/errors.rb +71 -0
- data/lib/taiwan_payments/form_helper.rb +21 -0
- data/lib/taiwan_payments/glossary/concepts.yml +122 -0
- data/lib/taiwan_payments/glossary/ecpay_invoice.yml +523 -0
- data/lib/taiwan_payments/glossary/ecpay_payment.yml +278 -0
- data/lib/taiwan_payments/glossary.rb +106 -0
- data/lib/taiwan_payments/hosted_form.rb +46 -0
- data/lib/taiwan_payments/http_client.rb +62 -0
- data/lib/taiwan_payments/instrumentation.rb +42 -0
- data/lib/taiwan_payments/invoice.rb +235 -0
- data/lib/taiwan_payments/money.rb +32 -0
- data/lib/taiwan_payments/php_compat.rb +39 -0
- data/lib/taiwan_payments/railtie.rb +23 -0
- data/lib/taiwan_payments/redactor.rb +29 -0
- data/lib/taiwan_payments/taipei_time.rb +42 -0
- data/lib/taiwan_payments/tax_id.rb +26 -0
- data/lib/taiwan_payments/testing.rb +152 -0
- data/lib/taiwan_payments/validation.rb +56 -0
- data/lib/taiwan_payments/version.rb +5 -0
- data/lib/taiwan_payments/webhooks.rb +54 -0
- data/lib/taiwan_payments.rb +67 -0
- metadata +118 -0
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
# ECPay payment fields: ECPay's name, its Chinese label in ECPay's documentation,
|
|
2
|
+
# what it means, and where it appears in this gem.
|
|
3
|
+
# docs/glossary/ecpay-payment.md is generated from this file by `rake docs:glossary`.
|
|
4
|
+
title: ECPay payment fields
|
|
5
|
+
title_zh: 綠界全方位金流欄位對照表
|
|
6
|
+
title_ja: ECPay 決済のフィールド一覧
|
|
7
|
+
reference: https://developers.ecpay.com.tw/?p=2862
|
|
8
|
+
fields:
|
|
9
|
+
MerchantID:
|
|
10
|
+
zh: 特店編號
|
|
11
|
+
en: Merchant ID issued by ECPay. Payments and e-invoices each have their own.
|
|
12
|
+
ja: ECPay が発行する加盟店 ID です。決済用と電子発票用でそれぞれ別の ID があります。
|
|
13
|
+
gem: '`merchant_id:` on the client'
|
|
14
|
+
PlatformID:
|
|
15
|
+
zh: 特約合作平台商代號
|
|
16
|
+
en: Platform merchant ID, only for marketplaces acting for sub-merchants.
|
|
17
|
+
ja: プラットフォーム加盟店 ID です。配下の加盟店を代理するマーケットプレイス専用です。
|
|
18
|
+
gem: '`platform_id:` on the client'
|
|
19
|
+
CheckMacValue:
|
|
20
|
+
zh: 檢查碼
|
|
21
|
+
en: SHA256 signature over every other field. The gem adds it to requests and verifies it on callbacks.
|
|
22
|
+
ja: ほかのすべてのフィールドから計算する SHA256 署名(チェックサム)です。gem がリクエストに付加し、コールバックでは検証します。
|
|
23
|
+
gem: '`client.check_mac_value`'
|
|
24
|
+
EncryptType:
|
|
25
|
+
zh: CheckMacValue 加密類型
|
|
26
|
+
en: Signature algorithm. Always 1 (SHA256).
|
|
27
|
+
ja: 署名アルゴリズムです。常に 1(SHA256)です。
|
|
28
|
+
gem: 'set automatically'
|
|
29
|
+
MerchantTradeNo:
|
|
30
|
+
zh: 特店訂單編號
|
|
31
|
+
en: Your unique order number, 1 to 20 letters and digits. ECPay rejects a reused one.
|
|
32
|
+
ja: 加盟店側で一意に決める注文番号で、1〜20 文字の英数字です。使用済みの番号は ECPay に拒否されます。
|
|
33
|
+
gem: '`order_no`'
|
|
34
|
+
MerchantTradeDate:
|
|
35
|
+
zh: 特店交易時間
|
|
36
|
+
en: When the order was created, in Taiwan time (yyyy/MM/dd HH:mm:ss).
|
|
37
|
+
ja: 注文の作成日時です。台湾時間で指定します(yyyy/MM/dd HH:mm:ss)。
|
|
38
|
+
gem: '`trade_date:`'
|
|
39
|
+
PaymentType:
|
|
40
|
+
zh: 交易類型 / 付款方式
|
|
41
|
+
en: Always "aio" in a checkout request. In callbacks it is the detailed method used, such as Credit_CreditCard or ATM_TAISHIN.
|
|
42
|
+
ja: '決済リクエストでは常に "aio" です。コールバックでは、Credit_CreditCard や ATM_TAISHIN のように、実際に使われた支払い方法の詳細が入ります。'
|
|
43
|
+
gem: '`payment_type`, `payment_method`'
|
|
44
|
+
TotalAmount:
|
|
45
|
+
zh: 交易金額
|
|
46
|
+
en: Amount in whole TWD.
|
|
47
|
+
ja: 金額です。新台湾ドル(TWD)の整数で指定します。
|
|
48
|
+
gem: '`amount:`'
|
|
49
|
+
TradeDesc:
|
|
50
|
+
zh: 交易描述
|
|
51
|
+
en: Order description. Not shown to the customer on most payment pages.
|
|
52
|
+
ja: 注文の説明です。ほとんどの決済ページでは購入者に表示されません。
|
|
53
|
+
gem: '`description:`'
|
|
54
|
+
ItemName:
|
|
55
|
+
zh: 商品名稱
|
|
56
|
+
en: Item names shown on the payment page, joined with "#".
|
|
57
|
+
ja: '決済ページに表示される商品名です。"#" で連結します。'
|
|
58
|
+
gem: '`items:`'
|
|
59
|
+
ReturnURL:
|
|
60
|
+
zh: 付款完成通知回傳網址
|
|
61
|
+
en: Server-to-server webhook that receives the payment result. Answer it with "1|OK".
|
|
62
|
+
ja: '決済結果を受け取るサーバー間の Webhook です。"1|OK" と応答してください。'
|
|
63
|
+
gem: '`return_url:`'
|
|
64
|
+
ChoosePayment:
|
|
65
|
+
zh: 選擇預設付款方式
|
|
66
|
+
en: Which payment method the page offers. ALL lets the customer choose.
|
|
67
|
+
ja: 決済ページで提示する支払い方法です。ALL にすると購入者が選択できます。
|
|
68
|
+
gem: '`payment_method:`'
|
|
69
|
+
ChooseSubPayment:
|
|
70
|
+
zh: 付款子項目
|
|
71
|
+
en: Narrows the method further, for example one bank for ATM transfers.
|
|
72
|
+
ja: 支払い方法をさらに絞り込みます。たとえば ATM 振込で使う銀行を 1 つに限定できます。
|
|
73
|
+
gem: '`sub_method:`'
|
|
74
|
+
IgnorePayment:
|
|
75
|
+
zh: 隱藏付款方式
|
|
76
|
+
en: Methods to hide when ChoosePayment is ALL, joined with "#".
|
|
77
|
+
ja: 'ChoosePayment が ALL のときに非表示にする支払い方法です。"#" で連結します。'
|
|
78
|
+
gem: '`ignore_methods:`'
|
|
79
|
+
OrderResultURL:
|
|
80
|
+
zh: Client 端回傳付款結果網址
|
|
81
|
+
en: The customer's browser is POSTed here with the result after paying. Must differ from ReturnURL.
|
|
82
|
+
ja: 支払い後、購入者のブラウザーが決済結果とともにここへ POST されます。ReturnURL とは異なる URL を指定する必要があります。
|
|
83
|
+
gem: '`order_result_url:`'
|
|
84
|
+
ClientBackURL:
|
|
85
|
+
zh: Client 端返回特店的按鈕連結
|
|
86
|
+
en: Target of the "back to shop" button on ECPay's page. Carries no payment result.
|
|
87
|
+
ja: ECPay のページにある「ショップに戻る」ボタンのリンク先です。決済結果は渡されません。
|
|
88
|
+
gem: '`client_back_url:`'
|
|
89
|
+
PaymentInfoURL:
|
|
90
|
+
zh: Server 端回傳付款相關資訊
|
|
91
|
+
en: Webhook that receives the ATM account or convenience store code the customer must pay to.
|
|
92
|
+
ja: 購入者が支払いに使う ATM 振込用のバーチャル口座またはコンビニ決済コード(超商代碼)を受け取る Webhook です。
|
|
93
|
+
gem: '`payment_info_url:`'
|
|
94
|
+
ClientRedirectURL:
|
|
95
|
+
zh: Client 端回傳付款相關資訊
|
|
96
|
+
en: The customer's browser is POSTed here with the ATM account or store code, instead of seeing ECPay's page.
|
|
97
|
+
ja: ECPay のページを表示する代わりに、購入者のブラウザーが ATM 振込用のバーチャル口座またはコンビニ決済コードとともにここへ POST されます。
|
|
98
|
+
gem: '`client_redirect_url:`'
|
|
99
|
+
ItemURL:
|
|
100
|
+
zh: 商品銷售網址
|
|
101
|
+
en: Link to the product page.
|
|
102
|
+
ja: 商品ページへのリンクです。
|
|
103
|
+
gem: '`item_url:`'
|
|
104
|
+
Remark:
|
|
105
|
+
zh: 備註欄位
|
|
106
|
+
en: Free-form note stored with the order.
|
|
107
|
+
ja: 注文とともに保存される自由記述のメモです。
|
|
108
|
+
gem: '`remark:`'
|
|
109
|
+
StoreID:
|
|
110
|
+
zh: 特店旗下店舖代號
|
|
111
|
+
en: Your own branch or store code, echoed back in callbacks.
|
|
112
|
+
ja: 加盟店独自の支店コードまたは店舗コードです。コールバックでそのまま返されます。
|
|
113
|
+
gem: '`store_id:`'
|
|
114
|
+
Language:
|
|
115
|
+
zh: 語系設定
|
|
116
|
+
en: Language of the payment page. ENG, KOR, JPN or CHI; Traditional Chinese when omitted.
|
|
117
|
+
ja: 決済ページの言語です。ENG、KOR、JPN、CHI のいずれかで、省略時は繁体字中国語になります。
|
|
118
|
+
gem: '`language:`'
|
|
119
|
+
NeedExtraPaidInfo:
|
|
120
|
+
zh: 是否需要額外的付款資訊
|
|
121
|
+
en: Y adds details such as card digits and bank codes to the callback.
|
|
122
|
+
ja: Y にすると、カード番号の一部や銀行コードなどの詳細がコールバックに追加されます。
|
|
123
|
+
gem: '`extra_paid_info:`'
|
|
124
|
+
CustomField1:
|
|
125
|
+
zh: 自訂名稱欄位 1
|
|
126
|
+
en: Up to 50 characters of your own data, echoed back in callbacks.
|
|
127
|
+
ja: 加盟店が自由に使える最大 50 文字のデータです。コールバックでそのまま返されます。
|
|
128
|
+
gem: '`custom_fields:`'
|
|
129
|
+
CustomField2:
|
|
130
|
+
zh: 自訂名稱欄位 2
|
|
131
|
+
en: See CustomField1.
|
|
132
|
+
ja: CustomField1 を参照してください。
|
|
133
|
+
gem: '`custom_fields:`'
|
|
134
|
+
CustomField3:
|
|
135
|
+
zh: 自訂名稱欄位 3
|
|
136
|
+
en: See CustomField1.
|
|
137
|
+
ja: CustomField1 を参照してください。
|
|
138
|
+
gem: '`custom_fields:`'
|
|
139
|
+
CustomField4:
|
|
140
|
+
zh: 自訂名稱欄位 4
|
|
141
|
+
en: See CustomField1.
|
|
142
|
+
ja: CustomField1 を参照してください。
|
|
143
|
+
gem: '`custom_fields:`'
|
|
144
|
+
ExpireDate:
|
|
145
|
+
zh: 允許繳費有效天數 / 繳費期限
|
|
146
|
+
en: In a checkout request, days allowed to pay an ATM transfer (1 to 60). In callbacks, the payment deadline.
|
|
147
|
+
ja: 決済リクエストでは、ATM 振込の支払い猶予日数(1〜60)です。コールバックでは支払い期限です。
|
|
148
|
+
gem: '`atm_expire_days:`, `payment_instructions.expires_at`'
|
|
149
|
+
StoreExpireDate:
|
|
150
|
+
zh: 超商繳費截止時間
|
|
151
|
+
en: Time allowed to pay at a convenience store. Minutes for a store code, days for a barcode.
|
|
152
|
+
ja: コンビニでの支払い猶予時間です。コンビニ決済コードの場合は分単位、コンビニバーコード(超商條碼)の場合は日単位です。
|
|
153
|
+
gem: '`store_expire:`'
|
|
154
|
+
Desc_1:
|
|
155
|
+
zh: 交易描述 1
|
|
156
|
+
en: Line printed on the convenience store kiosk slip.
|
|
157
|
+
ja: コンビニの店頭端末で出力される伝票に印字される行です。
|
|
158
|
+
gem: '`store_descriptions:`'
|
|
159
|
+
Desc_2:
|
|
160
|
+
zh: 交易描述 2
|
|
161
|
+
en: See Desc_1.
|
|
162
|
+
ja: Desc_1 を参照してください。
|
|
163
|
+
gem: '`store_descriptions:`'
|
|
164
|
+
Desc_3:
|
|
165
|
+
zh: 交易描述 3
|
|
166
|
+
en: See Desc_1.
|
|
167
|
+
ja: Desc_1 を参照してください。
|
|
168
|
+
gem: '`store_descriptions:`'
|
|
169
|
+
Desc_4:
|
|
170
|
+
zh: 交易描述 4
|
|
171
|
+
en: See Desc_1.
|
|
172
|
+
ja: Desc_1 を参照してください。
|
|
173
|
+
gem: '`store_descriptions:`'
|
|
174
|
+
CreditInstallment:
|
|
175
|
+
zh: 刷卡分期期數
|
|
176
|
+
en: Instalment terms to offer, for example 3,6,12.
|
|
177
|
+
ja: 提示する分割払いの回数です。たとえば 3,6,12 のように指定します。
|
|
178
|
+
gem: '`installments:`'
|
|
179
|
+
Redeem:
|
|
180
|
+
zh: 信用卡是否使用紅利折抵
|
|
181
|
+
en: Y lets the customer pay partly with card reward points.
|
|
182
|
+
ja: Y にすると、購入者は代金の一部をカードのポイントで支払えます。
|
|
183
|
+
gem: '`redeem:`'
|
|
184
|
+
UnionPay:
|
|
185
|
+
zh: 銀聯卡交易選項
|
|
186
|
+
en: 0 lets the customer choose, 1 forces UnionPay, 2 hides it.
|
|
187
|
+
ja: 0 は購入者が選択、1 は UnionPay(銀聯)に固定、2 は UnionPay を非表示にします。
|
|
188
|
+
gem: '`union_pay:`'
|
|
189
|
+
RtnCode:
|
|
190
|
+
zh: 交易狀態
|
|
191
|
+
en: Result code. 1 is paid, 2 means an ATM account was issued, 10100073 means a store code or barcode was issued. Anything else failed.
|
|
192
|
+
ja: 結果コードです。1 は支払い済み、2 は ATM 振込用のバーチャル口座の発行済み、10100073 はコンビニ決済コードまたはバーコードの発行済みを表します。それ以外は失敗です。
|
|
193
|
+
gem: '`status_code`, `status`, `paid?`'
|
|
194
|
+
RtnMsg:
|
|
195
|
+
zh: 交易訊息
|
|
196
|
+
en: ECPay's message for the result, usually Chinese.
|
|
197
|
+
ja: 結果に対する ECPay のメッセージです。通常は中国語です。
|
|
198
|
+
gem: '`message`'
|
|
199
|
+
TradeNo:
|
|
200
|
+
zh: 綠界的交易編號
|
|
201
|
+
en: ECPay's own number for the transaction. Keep it; refunds need it.
|
|
202
|
+
ja: ECPay 側の取引番号です。返金に必要なので保存しておいてください。
|
|
203
|
+
gem: '`trade_no`'
|
|
204
|
+
TradeAmt:
|
|
205
|
+
zh: 交易金額
|
|
206
|
+
en: Amount of the transaction in TWD. Compare it with your order before fulfilling.
|
|
207
|
+
ja: 新台湾ドル(TWD)建ての取引金額です。商品やサービスを提供する前に、自社の注文の金額と照合してください。
|
|
208
|
+
gem: '`amount`'
|
|
209
|
+
TradeDate:
|
|
210
|
+
zh: 訂單成立時間
|
|
211
|
+
en: When ECPay created the transaction.
|
|
212
|
+
ja: ECPay が取引を作成した日時です。
|
|
213
|
+
gem: '`ordered_at`'
|
|
214
|
+
PaymentDate:
|
|
215
|
+
zh: 付款時間
|
|
216
|
+
en: When the customer paid. Empty until then.
|
|
217
|
+
ja: 購入者が支払った日時です。支払われるまでは空です。
|
|
218
|
+
gem: '`paid_at`'
|
|
219
|
+
PaymentTypeChargeFee:
|
|
220
|
+
zh: 交易手續費金額
|
|
221
|
+
en: ECPay's fee for this transaction.
|
|
222
|
+
ja: この取引に対する ECPay の手数料です。
|
|
223
|
+
gem: '`fee`'
|
|
224
|
+
HandlingCharge:
|
|
225
|
+
zh: 手續費合計
|
|
226
|
+
en: Total handling charge, as reported by a query.
|
|
227
|
+
ja: 照会で返される手数料の合計です。
|
|
228
|
+
gem: '`raw["HandlingCharge"]`'
|
|
229
|
+
SimulatePaid:
|
|
230
|
+
zh: 是否為模擬付款
|
|
231
|
+
en: 1 when the callback was triggered from the back office "simulate payment" button. No money moved, so do not ship.
|
|
232
|
+
ja: 管理画面の支払いシミュレーション(模擬付款)ボタンから送信されたコールバックの場合は 1 です。実際の入金はないため、商品を発送しないでください。
|
|
233
|
+
gem: '`simulated?`'
|
|
234
|
+
TradeStatus:
|
|
235
|
+
zh: 交易狀態
|
|
236
|
+
en: In a query, 0 is unpaid and 1 is paid. Anything else is a failure code.
|
|
237
|
+
ja: 照会では、0 は未払い、1 は支払い済みです。それ以外は失敗コードです。
|
|
238
|
+
gem: '`status_code`, `status`'
|
|
239
|
+
TimeStamp:
|
|
240
|
+
zh: 驗證時間
|
|
241
|
+
en: Unix time of a query. ECPay rejects it after three minutes.
|
|
242
|
+
ja: 照会の Unix 時間です。3 分を過ぎると ECPay に拒否されます。
|
|
243
|
+
gem: 'set automatically'
|
|
244
|
+
BankCode:
|
|
245
|
+
zh: 繳費銀行代碼
|
|
246
|
+
en: Bank code of the virtual account the customer must transfer to.
|
|
247
|
+
ja: 購入者の振込先となるバーチャル口座の銀行コードです。
|
|
248
|
+
gem: '`payment_instructions.bank_code`'
|
|
249
|
+
vAccount:
|
|
250
|
+
zh: 繳費虛擬帳號
|
|
251
|
+
en: Virtual account number for the ATM transfer.
|
|
252
|
+
ja: ATM 振込用のバーチャル口座番号です。
|
|
253
|
+
gem: '`payment_instructions.virtual_account`'
|
|
254
|
+
PaymentNo:
|
|
255
|
+
zh: 繳費代碼
|
|
256
|
+
en: Code the customer enters at a convenience store kiosk.
|
|
257
|
+
ja: 購入者がコンビニの店頭端末に入力するコードです。
|
|
258
|
+
gem: '`payment_instructions.payment_no`'
|
|
259
|
+
Barcode1:
|
|
260
|
+
zh: 條碼第一段號碼
|
|
261
|
+
en: First of three barcodes the customer shows at a convenience store counter.
|
|
262
|
+
ja: 購入者がコンビニのレジで提示する 3 つのバーコードのうち 1 つ目です。
|
|
263
|
+
gem: '`payment_instructions.barcodes`'
|
|
264
|
+
Barcode2:
|
|
265
|
+
zh: 條碼第二段號碼
|
|
266
|
+
en: See Barcode1.
|
|
267
|
+
ja: Barcode1 を参照してください。
|
|
268
|
+
gem: '`payment_instructions.barcodes`'
|
|
269
|
+
Barcode3:
|
|
270
|
+
zh: 條碼第三段號碼
|
|
271
|
+
en: See Barcode1.
|
|
272
|
+
ja: Barcode1 を参照してください。
|
|
273
|
+
gem: '`payment_instructions.barcodes`'
|
|
274
|
+
Action:
|
|
275
|
+
zh: 執行動作
|
|
276
|
+
en: Card operation. C captures (關帳), R refunds (退刷), E cancels a capture (取消關帳), N releases the authorisation (放棄).
|
|
277
|
+
ja: カードに対する操作です。C は売上確定(關帳)、R は返金(退刷)、E は売上確定の取消(取消關帳)、N はオーソリの取消(放棄)です。
|
|
278
|
+
gem: '`capture`, `refund`, `cancel_capture`, `void_authorization`'
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "yaml"
|
|
4
|
+
|
|
5
|
+
module TaiwanPayments
|
|
6
|
+
# What each vendor field is called in Chinese, what it means in English and
|
|
7
|
+
# Japanese, and where it lives in this gem. One set of YAML tables feeds the
|
|
8
|
+
# `taiwan_payments glossary` command and the pages under docs/glossary,
|
|
9
|
+
# so the two cannot disagree.
|
|
10
|
+
#
|
|
11
|
+
# Not loaded by `require "taiwan_payments"`; require "taiwan_payments/glossary".
|
|
12
|
+
module Glossary
|
|
13
|
+
Entry = Data.define(:table, :term, :zh, :en, :ja, :gem) do
|
|
14
|
+
# The Chinese name is the vendor's label, not an explanation, so only
|
|
15
|
+
# Japanese has a meaning of its own; every other locale reads English.
|
|
16
|
+
def meaning(locale = :en) = (locale == :ja) ? ja : en
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
Table = Data.define(:name, :title, :title_zh, :title_ja, :reference, :entries) do
|
|
20
|
+
def heading(locale = :en) = (locale == :ja) ? title_ja : title
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
TABLES = Dir[File.join(__dir__, "glossary", "*.yml")].sort.to_h do |path|
|
|
24
|
+
name = File.basename(path, ".yml").to_sym
|
|
25
|
+
data = YAML.safe_load_file(path)
|
|
26
|
+
entries = data.fetch("fields").map do |term, details|
|
|
27
|
+
Entry.new(table: name, term: term, zh: details.fetch("zh"), en: details.fetch("en"), ja: details.fetch("ja"), gem: details.fetch("gem"))
|
|
28
|
+
end
|
|
29
|
+
[name, Table.new(name: name, title: data.fetch("title"), title_zh: data.fetch("title_zh"), title_ja: data.fetch("title_ja"),
|
|
30
|
+
reference: data.fetch("reference"), entries: entries.freeze)]
|
|
31
|
+
end.freeze
|
|
32
|
+
|
|
33
|
+
module_function
|
|
34
|
+
|
|
35
|
+
# @return [Array<Entry>]
|
|
36
|
+
def entries
|
|
37
|
+
TABLES.values.flat_map(&:entries)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Finds entries by vendor field, Chinese label, meaning or gem attribute.
|
|
41
|
+
# An exact field name wins over partial matches, so that "ItemName" does
|
|
42
|
+
# not also list every field mentioning items.
|
|
43
|
+
#
|
|
44
|
+
# @return [Array<Entry>]
|
|
45
|
+
def search(query)
|
|
46
|
+
needle = query.to_s.strip.downcase
|
|
47
|
+
return [] if needle.empty?
|
|
48
|
+
|
|
49
|
+
exact = entries.select { |entry| entry.term.downcase == needle }
|
|
50
|
+
return exact unless exact.empty?
|
|
51
|
+
|
|
52
|
+
entries.select { |entry| [entry.term, entry.zh, entry.en, entry.ja, entry.gem].any? { |text| text.downcase.include?(needle) } }
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# @param locale [Symbol] :en for the English and Chinese page, :ja for the Japanese one
|
|
56
|
+
# @return [String] the docs page for one table
|
|
57
|
+
def markdown(name, locale = :en)
|
|
58
|
+
table = TABLES.fetch(name)
|
|
59
|
+
# Vendor fields are identifiers and read best as code. Concepts are words.
|
|
60
|
+
rows = table.entries.map do |entry|
|
|
61
|
+
term = (name == :concepts) ? "**#{entry.term}**" : "`#{entry.term}`"
|
|
62
|
+
"| #{term} | #{cell(entry.zh)} | #{cell(entry.meaning(locale))} | #{cell(entry.gem)} |"
|
|
63
|
+
end
|
|
64
|
+
heading, intro, reference, columns = PAGE_TEXT.fetch(locale).call(table)
|
|
65
|
+
<<~MARKDOWN
|
|
66
|
+
<!-- Generated by `rake docs:glossary` from lib/taiwan_payments/glossary/#{name}.yml. Edit that file, not this one. -->
|
|
67
|
+
|
|
68
|
+
# #{heading}
|
|
69
|
+
|
|
70
|
+
#{intro}
|
|
71
|
+
|
|
72
|
+
#{reference}: #{table.reference}
|
|
73
|
+
|
|
74
|
+
#{columns}
|
|
75
|
+
| --- | --- | --- | --- |
|
|
76
|
+
#{rows.join("\n")}
|
|
77
|
+
MARKDOWN
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
PAGE_TEXT = {
|
|
81
|
+
en: lambda do |table|
|
|
82
|
+
["#{table.title} / #{table.title_zh}",
|
|
83
|
+
"Each row gives a field or term, its Chinese name, what it means, and where to find it in `taiwan_payments`.\n" \
|
|
84
|
+
"每一列為欄位或名詞、中文名稱、英文說明,以及在 `taiwan_payments` 中對應的寫法。",
|
|
85
|
+
"Reference / 參考資料", "| Term | 中文 | Meaning | In this gem |"]
|
|
86
|
+
end,
|
|
87
|
+
ja: lambda do |table|
|
|
88
|
+
[table.title_ja,
|
|
89
|
+
"各行は、フィールドまたは用語、その中国語名、意味、そして `taiwan_payments` での書き方を示します。",
|
|
90
|
+
"参考資料", "| 用語 | 中国語 | 意味 | この gem での書き方 |"]
|
|
91
|
+
end
|
|
92
|
+
}.freeze
|
|
93
|
+
private_constant :PAGE_TEXT
|
|
94
|
+
|
|
95
|
+
# A literal pipe would end the table cell early, as in the "1|OK" acknowledgement.
|
|
96
|
+
def cell(text)
|
|
97
|
+
text.gsub("|", "\\|")
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# @return [String] docs file name, e.g. "ecpay-invoice.md" or "ecpay-invoice.ja.md"
|
|
101
|
+
def file_name(name, locale = :en)
|
|
102
|
+
suffix = (locale == :en) ? "" : ".#{locale}"
|
|
103
|
+
"#{name.to_s.tr("_", "-")}#{suffix}.md"
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module TaiwanPayments
|
|
4
|
+
HostedForm = Data.define(:action_url, :fields, :http_method)
|
|
5
|
+
|
|
6
|
+
# ECPay and NewebPay both reject checkouts that do not arrive as a browser
|
|
7
|
+
# form POST (server-side posts and iframes are detected and refused), so the
|
|
8
|
+
# gem never submits a checkout itself. It returns the exact fields and lets
|
|
9
|
+
# the app render them, or renders an auto-submitting form on request.
|
|
10
|
+
class HostedForm
|
|
11
|
+
HTML_ESCAPES = {"&" => "&", "<" => "<", ">" => ">", '"' => """, "'" => "'"}.freeze
|
|
12
|
+
private_constant :HTML_ESCAPES
|
|
13
|
+
|
|
14
|
+
def initialize(action_url:, fields:, http_method: "post")
|
|
15
|
+
super(
|
|
16
|
+
action_url: action_url.to_s.freeze,
|
|
17
|
+
fields: fields.to_h { |name, value| [name.to_s, value.to_s] }.freeze,
|
|
18
|
+
http_method: http_method.to_s.freeze
|
|
19
|
+
)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# @param nonce [String, nil] CSP nonce for the inline auto-submit script
|
|
23
|
+
# @param auto_submit [Boolean] false renders a visible submit button instead
|
|
24
|
+
# @return [String] HTML; mark it html_safe yourself outside the Rails helper
|
|
25
|
+
def to_html(auto_submit: true, nonce: nil, id: "taiwan-payments-form", submit_text: "Continue to payment")
|
|
26
|
+
inputs = fields.map { |name, value| %( <input type="hidden" name="#{h(name)}" value="#{h(value)}">\n) }.join
|
|
27
|
+
button = %(<button type="submit">#{h(submit_text)}</button>)
|
|
28
|
+
open_tag = %(<form id="#{h(id)}" method="#{h(http_method)}" action="#{h(action_url)}" accept-charset="UTF-8">\n)
|
|
29
|
+
|
|
30
|
+
return "#{open_tag}#{inputs} #{button}\n</form>\n" unless auto_submit
|
|
31
|
+
|
|
32
|
+
# The script finds the form relative to itself so no value is ever
|
|
33
|
+
# interpolated into JavaScript, and calls the prototype's submit because
|
|
34
|
+
# a field named "submit" would otherwise shadow form.submit().
|
|
35
|
+
nonce_attribute = nonce ? %( nonce="#{h(nonce)}") : ""
|
|
36
|
+
"#{open_tag}#{inputs} <noscript>#{button}</noscript>\n</form>\n" \
|
|
37
|
+
"<script#{nonce_attribute}>HTMLFormElement.prototype.submit.call(document.currentScript.previousElementSibling);</script>\n"
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
def h(value)
|
|
43
|
+
value.to_s.gsub(/[&<>"']/, HTML_ESCAPES)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "net/http"
|
|
4
|
+
require "uri"
|
|
5
|
+
|
|
6
|
+
module TaiwanPayments
|
|
7
|
+
class HttpClient
|
|
8
|
+
Response = Data.define(:status, :body)
|
|
9
|
+
|
|
10
|
+
NETWORK_ERRORS = [
|
|
11
|
+
Timeout::Error, SocketError, EOFError, IOError, SystemCallError,
|
|
12
|
+
OpenSSL::SSL::SSLError, Net::ProtocolError
|
|
13
|
+
].freeze
|
|
14
|
+
|
|
15
|
+
attr_reader :open_timeout, :read_timeout
|
|
16
|
+
|
|
17
|
+
# Short timeouts because these calls usually sit inside a web request.
|
|
18
|
+
# There are deliberately no automatic retries: issuing an invoice or a
|
|
19
|
+
# refund twice is worse than surfacing a failure to the caller.
|
|
20
|
+
def initialize(open_timeout: 5, read_timeout: 15)
|
|
21
|
+
@open_timeout = open_timeout
|
|
22
|
+
@read_timeout = read_timeout
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# @param log [Hash] what instrumentation subscribers see. Callers pass the
|
|
26
|
+
# plaintext parameters here because the wire body may be encrypted.
|
|
27
|
+
def post_form(url, params, log: {})
|
|
28
|
+
perform(url, "application/x-www-form-urlencoded", PhpCompat.http_build_query(params), log)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def post_json(url, payload, log: {})
|
|
32
|
+
perform(url, "application/json", JSON.generate(payload), log)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def perform(url, content_type, body, log)
|
|
38
|
+
uri = URI(url)
|
|
39
|
+
request = Net::HTTP::Post.new(uri.request_uri)
|
|
40
|
+
request["Content-Type"] = content_type
|
|
41
|
+
request["User-Agent"] = "taiwan_payments/#{VERSION}"
|
|
42
|
+
request.body = body
|
|
43
|
+
|
|
44
|
+
response = Instrumentation.instrument("request.taiwan_payments", log.merge(url: url)) do |payload|
|
|
45
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
46
|
+
http.use_ssl = uri.scheme == "https"
|
|
47
|
+
http.open_timeout = open_timeout
|
|
48
|
+
http.read_timeout = read_timeout
|
|
49
|
+
result = http.start { |connection| connection.request(request) }
|
|
50
|
+
payload[:status] = result.code.to_i
|
|
51
|
+
result
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Vendors answer in UTF-8 but Net::HTTP hands back binary strings.
|
|
55
|
+
Response.new(status: response.code.to_i, body: response.body.to_s.dup.force_encoding(Encoding::UTF_8))
|
|
56
|
+
rescue *NETWORK_ERRORS => error
|
|
57
|
+
raise TransportError.new("#{error.class}: #{error.message} (POST #{url})",
|
|
58
|
+
zh: "連線失敗 #{error.class}: #{error.message}(POST #{url})",
|
|
59
|
+
ja: "接続に失敗しました #{error.class}: #{error.message}(POST #{url})")
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module TaiwanPayments
|
|
4
|
+
# A tiny pub/sub so apps can log or time vendor calls without the gem
|
|
5
|
+
# depending on ActiveSupport. The Railtie forwards these events to
|
|
6
|
+
# ActiveSupport::Notifications under the same names.
|
|
7
|
+
module Instrumentation
|
|
8
|
+
@subscribers = []
|
|
9
|
+
@mutex = Mutex.new
|
|
10
|
+
|
|
11
|
+
class << self
|
|
12
|
+
# @yieldparam name [String] event name, e.g. "request.taiwan_payments"
|
|
13
|
+
# @yieldparam payload [Hash] redacted details including :duration_ms
|
|
14
|
+
def subscribe(&block)
|
|
15
|
+
@mutex.synchronize { @subscribers << block }
|
|
16
|
+
block
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def unsubscribe(subscriber)
|
|
20
|
+
@mutex.synchronize { @subscribers.delete(subscriber) }
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def instrument(name, payload = {})
|
|
24
|
+
payload = payload.dup
|
|
25
|
+
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
26
|
+
yield payload
|
|
27
|
+
rescue => error
|
|
28
|
+
payload[:error] = error.class.name
|
|
29
|
+
raise
|
|
30
|
+
ensure
|
|
31
|
+
payload[:duration_ms] = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - started) * 1000).round(1)
|
|
32
|
+
publish(name, Redactor.call(payload))
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def publish(name, payload)
|
|
38
|
+
@mutex.synchronize { @subscribers.dup }.each { |subscriber| subscriber.call(name, payload) }
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|