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
data/README.zh-TW.md
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
# taiwan_payments
|
|
2
|
+
|
|
3
|
+
[English](README.md) | **繁體中文** | [日本語](README.ja.md)
|
|
4
|
+
|
|
5
|
+
[](https://buymeacoffee.com/randomizor)
|
|
6
|
+
|
|
7
|
+
用 Ruby 在台灣收款並開立電子發票。目前支援綠界(ECPay)金流與電子發票,接下來會加入藍新(NewebPay)金流與 ezPay 電子發票。
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
payments = TaiwanPayments::ECPay::PaymentClient.sandbox # 綠界公開的測試特店,不需要申請帳號
|
|
11
|
+
form = payments.checkout(order_no: "ORD1001", amount: 1200, description: "Order 1001",
|
|
12
|
+
items: ["Oolong tea x2"], return_url: "https://shop.example/webhooks/ecpay/payment")
|
|
13
|
+
form.to_html # 把這段 HTML 送到消費者的瀏覽器,就會進入綠界的付款頁
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## 為什麼用這個 gem
|
|
17
|
+
|
|
18
|
+
- **一分鐘就能跑起來。** 內建綠界公開的測試特店,還沒申請帳號就能完成結帳與開立發票。
|
|
19
|
+
- **金流與電子發票一次搞定。** 在台灣做生意兩者缺一不可:先收款,再為這筆交易開立統一發票。
|
|
20
|
+
- **看不懂中文的人能用,看得懂的人也好用。** API 與文件以英文為主,錯誤訊息提供英文、中文與日文,並附上[欄位對照表](docs/glossary/),每個廠商欄位都有中文名稱與說明。
|
|
21
|
+
- **沒有任何相依套件。** 只用 Ruby 標準函式庫。Rails 整合只有在 Rails 存在時才會載入。
|
|
22
|
+
- **有驗證,不靠猜。** 簽章與加密都以綠界公布的測試向量驗證。每一支 API 都實際呼叫過綠界測試環境,測試用的就是當時的真實回應。行覆蓋率與分支覆蓋率皆為 100%。
|
|
23
|
+
- **預設就安全。** 通知必須先通過簽章驗證才能讀取。金鑰與消費者個資在紀錄中會被遮蔽。任何牽涉金流的動作都不會自動重試。
|
|
24
|
+
- **容易測試。** 內建的輔助方法可以產生簽章正確的假通知與加密過的假 API 回應,你的測試不需要連網。
|
|
25
|
+
|
|
26
|
+
| | 金流 | 電子發票 |
|
|
27
|
+
| --- | --- | --- |
|
|
28
|
+
| 綠界 ECPay | 支援 | 支援(B2C API,含打統編的發票) |
|
|
29
|
+
| 藍新 NewebPay | 預計 0.2 版 | |
|
|
30
|
+
| ezPay 簡單付 | | 預計 0.3 版 |
|
|
31
|
+
|
|
32
|
+
第一次接觸台灣的金流嗎?請先讀[台灣金流入門](docs/taiwan-payments-101.zh-TW.md),十分鐘了解載具、統編、超商付款與統一發票對獎。
|
|
33
|
+
|
|
34
|
+
不寫 Ruby?[API 指南](https://randomizor.github.io/taiwan_payments/zh-TW/)以不限程式語言的方式說明同樣的請求、簽章與通知,附 curl、PHP、Node.js、Python 與 Go 的可執行範例,提供 English、繁體中文與日本語三種版本。其中的[名詞對照表](https://randomizor.github.io/taiwan_payments/zh-TW/glossary.html)把載具、折讓、字軌等名詞的中文、英文、日文說法並列。
|
|
35
|
+
|
|
36
|
+
## 安裝
|
|
37
|
+
|
|
38
|
+
```ruby
|
|
39
|
+
# Gemfile
|
|
40
|
+
gem "taiwan_payments"
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
需要 Ruby 3.2 以上。Rails 為選用,已測試 7.1 到 8.1。
|
|
44
|
+
|
|
45
|
+
## Rails 快速上手
|
|
46
|
+
|
|
47
|
+
```sh
|
|
48
|
+
bin/rails generate taiwan_payments:install
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
這會產生 `config/initializers/taiwan_payments.rb`、`Webhooks::EcpayController` 以及對應的路由。在正式環境以外,initializer 會使用綠界的測試特店;正式環境則從 Rails credentials 讀取你的金鑰。
|
|
52
|
+
|
|
53
|
+
把消費者導向付款頁:
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
class CheckoutsController < ApplicationController
|
|
57
|
+
def create
|
|
58
|
+
order = current_user.orders.find(params[:order_id])
|
|
59
|
+
@form = TaiwanPayments.ecpay_payments.checkout(
|
|
60
|
+
order_no: order.number,
|
|
61
|
+
amount: order.total,
|
|
62
|
+
description: "Order #{order.number}",
|
|
63
|
+
items: order.line_items.map(&:name),
|
|
64
|
+
return_url: webhooks_ecpay_payment_url,
|
|
65
|
+
client_back_url: order_url(order)
|
|
66
|
+
)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
```erb
|
|
72
|
+
<%# app/views/checkouts/create.html.erb %>
|
|
73
|
+
<%= taiwan_payments_form(@form) %>
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
消費者付款後,綠界會呼叫你的伺服器:
|
|
77
|
+
|
|
78
|
+
```ruby
|
|
79
|
+
module Webhooks
|
|
80
|
+
class EcpayController < ApplicationController
|
|
81
|
+
include TaiwanPayments::Webhooks
|
|
82
|
+
|
|
83
|
+
def payment
|
|
84
|
+
notification = ecpay_payment_notification # 已驗證簽章,驗證失敗會直接回應 400
|
|
85
|
+
order = Order.find_by!(number: notification.order_no)
|
|
86
|
+
order.mark_paid!(trade_no: notification.trade_no) if notification.paid? && !order.paid?
|
|
87
|
+
render_ecpay_ack # 綠界會一直重送,直到收到 "1|OK"
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
`return_url` 必須是綠界連得到的公開網址,開發時請使用 ngrok 或 cloudflared 之類的通道。[Webhooks](docs/webhooks.md) 說明三種通知的差別,以及哪些錯誤會讓訂單漏單。[Rails](docs/rails.md) 說明 helper、concern 與產生器。
|
|
94
|
+
|
|
95
|
+
## 金流
|
|
96
|
+
|
|
97
|
+
<!-- runnable -->
|
|
98
|
+
```ruby
|
|
99
|
+
require "taiwan_payments"
|
|
100
|
+
|
|
101
|
+
payments = TaiwanPayments::ECPay::PaymentClient.sandbox
|
|
102
|
+
# 使用自己的金鑰:
|
|
103
|
+
# TaiwanPayments::ECPay::PaymentClient.new(merchant_id: "...", hash_key: "...", hash_iv: "...", env: :production)
|
|
104
|
+
|
|
105
|
+
form = payments.checkout(
|
|
106
|
+
order_no: "ORD1001", # 你的訂單編號,不可重複,最多 20 碼英數字
|
|
107
|
+
amount: 1200, # 新台幣整數金額
|
|
108
|
+
description: "Order 1001",
|
|
109
|
+
items: ["Oolong tea x2", "Shipping"],
|
|
110
|
+
return_url: "https://shop.example/webhooks/ecpay/payment",
|
|
111
|
+
payment_method: :credit # 或 :atm、:cvs、:barcode、:web_atm、:apple_pay 等,預設為 :all
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
form.action_url # => "https://payment-stage.ecpay.com.tw/Cashier/AioCheckOut/V5"
|
|
115
|
+
form.fields["TotalAmount"] # => "1200"
|
|
116
|
+
form.fields["ChoosePayment"] # => "Credit"
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
綠界只接受由消費者瀏覽器以表單 POST 送出的結帳請求,所以這個 gem 不會自己呼叫綠界,而是把表單交給你。你可以直接輸出 `form.to_html`,或用 `form.action_url` 與 `form.fields` 自己組表單。
|
|
120
|
+
|
|
121
|
+
收到通知時,把 POST 進來的欄位交給 `notification`。如果不是綠界用你的金鑰簽的,會拋出 `TaiwanPayments::SignatureError`。
|
|
122
|
+
|
|
123
|
+
<!-- runnable -->
|
|
124
|
+
```ruby
|
|
125
|
+
require "taiwan_payments/testing" # 產生一筆簽章正確的通知,在這裡代替綠界
|
|
126
|
+
params = TaiwanPayments::Testing.ecpay_payment_notification(payments, order_no: "ORD1001", amount: 1200)
|
|
127
|
+
|
|
128
|
+
notification = payments.notification(params)
|
|
129
|
+
notification.paid? # => true
|
|
130
|
+
notification.order_no # => "ORD1001"
|
|
131
|
+
notification.amount # => 1200
|
|
132
|
+
notification.payment_method # => :credit
|
|
133
|
+
notification.status # => :paid
|
|
134
|
+
notification.ack # => "1|OK"
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
`status` 可能是 `:paid`(已付款)、`:awaiting_payment`(已取得 ATM 虛擬帳號或超商代碼,消費者尚未繳費)、`:simulated`(有人在綠界後台按了「模擬付款」,實際上沒有金流)或 `:failed`(失敗)。
|
|
138
|
+
|
|
139
|
+
如果一直沒收到通知,可以主動向綠界查詢:
|
|
140
|
+
|
|
141
|
+
<!-- runnable -->
|
|
142
|
+
```ruby
|
|
143
|
+
trade = payments.query(order_no: "ORD1001") # 綠界查無此訂單時回傳 nil
|
|
144
|
+
trade.paid? # => true
|
|
145
|
+
trade.amount # => 1200
|
|
146
|
+
trade.trade_no # => "2612031748190001"
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
信用卡交易可以用 `payments.refund(order_no:, trade_no:, amount:)` 退刷。所有選項、各種付款方式、分期,以及關帳與退刷,請見 [ECPay payments](docs/ecpay-payments.md)。
|
|
150
|
+
|
|
151
|
+
## 電子發票
|
|
152
|
+
|
|
153
|
+
發票只需要描述一次。這個模型不綁定綠界,之後同一個物件也能用在 ezPay。
|
|
154
|
+
|
|
155
|
+
<!-- runnable -->
|
|
156
|
+
```ruby
|
|
157
|
+
invoice = TaiwanPayments::Invoice.new(
|
|
158
|
+
order_no: "ORD1001",
|
|
159
|
+
buyer: {email: "buyer@example.com"},
|
|
160
|
+
items: [
|
|
161
|
+
{name: "Oolong tea", quantity: 2, unit_price: 500},
|
|
162
|
+
{name: "Shipping", unit_price: 50}
|
|
163
|
+
]
|
|
164
|
+
)
|
|
165
|
+
invoice.total # => 1050
|
|
166
|
+
|
|
167
|
+
invoices = TaiwanPayments::ECPay::InvoiceClient.sandbox
|
|
168
|
+
result = invoices.issue(invoice)
|
|
169
|
+
result.invoice_no # => "OQ13570511"
|
|
170
|
+
result.random_number # => "5248"
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
買受人如何取得發票,是新手最容易卡住的地方。模型會挑選最常見的預設值,你也可以自行指定:
|
|
174
|
+
|
|
175
|
+
| 買受人 | 傳入 | 結果 |
|
|
176
|
+
| --- | --- | --- |
|
|
177
|
+
| 沒有特別要求 | 不用傳 | 存入綠界會員載具,綠界會寄信通知買受人 |
|
|
178
|
+
| 有手機條碼 | `carrier: "/ABC+123"` | 存入買受人自己的載具 |
|
|
179
|
+
| 想捐贈發票 | `donation_code: "168001"` | 捐給該受捐贈單位 |
|
|
180
|
+
| 是公司(要打統編) | `buyer: {tax_id: "53538851", name: "...", address: "...", email: "..."}` | 開立有統編的發票並列印 |
|
|
181
|
+
| 想要紙本 | `print: true`,並提供買受人姓名與地址 | 列印 |
|
|
182
|
+
|
|
183
|
+
退款有兩種做法。在同一個兩個月的申報期內可以直接作廢發票;過了申報期,或只退部分金額,則要開立折讓單。
|
|
184
|
+
|
|
185
|
+
<!-- runnable -->
|
|
186
|
+
```ruby
|
|
187
|
+
invoices.void(invoice_no: "OQ13570511", issued_on: result.issued_at, reason: "Order cancelled") # => true
|
|
188
|
+
|
|
189
|
+
credit = invoices.allowance(invoice_no: "OQ13570511", issued_on: result.issued_at,
|
|
190
|
+
items: [{name: "Shipping", unit_price: 50}], email: "buyer@example.com")
|
|
191
|
+
credit.remaining_amount # => 1000
|
|
192
|
+
|
|
193
|
+
record = invoices.find(order_no: "ORD1001") # 查無發票時回傳 nil
|
|
194
|
+
record.voided? # => false
|
|
195
|
+
record.total # => 1050
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
課稅類別、零稅率、延遲開立、列印,以及捐贈碼與手機條碼的驗證,請見 [ECPay e-invoices](docs/ecpay-invoices.md)。
|
|
199
|
+
|
|
200
|
+
## 三種語言的錯誤訊息
|
|
201
|
+
|
|
202
|
+
這個 gem 拋出的例外都是 `TaiwanPayments::Error`,同時帶有英文、中文與日文訊息。綠界原本的訊息是中文,所以常見的代碼都附上英文與日文說明。
|
|
203
|
+
|
|
204
|
+
<!-- runnable -->
|
|
205
|
+
```ruby
|
|
206
|
+
begin
|
|
207
|
+
invoices.issue(invoice) # 上面已經用過這個訂單編號,所以綠界會拒絕
|
|
208
|
+
rescue TaiwanPayments::ApiError => error
|
|
209
|
+
error.code # => "5070357"
|
|
210
|
+
error.message # => "ECPay error 5070357: This order number (RelateNumber) already has an invoice."
|
|
211
|
+
error.message_zh # => "ECPay 錯誤 5070357:B2C開立發票 自訂編號重覆,請重新設定"
|
|
212
|
+
error.message_ja # => "ECPay エラー 5070357:この注文番号(RelateNumber)の発票はすでに発行されています。"
|
|
213
|
+
end
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
| 例外 | 意義 |
|
|
217
|
+
| --- | --- |
|
|
218
|
+
| `ValidationError` | 輸入的資料不符合廠商規則。在連線之前就會拋出,`error.field` 會指出是哪個參數。 |
|
|
219
|
+
| `ApiError` | 廠商拒絕了這個請求。`error.code` 是廠商的代碼。 |
|
|
220
|
+
| `SignatureError` | 通知或回應不是用你的金鑰簽的,請視為惡意請求。 |
|
|
221
|
+
| `TransportError` | 網路連線失敗。`HttpError` 與 `RateLimitedError` 是它的子類別。 |
|
|
222
|
+
| `ConfigurationError` | 金鑰缺漏或格式不正確。 |
|
|
223
|
+
|
|
224
|
+
設定 `TaiwanPayments.locale = :zh_tw` 之後,`message` 會回傳中文訊息;設定為 `:ja` 則回傳日文訊息。
|
|
225
|
+
|
|
226
|
+
## 測試你的應用程式
|
|
227
|
+
|
|
228
|
+
<!-- runnable -->
|
|
229
|
+
```ruby
|
|
230
|
+
require "taiwan_payments/testing"
|
|
231
|
+
|
|
232
|
+
# 一筆你的 controller 會視為真實的通知:
|
|
233
|
+
params = TaiwanPayments::Testing.ecpay_payment_notification(payments, order_no: "ORD1001", amount: 1200)
|
|
234
|
+
|
|
235
|
+
# 一份加密過的 API 回應,可搭配 WebMock 之類的工具使用:
|
|
236
|
+
body = TaiwanPayments::Testing.ecpay_invoice_issued_body(invoices, invoice_no: "AB12345678")
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
```ruby
|
|
240
|
+
# 在 request spec 中:
|
|
241
|
+
post webhooks_ecpay_payment_path, params: params
|
|
242
|
+
expect(response.body).to eq("1|OK")
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
付款失敗、ATM 與超商代碼,以及如何檢查送出的內容,請見 [Testing](docs/testing.md)。
|
|
246
|
+
|
|
247
|
+
## 命令列工具
|
|
248
|
+
|
|
249
|
+
```sh
|
|
250
|
+
taiwan_payments glossary 載具 # 載具是什麼?對應哪個欄位?
|
|
251
|
+
taiwan_payments glossary RelateNumber
|
|
252
|
+
taiwan_payments tax-id 53538851 # 檢查統一編號
|
|
253
|
+
taiwan_payments ecpay checkout --open # 在瀏覽器開啟測試付款頁
|
|
254
|
+
taiwan_payments ecpay query ORD1001 # 向綠界查詢訂單
|
|
255
|
+
taiwan_payments ecpay invoice # 開立一張測試發票
|
|
256
|
+
taiwan_payments ecpay mac 'A=1&B=2' # 計算或驗證 CheckMacValue
|
|
257
|
+
taiwan_payments ecpay decrypt 'BASE64' # 解開電子發票的加密內容
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
## 文件
|
|
261
|
+
|
|
262
|
+
以下文件除特別標示外皆為英文。
|
|
263
|
+
|
|
264
|
+
- [API 指南](https://randomizor.github.io/taiwan_payments/zh-TW/):不限程式語言的廠商 API 說明(English、繁體中文、日本語)
|
|
265
|
+
- [名詞對照表](https://randomizor.github.io/taiwan_payments/zh-TW/glossary.html):金流與發票名詞的中文、英文、日文對照
|
|
266
|
+
- [台灣金流入門](docs/taiwan-payments-101.zh-TW.md)([English](docs/taiwan-payments-101.md)、[日本語](docs/taiwan-payments-101.ja.md))
|
|
267
|
+
- [ECPay payments](docs/ecpay-payments.md):綠界金流
|
|
268
|
+
- [ECPay e-invoices](docs/ecpay-invoices.md):綠界電子發票
|
|
269
|
+
- [Webhooks](docs/webhooks.md):付款通知
|
|
270
|
+
- [Rails](docs/rails.md)
|
|
271
|
+
- [Testing](docs/testing.md):測試
|
|
272
|
+
- [Sandbox setup](docs/sandbox-setup.md):測試環境
|
|
273
|
+
- 對照表(中英並列):[名詞解釋](docs/glossary/concepts.md)、[綠界金流欄位](docs/glossary/ecpay-payment.md)、[綠界電子發票欄位](docs/glossary/ecpay-invoice.md)。日文版:[用語](docs/glossary/concepts.ja.md)、[決済フィールド](docs/glossary/ecpay-payment.ja.md)、[電子発票フィールド](docs/glossary/ecpay-invoice.ja.md)
|
|
274
|
+
|
|
275
|
+
## 開發
|
|
276
|
+
|
|
277
|
+
```sh
|
|
278
|
+
bundle install
|
|
279
|
+
bundle exec rake # 測試與程式碼風格檢查
|
|
280
|
+
TAIWAN_PAYMENTS_SANDBOX=1 bundle exec rspec --tag sandbox # 同時呼叫綠界測試環境
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
請見 [CONTRIBUTING.md](CONTRIBUTING.md)。安全性問題請依 [SECURITY.md](SECURITY.md) 的方式回報。
|
|
284
|
+
|
|
285
|
+
## 支持這個專案
|
|
286
|
+
|
|
287
|
+
如果這個 gem 幫你省下了時間,歡迎[請我喝杯咖啡](https://buymeacoffee.com/randomizor)。
|
|
288
|
+
|
|
289
|
+
## 授權
|
|
290
|
+
|
|
291
|
+
MIT,詳見 [LICENSE.txt](LICENSE.txt)。
|
|
292
|
+
|
|
293
|
+
本專案與綠界科技、藍新金流、ezPay 無任何關係。
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
# ECPay e-invoices
|
|
2
|
+
|
|
3
|
+
ECPay issues uniform invoices (統一發票) on your behalf and reports them to the Ministry of Finance. This gem uses ECPay's B2C e-invoice API, which covers both consumer invoices and invoices that carry a buyer's company tax ID. That is everything an online shop issues.
|
|
4
|
+
|
|
5
|
+
Every vendor field mentioned here is explained in the [ECPay e-invoice glossary](glossary/ecpay-invoice.md). For carriers, donation codes and tax IDs, read [Taiwan payments 101](taiwan-payments-101.md).
|
|
6
|
+
|
|
7
|
+
## The client
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
# ECPay's public test merchant. Nothing to sign up for.
|
|
11
|
+
invoices = TaiwanPayments::ECPay::InvoiceClient.sandbox
|
|
12
|
+
|
|
13
|
+
# Your own merchant.
|
|
14
|
+
invoices = TaiwanPayments::ECPay::InvoiceClient.new(
|
|
15
|
+
merchant_id: "1234567",
|
|
16
|
+
hash_key: ENV.fetch("ECPAY_INVOICE_HASH_KEY"),
|
|
17
|
+
hash_iv: ENV.fetch("ECPAY_INVOICE_HASH_IV"),
|
|
18
|
+
env: :production
|
|
19
|
+
)
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
The e-invoice product has its own merchant ID and keys. They are **not** the ones from the payment product, and each must be exactly 16 characters.
|
|
23
|
+
|
|
24
|
+
## Describing an invoice
|
|
25
|
+
|
|
26
|
+
`TaiwanPayments::Invoice` is independent of ECPay, so the same object will work with other providers as they are added.
|
|
27
|
+
|
|
28
|
+
```ruby
|
|
29
|
+
invoice = TaiwanPayments::Invoice.new(
|
|
30
|
+
order_no: "ORD1001",
|
|
31
|
+
buyer: {email: "buyer@example.com"},
|
|
32
|
+
items: [
|
|
33
|
+
{name: "Oolong tea", quantity: 2, unit: "罐", unit_price: 500},
|
|
34
|
+
{name: "Shipping", unit_price: 50},
|
|
35
|
+
{name: "Coupon", unit_price: -100}
|
|
36
|
+
],
|
|
37
|
+
remark: "Thank you"
|
|
38
|
+
)
|
|
39
|
+
invoice.total # => 950
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Items
|
|
43
|
+
|
|
44
|
+
| Key | Default | Notes |
|
|
45
|
+
| --- | --- | --- |
|
|
46
|
+
| `name:` | required | Up to 500 characters at ECPay |
|
|
47
|
+
| `unit_price:` | required | Tax included. May be negative for a discount line. |
|
|
48
|
+
| `quantity:` | `1` | Must be greater than 0. Decimals are allowed. |
|
|
49
|
+
| `unit:` | `"式"` | The customary catch-all unit. Up to 6 characters at ECPay. |
|
|
50
|
+
| `amount:` | `quantity * unit_price` | Override when a line is discounted as a whole |
|
|
51
|
+
| `tax_type:` | the invoice's | Only needed on a `:mixed` invoice |
|
|
52
|
+
| `remark:` | none | |
|
|
53
|
+
|
|
54
|
+
Prices include tax, which is how consumer prices are quoted in Taiwan. Arithmetic is done in exact decimals, so `3 * 34.9` is `104.7` and not `104.69999999999999`. The invoice total is the sum of the lines rounded half up to a whole dollar, which is what ECPay checks.
|
|
55
|
+
|
|
56
|
+
### Buyer
|
|
57
|
+
|
|
58
|
+
| Key | Notes |
|
|
59
|
+
| --- | --- |
|
|
60
|
+
| `email:` | ECPay sends the invoice notice here. Either `email` or `phone` is required. |
|
|
61
|
+
| `phone:` | Mobile number, digits only |
|
|
62
|
+
| `name:`, `address:` | Required on a printed invoice |
|
|
63
|
+
| `tax_id:` | The company's 8-digit 統一編號. Makes it a business invoice. |
|
|
64
|
+
| `customer_id:` | Your own ID for the customer |
|
|
65
|
+
|
|
66
|
+
### How the buyer receives it
|
|
67
|
+
|
|
68
|
+
| Situation | Pass | Sent to ECPay as |
|
|
69
|
+
| --- | --- | --- |
|
|
70
|
+
| Nothing special (default for consumers) | nothing | Member carrier. ECPay emails the buyer. |
|
|
71
|
+
| Mobile barcode | `carrier: "/ABC+123"` | `CarrierType` 3 |
|
|
72
|
+
| Citizen digital certificate | `carrier: "AB12345678901234"` | `CarrierType` 2 |
|
|
73
|
+
| Donation | `donation_code: "168001"` | `Donation` 1, `LoveCode` |
|
|
74
|
+
| Paper | `print: true`, with buyer `name` and `address` | `Print` 1 |
|
|
75
|
+
| Company (default when `tax_id` is given) | `buyer: {tax_id: "53538851", name: "...", address: "...", email: "..."}` | `CustomerIdentifier`, `Print` 1 |
|
|
76
|
+
| Company, to their mobile barcode | the above with `carrier: "/ABC+123"` | `CarrierType` 3, `Print` 0 |
|
|
77
|
+
| No carrier at all | `carrier: false` | Stored by ECPay only |
|
|
78
|
+
|
|
79
|
+
A carrier given as a string is recognised by its format. You can also be explicit: `carrier: :member`, or `carrier: {type: :mobile_barcode, number: "/ABC+123"}`.
|
|
80
|
+
|
|
81
|
+
Combinations ECPay would refuse are caught locally with a `ValidationError`, for example donating a business invoice, printing an invoice that is stored in a carrier, or a business invoice with neither a carrier nor print.
|
|
82
|
+
|
|
83
|
+
### Tax types
|
|
84
|
+
|
|
85
|
+
```ruby
|
|
86
|
+
# attributes holds order_no:, buyer: and items: as above
|
|
87
|
+
TaiwanPayments::Invoice.new(**attributes, tax_type: :exempt)
|
|
88
|
+
|
|
89
|
+
TaiwanPayments::Invoice.new(**attributes, tax_type: :zero_rated, customs_clearance: :customs, zero_tax_rate_reason: 71)
|
|
90
|
+
|
|
91
|
+
TaiwanPayments::Invoice.new(**attributes, tax_type: :mixed, items: [
|
|
92
|
+
{name: "Tea", unit_price: 100, tax_type: :taxable},
|
|
93
|
+
{name: "Rice", unit_price: 50, tax_type: :exempt}
|
|
94
|
+
])
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
`tax_type:` is `:taxable` (default), `:zero_rated`, `:exempt` or `:mixed`. A zero-rated sale needs `customs_clearance:` (`:customs` or `:non_customs`) and a `zero_tax_rate_reason:` from 71 to 79, the statutory reason codes the Ministry of Finance requires from 2026. ECPay's stage environment currently accepts a zero-rated invoice without the reason, but the gem requires it so that you find out before production does.
|
|
98
|
+
|
|
99
|
+
## Issuing
|
|
100
|
+
|
|
101
|
+
```ruby
|
|
102
|
+
result = invoices.issue(invoice)
|
|
103
|
+
result.invoice_no # "AB12345678"
|
|
104
|
+
result.issued_at # Time, in Taiwan time
|
|
105
|
+
result.random_number # "1234", the 4-digit code on the invoice
|
|
106
|
+
result.raw # ECPay's decrypted response
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Store `invoice_no` and `issued_at`. Every later operation on the invoice needs both.
|
|
110
|
+
|
|
111
|
+
`issue(invoice, extra: {"InvType" => "08"})` adds or overrides raw ECPay fields for options the gem does not model.
|
|
112
|
+
|
|
113
|
+
Calling `issue` twice with the same `order_no` raises `ApiError` with code `5070357`. That makes `order_no` a safe idempotency key: if a request times out, look the invoice up with `find(order_no:)` before trying again.
|
|
114
|
+
|
|
115
|
+
## Voiding and crediting
|
|
116
|
+
|
|
117
|
+
```ruby
|
|
118
|
+
invoices.void(invoice_no: "AB12345678", issued_on: result.issued_at, reason: "Order cancelled")
|
|
119
|
+
|
|
120
|
+
credit = invoices.allowance(
|
|
121
|
+
invoice_no: "AB12345678",
|
|
122
|
+
issued_on: result.issued_at,
|
|
123
|
+
items: [{name: "Shipping", unit_price: 50}],
|
|
124
|
+
email: "buyer@example.com" # or phone:, or both, or neither for no notice
|
|
125
|
+
)
|
|
126
|
+
credit.allowance_no # keep this to void the allowance later
|
|
127
|
+
credit.remaining_amount # what can still be credited against the invoice
|
|
128
|
+
|
|
129
|
+
invoices.void_allowance(invoice_no: "AB12345678", allowance_no: credit.allowance_no, reason: "Entered twice")
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
`issued_on` accepts a `Time`, a `Date` or a string. Only the calendar day in Taiwan time is used. `reason` is limited to 20 characters.
|
|
133
|
+
|
|
134
|
+
Void when the whole sale is cancelled and the two-month period is still open. Use an allowance for partial refunds and for anything after the period has closed.
|
|
135
|
+
|
|
136
|
+
## Looking up an invoice
|
|
137
|
+
|
|
138
|
+
```ruby
|
|
139
|
+
record = invoices.find(order_no: "ORD1001")
|
|
140
|
+
record = invoices.find(invoice_no: "AB12345678", issued_on: "2026-12-03")
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Returns `nil` when there is no such invoice. Otherwise an `InvoiceRecord` with `invoice_no`, `order_no`, `total`, `tax_amount`, `issued_at`, `random_number`, `buyer`, `carrier_type`, `carrier_number`, `donation_code`, `remaining_allowance_amount`, the predicates `voided?`, `uploaded?` (reported to the Ministry of Finance) and `printed?`, and `raw` with everything else, including the items.
|
|
144
|
+
|
|
145
|
+
ECPay reports `tax_amount` as 0 for consumer invoices, where tax is not shown separately. It is filled in for invoices with a tax ID.
|
|
146
|
+
|
|
147
|
+
## Issuing later
|
|
148
|
+
|
|
149
|
+
```ruby
|
|
150
|
+
invoices.schedule(invoice, days: 7) # ECPay issues it automatically in 7 days (1 to 15)
|
|
151
|
+
|
|
152
|
+
held = invoices.schedule(invoice) # held until you say so
|
|
153
|
+
invoices.trigger(held.reference) # issue it now
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Useful when the invoice should wait for a return period or for shipping. `reference:` defaults to the invoice's `order_no` and identifies the pending invoice. `notify_url:` asks ECPay to POST to you once the invoice exists.
|
|
157
|
+
|
|
158
|
+
`trigger` does not return the invoice number, because ECPay does not send it. Fetch it with `find(order_no:)`. The gem does not yet parse the `notify_url` callback.
|
|
159
|
+
|
|
160
|
+
## Printing
|
|
161
|
+
|
|
162
|
+
```ruby
|
|
163
|
+
url = invoices.print_url(invoice_no: "AB12345678", issued_on: result.issued_at)
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
Returns a link to ECPay's printable page. It exists only for invoices issued with print enabled, and it expires, so fetch it when the buyer asks instead of storing it. For other invoices ECPay answers with code `7`.
|
|
167
|
+
|
|
168
|
+
## Checking what the buyer typed
|
|
169
|
+
|
|
170
|
+
```ruby
|
|
171
|
+
invoices.mobile_barcode_valid?("/ABC+123") # is this carrier registered with the Ministry of Finance?
|
|
172
|
+
invoices.donation_code_valid?("168001") # is this a registered charity?
|
|
173
|
+
TaiwanPayments::TaxId.valid?("53538851") # checksum only, no network
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
The first two call ECPay, which asks the Ministry of Finance. Malformed input returns `false` without a network call. If the lookup service is down they raise `ApiError` instead of returning `false`, so an outage is not mistaken for a typo. In that case most shops accept the value and let issuing decide.
|
|
177
|
+
|
|
178
|
+
ECPay's stage environment answers yes to every well-formed barcode and donation code.
|
|
179
|
+
|
|
180
|
+
## Errors
|
|
181
|
+
|
|
182
|
+
`ValidationError` is raised before any network call. Its message names the argument and the ECPay field:
|
|
183
|
+
|
|
184
|
+
```
|
|
185
|
+
buyer.name (CustomerName) is required for a printed invoice
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
`ApiError` carries ECPay's code. The ones you are most likely to meet:
|
|
189
|
+
|
|
190
|
+
| Code | Meaning |
|
|
191
|
+
| --- | --- |
|
|
192
|
+
| `T110` | ECPay could not decrypt the request. Wrong keys, usually the payment product's. |
|
|
193
|
+
| `T104` | The request timestamp is more than 10 minutes off. Check the server clock. |
|
|
194
|
+
| `5070357` | This `order_no` already has an invoice |
|
|
195
|
+
| `5000034` | The tax ID fails its checksum |
|
|
196
|
+
| `5070453` | The invoice was already voided |
|
|
197
|
+
| `1500047`, `5070350` | Invoice number track (字軌) not set up, or used up |
|
|
198
|
+
| `2` | Not found. `find` returns `nil` for this instead of raising. |
|
|
199
|
+
|
|
200
|
+
Codes starting with `T` come from the request envelope (`TransCode`). The rest are `RtnCode` values. A code without a translation shows ECPay's own Chinese message.
|
|
201
|
+
|
|
202
|
+
Read an encrypted payload from a log with `taiwan_payments ecpay decrypt`.
|