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,138 @@
|
|
|
1
|
+
# 台灣金流入門
|
|
2
|
+
|
|
3
|
+
[English](taiwan-payments-101.md) | **繁體中文** | [日本語](taiwan-payments-101.ja.md)
|
|
4
|
+
|
|
5
|
+
寫給第一次在台灣做線上銷售的開發者,十分鐘看完。內容說明你在 `taiwan_payments` 與廠商文件中會遇到的欄位背後的觀念。想逐一查詢名詞,請見[名詞對照表](glossary/concepts.md)。
|
|
6
|
+
|
|
7
|
+
本文是給開發者的導覽,不是稅務或法律建議。實際的義務請詢問你的會計師或記帳士。
|
|
8
|
+
|
|
9
|
+
## 重點整理
|
|
10
|
+
|
|
11
|
+
1. 你不需要直接跟銀行串接,而是向金流公司(例如綠界 ECPay、藍新 NewebPay)申請,取得特店編號與金鑰。
|
|
12
|
+
2. 收款時,把消費者的瀏覽器導向金流公司的付款頁。付款結果由金流公司以帶有簽章的通知回傳給你的伺服器。
|
|
13
|
+
3. 有幾種常用的付款方式不是即時的:消費者會先拿到虛擬帳號或超商代碼,幾小時甚至幾天後才繳費。
|
|
14
|
+
4. 每一筆銷售都要開立統一發票。網路商店透過加值服務中心開立電子發票,由對方上傳到財政部。
|
|
15
|
+
5. 發票可以對獎,所以消費者很在意發票存到哪裡。載具與捐贈碼就是為了這件事。
|
|
16
|
+
|
|
17
|
+
## 第一部分:金流
|
|
18
|
+
|
|
19
|
+
### 付款流程
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
你的伺服器 消費者的瀏覽器 金流公司
|
|
23
|
+
| 產生帶簽章的表單 -> | |
|
|
24
|
+
| | 以 POST 送出表單 ---------> |
|
|
25
|
+
| | 在金流公司的頁面付款
|
|
26
|
+
| <---------------- 帶簽章的通知(伺服器對伺服器) ---------- |
|
|
27
|
+
| 回應 "1|OK" ---------------------------------------------> |
|
|
28
|
+
| | <---------- 導回你的網站 |
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
這個流程帶來兩個重點。
|
|
32
|
+
|
|
33
|
+
**以伺服器收到的通知為準。** 消費者可能在被導回你的網站之前就關掉分頁,所以不能因為瀏覽器回來了就把訂單標成已付款,而是要等到簽章正確的通知告訴你已付款。`taiwan_payments` 會先驗證簽章,通過之後才讓你讀取內容。
|
|
34
|
+
|
|
35
|
+
**結帳請求必須由瀏覽器送出。** 金流公司會拒絕從你的伺服器直接發出的結帳請求。這就是為什麼 `checkout` 回傳的是一份讓你輸出的表單,而不是直接發出 HTTP 請求。
|
|
36
|
+
|
|
37
|
+
### 付款方式
|
|
38
|
+
|
|
39
|
+
| 付款方式 | 英文 | 消費者怎麼付 | 是否即時 |
|
|
40
|
+
| --- | --- | --- | --- |
|
|
41
|
+
| 信用卡 | Credit card | 在金流公司的頁面輸入卡號,通常搭配簡訊驗證碼 | 是 |
|
|
42
|
+
| 分期付款 | Instalments | 信用卡分 3 到 30 期 | 是 |
|
|
43
|
+
| ATM 轉帳、虛擬帳號 | ATM transfer | 金流公司產生一組一次性的帳號,消費者用 ATM 或網路銀行轉帳 | 否 |
|
|
44
|
+
| 網路 ATM | WebATM | 將金融卡插入接在電腦上的讀卡機 | 是 |
|
|
45
|
+
| 超商代碼 | Convenience store code | 消費者在超商的機台輸入代碼,再到櫃檯付現 | 否 |
|
|
46
|
+
| 超商條碼 | Convenience store barcode | 消費者在櫃檯出示三段條碼並付現 | 否 |
|
|
47
|
+
| 行動支付 | Mobile wallets | Apple Pay、TWQR 等,在 App 內付款或掃碼 | 是 |
|
|
48
|
+
|
|
49
|
+
便利商店(7-ELEVEN、全家、萊爾富、OK)到處都有,而且二十四小時營業,很多人寧願去超商付現,也不想在網路上輸入卡號。如果你的客群是一般消費者,要預期有相當比例的訂單會使用非即時的付款方式。
|
|
50
|
+
|
|
51
|
+
### 非即時的付款
|
|
52
|
+
|
|
53
|
+
ATM 與超商付款會多一個步驟:
|
|
54
|
+
|
|
55
|
+
1. 消費者選擇付款方式,金流公司產生繳費資訊(取號)。
|
|
56
|
+
2. 你的伺服器收到一筆「取號成功」的通知。在 `taiwan_payments` 中是 `notification.awaiting_payment?`,而 `notification.payment_instructions` 內有帳號或代碼,以及繳費期限。
|
|
57
|
+
3. 消費者繳費,可能是幾天之後。你的伺服器收到第二筆通知,表示訂單已付款。
|
|
58
|
+
4. 如果超過期限,不會有任何通知,你必須自己讓訂單過期。
|
|
59
|
+
|
|
60
|
+
設計訂單狀態時請把這點考慮進去:已建立、待繳費、已付款、已過期。
|
|
61
|
+
|
|
62
|
+
### 信用卡相關名詞
|
|
63
|
+
|
|
64
|
+
- **授權(Authorisation)**:先在卡片上保留額度。
|
|
65
|
+
- **請款(Capture,綠界稱為關帳)**:實際向發卡行請款。大多數特店是每天自動請款。
|
|
66
|
+
- **退款(Refund,信用卡又稱退刷)**:請款之後把錢退回。
|
|
67
|
+
- **紅利折抵(Reward points)**:消費者用信用卡紅利點數折抵部分金額。
|
|
68
|
+
|
|
69
|
+
### 測試
|
|
70
|
+
|
|
71
|
+
每家金流公司都有獨立的測試環境與測試金鑰。綠界提供任何人都能使用的公開測試特店,所以 `PaymentClient.sandbox` 不需要任何設定就能使用。金流公司的後台還有「模擬付款」按鈕,會送出一筆假的付款成功通知。`taiwan_payments` 會把這類通知標示為 `simulated?`,絕不會是 `paid?`,因為正式環境也有這個按鈕。
|
|
72
|
+
|
|
73
|
+
## 第二部分:發票
|
|
74
|
+
|
|
75
|
+
### 統一發票
|
|
76
|
+
|
|
77
|
+
在台灣,營業人每一筆銷售都要開立統一發票,它是營業稅的正式憑證。營業稅是 5%,一般標示給消費者的價格已經內含。
|
|
78
|
+
|
|
79
|
+
財政部讓每一張發票都能對獎。發票號碼是兩碼英文加八碼數字,例如 `AB-12345678`。每兩個月開獎一次,獎金從新台幣 200 元到 1,000 萬元。這個制度的目的是讓消費者主動索取發票,讓銷售確實入帳。它確實有效,所以如果發票沒開或存錯地方,你的客人一定會發現。
|
|
80
|
+
|
|
81
|
+
### 電子發票
|
|
82
|
+
|
|
83
|
+
電子發票就是以電子方式開立的統一發票。網路商店不需要自己印發票,而是呼叫加值服務中心(例如綠界或 ezPay),由對方配號、上傳到財政部,並通知買受人。
|
|
84
|
+
|
|
85
|
+
發票號碼來自國稅局每兩個月(一期)配給你的字軌。帳號設定完成後,這些由加值服務中心代為管理。如果看到與字軌有關的錯誤,表示字軌尚未設定或已經用完。
|
|
86
|
+
|
|
87
|
+
### 買受人的發票要存到哪裡?
|
|
88
|
+
|
|
89
|
+
大部分的發票欄位都在回答這個問題。答案有四種。
|
|
90
|
+
|
|
91
|
+
**載具。** 載具是一組識別碼,無紙化的發票會歸在它底下,買受人可以查詢發票並領獎。
|
|
92
|
+
|
|
93
|
+
- **手機條碼**最常見,長得像 `/ABC+123`:一個斜線加七個字元。民眾只要向財政部申請一次,之後消費時出示條碼即可。
|
|
94
|
+
- **自然人憑證**的號碼(兩碼英文加十四碼數字)也可以當作載具。
|
|
95
|
+
- **會員載具**屬於加值服務中心。它以買受人的電子信箱或手機號碼為識別,不需要買受人提供任何號碼,並由加值服務中心寄送發票通知。買受人什麼都沒提供時,商店通常就用這種載具,這也是 `taiwan_payments` 的預設值。
|
|
96
|
+
|
|
97
|
+
**捐贈。** 買受人提供三到七碼的捐贈碼(又稱愛心碼),發票與中獎獎金都歸該受捐贈單位。捐贈的發票不能列印,也不能反悔。
|
|
98
|
+
|
|
99
|
+
**紙本。** 買受人索取紙本(列印)。列印的發票不會存入載具。
|
|
100
|
+
|
|
101
|
+
**公司帳務。** 請見下一節。
|
|
102
|
+
|
|
103
|
+
### 公司發票與統一編號
|
|
104
|
+
|
|
105
|
+
每一家公司行號與機關團體都有一組八碼的統一編號(簡稱統編)。替公司採購的買受人會提供統編,讓發票可以入公司的帳並扣抵進項稅額。這就是俗稱的「打統編」,台灣的結帳頁幾乎都會提供這個欄位。
|
|
106
|
+
|
|
107
|
+
有統編的發票:
|
|
108
|
+
|
|
109
|
+
- 稅額會分開列示,
|
|
110
|
+
- 不能捐贈,
|
|
111
|
+
- 不能對獎,
|
|
112
|
+
- 通常會列印或寄給公司,也可以存入手機條碼。
|
|
113
|
+
|
|
114
|
+
統編的八碼本身帶有檢查規則。`TaiwanPayments::TaxId.valid?` 可以驗證,讓你在自己的表單就能擋下打錯的號碼。
|
|
115
|
+
|
|
116
|
+
### 更正錯誤:作廢與折讓
|
|
117
|
+
|
|
118
|
+
- **作廢**是把整張發票取消,用在整筆交易取消的情況,而且只能在發票所屬的那一期(兩個月)尚未結束時進行。
|
|
119
|
+
- **折讓**是針對某張發票開立折讓單,用在部分退款,或是該期結束之後的任何退款。
|
|
120
|
+
|
|
121
|
+
在 `taiwan_payments` 中分別是 `void` 與 `allowance`。
|
|
122
|
+
|
|
123
|
+
### 課稅類別
|
|
124
|
+
|
|
125
|
+
| 類別 | 英文 | 適用情況 |
|
|
126
|
+
| --- | --- | --- |
|
|
127
|
+
| 應稅 | Taxable | 幾乎所有國內銷售。營業稅 5%,已內含於價格。 |
|
|
128
|
+
| 零稅率 | Zero-rated | 主要是外銷。需要通關方式註記,2026 年起還需要零稅率原因代碼。 |
|
|
129
|
+
| 免稅 | Exempt | 稅法明定的特定貨物與勞務。 |
|
|
130
|
+
|
|
131
|
+
大多數商店只會開立應稅發票,這也是預設值。
|
|
132
|
+
|
|
133
|
+
## 接下來
|
|
134
|
+
|
|
135
|
+
- API 請見 [ECPay payments](ecpay-payments.md) 與 [ECPay e-invoices](ecpay-invoices.md)(英文)。
|
|
136
|
+
- 如何安全地接收通知,請見 [Webhooks](webhooks.md)(英文)。
|
|
137
|
+
- 遇到任何欄位都可以查對照表:[名詞解釋](glossary/concepts.md)、[綠界金流欄位](glossary/ecpay-payment.md)、[綠界電子發票欄位](glossary/ecpay-invoice.md)。
|
|
138
|
+
- 在終端機輸入:`taiwan_payments glossary 載具`。
|
data/docs/testing.md
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# Testing your integration
|
|
2
|
+
|
|
3
|
+
`taiwan_payments/testing` builds vendor payloads that are signed and encrypted exactly as the vendor would, with the keys of the client you pass in. Your specs can exercise real verification code with no network and no sandbox account.
|
|
4
|
+
|
|
5
|
+
```ruby
|
|
6
|
+
# spec/rails_helper.rb or test/test_helper.rb
|
|
7
|
+
require "taiwan_payments/testing"
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
It is not loaded by `require "taiwan_payments"`, so it never ships in your production code path.
|
|
11
|
+
|
|
12
|
+
## Payment callbacks
|
|
13
|
+
|
|
14
|
+
```ruby
|
|
15
|
+
client = TaiwanPayments.ecpay_payments # or PaymentClient.sandbox
|
|
16
|
+
|
|
17
|
+
# Paid
|
|
18
|
+
params = TaiwanPayments::Testing.ecpay_payment_notification(client, order_no: "ORD1001", amount: 1200)
|
|
19
|
+
|
|
20
|
+
# Failed
|
|
21
|
+
TaiwanPayments::Testing.ecpay_payment_notification(client, order_no: "ORD1001", amount: 1200, paid: false)
|
|
22
|
+
|
|
23
|
+
# The back office "simulate payment" button
|
|
24
|
+
TaiwanPayments::Testing.ecpay_payment_notification(client, order_no: "ORD1001", amount: 1200, simulated: true)
|
|
25
|
+
|
|
26
|
+
# Any raw ECPay field can be set. The payload is signed after your changes.
|
|
27
|
+
TaiwanPayments::Testing.ecpay_payment_notification(client, order_no: "ORD1001", amount: 1200,
|
|
28
|
+
PaymentType: "WebATM_TAISHIN", CustomField1: "user-42")
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Each returns a Hash of Strings, ready to post:
|
|
32
|
+
|
|
33
|
+
```ruby
|
|
34
|
+
RSpec.describe "ECPay webhook", type: :request do
|
|
35
|
+
let(:client) { TaiwanPayments.ecpay_payments }
|
|
36
|
+
let(:order) { create(:order, number: "ORD1001", total: 1200) }
|
|
37
|
+
|
|
38
|
+
it "marks the order paid" do
|
|
39
|
+
post webhooks_ecpay_payment_path,
|
|
40
|
+
params: TaiwanPayments::Testing.ecpay_payment_notification(client, order_no: order.number, amount: 1200)
|
|
41
|
+
|
|
42
|
+
expect(response.body).to eq("1|OK")
|
|
43
|
+
expect(order.reload).to be_paid
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it "ignores a simulated payment" do
|
|
47
|
+
post webhooks_ecpay_payment_path,
|
|
48
|
+
params: TaiwanPayments::Testing.ecpay_payment_notification(client, order_no: order.number, amount: 1200, simulated: true)
|
|
49
|
+
|
|
50
|
+
expect(order.reload).not_to be_paid
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
it "rejects a forged callback" do
|
|
54
|
+
forged = TaiwanPayments::Testing.ecpay_payment_notification(client, order_no: order.number, amount: 1200)
|
|
55
|
+
|
|
56
|
+
post webhooks_ecpay_payment_path, params: forged.merge("TradeAmt" => "1")
|
|
57
|
+
|
|
58
|
+
expect(response).to have_http_status(:bad_request)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Payment codes
|
|
64
|
+
|
|
65
|
+
For ATM, convenience store code and barcode payments, the first callback says a code was issued:
|
|
66
|
+
|
|
67
|
+
```ruby
|
|
68
|
+
params = TaiwanPayments::Testing.ecpay_payment_code_notification(client, order_no: "ORD1001", amount: 1200, payment_method: :atm)
|
|
69
|
+
|
|
70
|
+
notification = client.notification(params)
|
|
71
|
+
notification.awaiting_payment? # => true
|
|
72
|
+
notification.payment_instructions.virtual_account # => "5219111811417485"
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
`payment_method:` is `:atm`, `:cvs` or `:barcode`. `expires_at:` sets the deadline.
|
|
76
|
+
|
|
77
|
+
## Order queries
|
|
78
|
+
|
|
79
|
+
`query` makes an HTTP call, so stub it with the body ECPay would return:
|
|
80
|
+
|
|
81
|
+
```ruby
|
|
82
|
+
stub_request(:post, "https://payment-stage.ecpay.com.tw/Cashier/QueryTradeInfo/V5")
|
|
83
|
+
.to_return(body: TaiwanPayments::Testing.ecpay_trade_query_body(client, order_no: "ORD1001", amount: 1200, status: :paid))
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
`status:` is `:paid`, `:unpaid` or `:failed`. The example uses WebMock. Any HTTP stubbing library works.
|
|
87
|
+
|
|
88
|
+
## E-invoice calls
|
|
89
|
+
|
|
90
|
+
E-invoice responses are encrypted. These helpers encrypt with the invoice client's keys:
|
|
91
|
+
|
|
92
|
+
```ruby
|
|
93
|
+
invoices = TaiwanPayments.ecpay_invoices # or InvoiceClient.sandbox
|
|
94
|
+
issue_url = "https://einvoice-stage.ecpay.com.tw/B2CInvoice/Issue"
|
|
95
|
+
|
|
96
|
+
# A successful issue
|
|
97
|
+
stub_request(:post, issue_url)
|
|
98
|
+
.to_return(body: TaiwanPayments::Testing.ecpay_invoice_issued_body(invoices, invoice_no: "AB12345678"))
|
|
99
|
+
|
|
100
|
+
# A refusal, raised by the client as ApiError
|
|
101
|
+
stub_request(:post, issue_url)
|
|
102
|
+
.to_return(body: TaiwanPayments::Testing.ecpay_invoice_error_body(invoices, code: 5070357, message: "自訂編號重覆"))
|
|
103
|
+
|
|
104
|
+
# Any other response, from its decrypted fields
|
|
105
|
+
stub_request(:post, "https://einvoice-stage.ecpay.com.tw/B2CInvoice/Invalid")
|
|
106
|
+
.to_return(body: TaiwanPayments::Testing.ecpay_invoice_body(invoices, "RtnCode" => 1, "InvoiceNo" => "AB12345678"))
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
To assert on what your code sent, decrypt the request:
|
|
110
|
+
|
|
111
|
+
```ruby
|
|
112
|
+
expect(a_request(:post, issue_url).with { |request|
|
|
113
|
+
sent = TaiwanPayments::Testing.ecpay_invoice_request(invoices, request.body)
|
|
114
|
+
sent["SalesAmount"] == 1200 && sent["CarrierType"] == "3"
|
|
115
|
+
}).to have_been_made
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
The endpoint names and the fields of each response are listed in the [ECPay e-invoice glossary](glossary/ecpay-invoice.md).
|
|
119
|
+
|
|
120
|
+
## Resetting configuration
|
|
121
|
+
|
|
122
|
+
If a test changes the global configuration, put it back:
|
|
123
|
+
|
|
124
|
+
```ruby
|
|
125
|
+
after { TaiwanPayments.reset_configuration! }
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Testing against ECPay itself
|
|
129
|
+
|
|
130
|
+
The public test merchants work from anywhere, so an occasional end-to-end check costs nothing:
|
|
131
|
+
|
|
132
|
+
```ruby
|
|
133
|
+
invoices = TaiwanPayments::ECPay::InvoiceClient.sandbox
|
|
134
|
+
invoice = TaiwanPayments::Invoice.new(order_no: "T#{Time.now.to_i}", buyer: {email: "test@example.com"},
|
|
135
|
+
items: [{name: "Test", unit_price: 100}])
|
|
136
|
+
result = invoices.issue(invoice)
|
|
137
|
+
invoices.void(invoice_no: result.invoice_no, issued_on: result.issued_at, reason: "test")
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
The stage environment is shared by every developer in Taiwan, so do not send real customer data to it, and keep such tests out of the suite you run on every commit.
|
data/docs/webhooks.md
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# Webhooks
|
|
2
|
+
|
|
3
|
+
ECPay tells you about a payment by calling URLs you pass at checkout. Getting this part right is what separates a shop that works from one that loses orders.
|
|
4
|
+
|
|
5
|
+
## The four URLs
|
|
6
|
+
|
|
7
|
+
| Argument | ECPay field | Who calls it | What for |
|
|
8
|
+
| --- | --- | --- | --- |
|
|
9
|
+
| `return_url:` | `ReturnURL` | ECPay's server | The payment result. **This is the one that matters.** |
|
|
10
|
+
| `payment_info_url:` | `PaymentInfoURL` | ECPay's server | The ATM account or convenience store code that was issued |
|
|
11
|
+
| `order_result_url:` | `OrderResultURL` | The customer's browser, by form POST | The payment result again, to show a "thank you" page |
|
|
12
|
+
| `client_back_url:` | `ClientBackURL` | The customer's browser, by a link | Nothing. It is just a "back to shop" button. |
|
|
13
|
+
|
|
14
|
+
Mark orders paid from `return_url` only. The browser may never come back, and a link carries no proof of payment.
|
|
15
|
+
|
|
16
|
+
All of them must be reachable from the public internet on port 80 or 443. In development, run a tunnel such as ngrok or cloudflared and pass its URL.
|
|
17
|
+
|
|
18
|
+
## Receiving a callback
|
|
19
|
+
|
|
20
|
+
With Rails:
|
|
21
|
+
|
|
22
|
+
```ruby
|
|
23
|
+
module Webhooks
|
|
24
|
+
class EcpayController < ApplicationController
|
|
25
|
+
include TaiwanPayments::Webhooks
|
|
26
|
+
|
|
27
|
+
def payment
|
|
28
|
+
notification = ecpay_payment_notification
|
|
29
|
+
order = Order.find_by!(number: notification.order_no)
|
|
30
|
+
|
|
31
|
+
if notification.paid? && !order.paid?
|
|
32
|
+
raise "amount mismatch" unless notification.amount == order.total
|
|
33
|
+
|
|
34
|
+
order.mark_paid!(trade_no: notification.trade_no, paid_at: notification.paid_at)
|
|
35
|
+
elsif notification.awaiting_payment?
|
|
36
|
+
order.update!(payment_instructions: notification.payment_instructions.to_h)
|
|
37
|
+
elsif notification.failed?
|
|
38
|
+
order.payment_failed!(notification.message)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
render_ecpay_ack
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Without Rails:
|
|
48
|
+
|
|
49
|
+
```ruby
|
|
50
|
+
notification = payments.notification(form_fields) # the POST body as a Hash
|
|
51
|
+
# ... update the order ...
|
|
52
|
+
[200, {"content-type" => "text/plain"}, [notification.ack]]
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Rules that keep orders safe
|
|
56
|
+
|
|
57
|
+
**Verify first.** `ecpay_payment_notification` and `client.notification` raise `SignatureError` unless ECPay signed the payload with your keys. The Rails concern turns that into HTTP 400 and logs a warning. Never build your own shortcut around this.
|
|
58
|
+
|
|
59
|
+
**Answer `1|OK`.** ECPay treats anything else, including an HTML error page or a redirect, as a failed delivery and sends the callback again later. Answer `1|OK` once you have recorded the result, including for failed payments and for callbacks you have already processed.
|
|
60
|
+
|
|
61
|
+
**Be idempotent.** The same callback can arrive more than once. Check the order's state before changing it, as in the example.
|
|
62
|
+
|
|
63
|
+
**Use `paid?`, not `RtnCode`.** ECPay's back office has a "simulate payment" (模擬付款) button, in production too. It sends `RtnCode=1` with `SimulatePaid=1` and moves no money. `paid?` is false for those. `simulated?` is true.
|
|
64
|
+
|
|
65
|
+
**Check the amount.** Compare `notification.amount` with what you expected for that order.
|
|
66
|
+
|
|
67
|
+
**Use the POST body only.** `request.request_parameters` is what ECPay signed. `params` in Rails also contains route and query-string values. The concern uses the right one, and `client.notification` also drops Rails' `controller` and `action` keys if you pass `params` anyway.
|
|
68
|
+
|
|
69
|
+
**Do slow work later.** ECPay waits only a short time for your answer. Record the result, enqueue a job for emails and fulfilment, and respond.
|
|
70
|
+
|
|
71
|
+
## Payments that are not instant
|
|
72
|
+
|
|
73
|
+
For ATM, convenience store code and barcode payments, two callbacks arrive:
|
|
74
|
+
|
|
75
|
+
1. At `payment_info_url`, right away: `awaiting_payment?` is true and `payment_instructions` tells the customer how to pay.
|
|
76
|
+
2. At `return_url`, when the customer has paid: `paid?` is true.
|
|
77
|
+
|
|
78
|
+
If the customer never pays, no second callback is sent. Expire the order yourself after `payment_instructions.expires_at`.
|
|
79
|
+
|
|
80
|
+
## Showing a result page with `order_result_url`
|
|
81
|
+
|
|
82
|
+
After paying, ECPay can POST the result to your site through the customer's browser. Two details bite here.
|
|
83
|
+
|
|
84
|
+
First, it is a cross-site POST. Browsers do not send `SameSite=Lax` cookies, which is the Rails default for the session cookie, with a POST that comes from another site. Your customer will look logged out on that one request, and Rails' CSRF check would reject it.
|
|
85
|
+
|
|
86
|
+
Second, `order_result_url` must not be the same URL as `return_url`. Both receive the same fields, so one endpoint would process the order twice and show the customer the text `1|OK`. The gem refuses a checkout where they match.
|
|
87
|
+
|
|
88
|
+
Handle it in the webhook controller, which needs no session, and redirect to a normal page. The redirect is a top-level GET, so cookies are sent again:
|
|
89
|
+
|
|
90
|
+
```ruby
|
|
91
|
+
# POST /webhooks/ecpay/result
|
|
92
|
+
def result
|
|
93
|
+
notification = ecpay_payment_notification
|
|
94
|
+
redirect_to order_path(notification.order_no), status: :see_other
|
|
95
|
+
end
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Treat this request as a way to pick the page to show. Leave marking the order paid to `return_url`.
|
|
99
|
+
|
|
100
|
+
## When a callback never arrives
|
|
101
|
+
|
|
102
|
+
Networks fail, deploys happen, and tunnels close. For any order still waiting after a reasonable time, ask ECPay directly:
|
|
103
|
+
|
|
104
|
+
```ruby
|
|
105
|
+
trade = payments.query(order_no: order.number)
|
|
106
|
+
order.mark_paid!(trade_no: trade.trade_no) if trade&.paid? && !order.paid?
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Run this from a scheduled job for orders older than about ten minutes, and spread the calls out. ECPay rate-limits merchants that query too often.
|
|
110
|
+
|
|
111
|
+
## Testing callbacks
|
|
112
|
+
|
|
113
|
+
`TaiwanPayments::Testing` builds payloads that are signed exactly as ECPay signs them, so your controller specs need no network. See [Testing](testing.md).
|
data/exe/taiwan_payments
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rails/generators/base"
|
|
4
|
+
|
|
5
|
+
module TaiwanPayments
|
|
6
|
+
module Generators
|
|
7
|
+
# bin/rails generate taiwan_payments:install
|
|
8
|
+
class InstallGenerator < ::Rails::Generators::Base
|
|
9
|
+
source_root File.expand_path("templates", __dir__)
|
|
10
|
+
|
|
11
|
+
desc "Creates the taiwan_payments initializer and a controller for ECPay payment callbacks."
|
|
12
|
+
class_option :skip_webhooks, type: :boolean, default: false, desc: "Skip the callback controller and its route"
|
|
13
|
+
|
|
14
|
+
def create_initializer
|
|
15
|
+
template "initializer.rb.tt", "config/initializers/taiwan_payments.rb"
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def create_webhook_controller
|
|
19
|
+
return if options[:skip_webhooks]
|
|
20
|
+
|
|
21
|
+
template "ecpay_controller.rb.tt", "app/controllers/webhooks/ecpay_controller.rb"
|
|
22
|
+
route 'post "webhooks/ecpay/payment", to: "webhooks/ecpay#payment"'
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def show_next_steps
|
|
26
|
+
say <<~TEXT
|
|
27
|
+
|
|
28
|
+
taiwan_payments is ready. Outside production it uses ECPay's public
|
|
29
|
+
test merchants, so you can try a checkout before you have an account.
|
|
30
|
+
|
|
31
|
+
Next:
|
|
32
|
+
1. Build a checkout and render it: <%= taiwan_payments_form(@form) %>
|
|
33
|
+
2. Point return_url at /webhooks/ecpay/payment (ECPay needs a public
|
|
34
|
+
URL: use a tunnel such as ngrok or cloudflared in development).
|
|
35
|
+
3. Add your production keys with: bin/rails credentials:edit --environment production
|
|
36
|
+
|
|
37
|
+
TEXT
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Webhooks
|
|
4
|
+
# Receives ECPay's server-to-server payment callbacks (ReturnURL).
|
|
5
|
+
#
|
|
6
|
+
# ECPay cannot log in, so skip any authentication filter that
|
|
7
|
+
# ApplicationController applies. The callback is authenticated by its
|
|
8
|
+
# signature instead: a request that fails verification gets HTTP 400.
|
|
9
|
+
class EcpayController < ApplicationController
|
|
10
|
+
include TaiwanPayments::Webhooks
|
|
11
|
+
|
|
12
|
+
# POST /webhooks/ecpay/payment
|
|
13
|
+
def payment
|
|
14
|
+
notification = ecpay_payment_notification
|
|
15
|
+
|
|
16
|
+
# ECPay can deliver the same callback more than once, so whatever you
|
|
17
|
+
# do here must be safe to repeat. For example:
|
|
18
|
+
#
|
|
19
|
+
# order = Order.find_by!(number: notification.order_no)
|
|
20
|
+
# order.mark_paid!(trade_no: notification.trade_no) if notification.paid? && !order.paid?
|
|
21
|
+
Rails.logger.info("ECPay callback for #{notification.order_no}: #{notification.status}")
|
|
22
|
+
|
|
23
|
+
render_ecpay_ack
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Payments and e-invoicing for Taiwan.
|
|
4
|
+
#
|
|
5
|
+
# ECPay (綠界) sells payments and e-invoices as separate products, each with
|
|
6
|
+
# its own merchant ID and keys. Outside production this file uses ECPay's
|
|
7
|
+
# public test merchants, so everything works before you have an account.
|
|
8
|
+
#
|
|
9
|
+
# For production, run
|
|
10
|
+
#
|
|
11
|
+
# bin/rails credentials:edit --environment production
|
|
12
|
+
#
|
|
13
|
+
# and add:
|
|
14
|
+
#
|
|
15
|
+
# ecpay:
|
|
16
|
+
# payments:
|
|
17
|
+
# merchant_id: "1234567"
|
|
18
|
+
# hash_key: "................"
|
|
19
|
+
# hash_iv: "................"
|
|
20
|
+
# invoices:
|
|
21
|
+
# merchant_id: "1234567"
|
|
22
|
+
# hash_key: "................"
|
|
23
|
+
# hash_iv: "................"
|
|
24
|
+
TaiwanPayments.configure do |config|
|
|
25
|
+
if Rails.env.production?
|
|
26
|
+
config.env = :production
|
|
27
|
+
config.ecpay_payments = Rails.application.credentials.dig(:ecpay, :payments)
|
|
28
|
+
config.ecpay_invoices = Rails.application.credentials.dig(:ecpay, :invoices)
|
|
29
|
+
else
|
|
30
|
+
config.ecpay_payments = :sandbox
|
|
31
|
+
config.ecpay_invoices = :sandbox
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Error messages are in English by default. To get the vendors' original
|
|
36
|
+
# Chinese wording instead:
|
|
37
|
+
#
|
|
38
|
+
# TaiwanPayments.locale = :zh_tw # or :ja
|