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,65 @@
|
|
|
1
|
+
<!-- Generated by `rake docs:glossary` from lib/taiwan_payments/glossary/ecpay_payment.yml. Edit that file, not this one. -->
|
|
2
|
+
|
|
3
|
+
# ECPay payment fields / 綠界全方位金流欄位對照表
|
|
4
|
+
|
|
5
|
+
Each row gives a field or term, its Chinese name, what it means, and where to find it in `taiwan_payments`.
|
|
6
|
+
每一列為欄位或名詞、中文名稱、英文說明,以及在 `taiwan_payments` 中對應的寫法。
|
|
7
|
+
|
|
8
|
+
Reference / 參考資料: https://developers.ecpay.com.tw/?p=2862
|
|
9
|
+
|
|
10
|
+
| Term | 中文 | Meaning | In this gem |
|
|
11
|
+
| --- | --- | --- | --- |
|
|
12
|
+
| `MerchantID` | 特店編號 | Merchant ID issued by ECPay. Payments and e-invoices each have their own. | `merchant_id:` on the client |
|
|
13
|
+
| `PlatformID` | 特約合作平台商代號 | Platform merchant ID, only for marketplaces acting for sub-merchants. | `platform_id:` on the client |
|
|
14
|
+
| `CheckMacValue` | 檢查碼 | SHA256 signature over every other field. The gem adds it to requests and verifies it on callbacks. | `client.check_mac_value` |
|
|
15
|
+
| `EncryptType` | CheckMacValue 加密類型 | Signature algorithm. Always 1 (SHA256). | set automatically |
|
|
16
|
+
| `MerchantTradeNo` | 特店訂單編號 | Your unique order number, 1 to 20 letters and digits. ECPay rejects a reused one. | `order_no` |
|
|
17
|
+
| `MerchantTradeDate` | 特店交易時間 | When the order was created, in Taiwan time (yyyy/MM/dd HH:mm:ss). | `trade_date:` |
|
|
18
|
+
| `PaymentType` | 交易類型 / 付款方式 | Always "aio" in a checkout request. In callbacks it is the detailed method used, such as Credit_CreditCard or ATM_TAISHIN. | `payment_type`, `payment_method` |
|
|
19
|
+
| `TotalAmount` | 交易金額 | Amount in whole TWD. | `amount:` |
|
|
20
|
+
| `TradeDesc` | 交易描述 | Order description. Not shown to the customer on most payment pages. | `description:` |
|
|
21
|
+
| `ItemName` | 商品名稱 | Item names shown on the payment page, joined with "#". | `items:` |
|
|
22
|
+
| `ReturnURL` | 付款完成通知回傳網址 | Server-to-server webhook that receives the payment result. Answer it with "1\|OK". | `return_url:` |
|
|
23
|
+
| `ChoosePayment` | 選擇預設付款方式 | Which payment method the page offers. ALL lets the customer choose. | `payment_method:` |
|
|
24
|
+
| `ChooseSubPayment` | 付款子項目 | Narrows the method further, for example one bank for ATM transfers. | `sub_method:` |
|
|
25
|
+
| `IgnorePayment` | 隱藏付款方式 | Methods to hide when ChoosePayment is ALL, joined with "#". | `ignore_methods:` |
|
|
26
|
+
| `OrderResultURL` | Client 端回傳付款結果網址 | The customer's browser is POSTed here with the result after paying. Must differ from ReturnURL. | `order_result_url:` |
|
|
27
|
+
| `ClientBackURL` | Client 端返回特店的按鈕連結 | Target of the "back to shop" button on ECPay's page. Carries no payment result. | `client_back_url:` |
|
|
28
|
+
| `PaymentInfoURL` | Server 端回傳付款相關資訊 | Webhook that receives the ATM account or convenience store code the customer must pay to. | `payment_info_url:` |
|
|
29
|
+
| `ClientRedirectURL` | Client 端回傳付款相關資訊 | The customer's browser is POSTed here with the ATM account or store code, instead of seeing ECPay's page. | `client_redirect_url:` |
|
|
30
|
+
| `ItemURL` | 商品銷售網址 | Link to the product page. | `item_url:` |
|
|
31
|
+
| `Remark` | 備註欄位 | Free-form note stored with the order. | `remark:` |
|
|
32
|
+
| `StoreID` | 特店旗下店舖代號 | Your own branch or store code, echoed back in callbacks. | `store_id:` |
|
|
33
|
+
| `Language` | 語系設定 | Language of the payment page. ENG, KOR, JPN or CHI; Traditional Chinese when omitted. | `language:` |
|
|
34
|
+
| `NeedExtraPaidInfo` | 是否需要額外的付款資訊 | Y adds details such as card digits and bank codes to the callback. | `extra_paid_info:` |
|
|
35
|
+
| `CustomField1` | 自訂名稱欄位 1 | Up to 50 characters of your own data, echoed back in callbacks. | `custom_fields:` |
|
|
36
|
+
| `CustomField2` | 自訂名稱欄位 2 | See CustomField1. | `custom_fields:` |
|
|
37
|
+
| `CustomField3` | 自訂名稱欄位 3 | See CustomField1. | `custom_fields:` |
|
|
38
|
+
| `CustomField4` | 自訂名稱欄位 4 | See CustomField1. | `custom_fields:` |
|
|
39
|
+
| `ExpireDate` | 允許繳費有效天數 / 繳費期限 | In a checkout request, days allowed to pay an ATM transfer (1 to 60). In callbacks, the payment deadline. | `atm_expire_days:`, `payment_instructions.expires_at` |
|
|
40
|
+
| `StoreExpireDate` | 超商繳費截止時間 | Time allowed to pay at a convenience store. Minutes for a store code, days for a barcode. | `store_expire:` |
|
|
41
|
+
| `Desc_1` | 交易描述 1 | Line printed on the convenience store kiosk slip. | `store_descriptions:` |
|
|
42
|
+
| `Desc_2` | 交易描述 2 | See Desc_1. | `store_descriptions:` |
|
|
43
|
+
| `Desc_3` | 交易描述 3 | See Desc_1. | `store_descriptions:` |
|
|
44
|
+
| `Desc_4` | 交易描述 4 | See Desc_1. | `store_descriptions:` |
|
|
45
|
+
| `CreditInstallment` | 刷卡分期期數 | Instalment terms to offer, for example 3,6,12. | `installments:` |
|
|
46
|
+
| `Redeem` | 信用卡是否使用紅利折抵 | Y lets the customer pay partly with card reward points. | `redeem:` |
|
|
47
|
+
| `UnionPay` | 銀聯卡交易選項 | 0 lets the customer choose, 1 forces UnionPay, 2 hides it. | `union_pay:` |
|
|
48
|
+
| `RtnCode` | 交易狀態 | Result code. 1 is paid, 2 means an ATM account was issued, 10100073 means a store code or barcode was issued. Anything else failed. | `status_code`, `status`, `paid?` |
|
|
49
|
+
| `RtnMsg` | 交易訊息 | ECPay's message for the result, usually Chinese. | `message` |
|
|
50
|
+
| `TradeNo` | 綠界的交易編號 | ECPay's own number for the transaction. Keep it; refunds need it. | `trade_no` |
|
|
51
|
+
| `TradeAmt` | 交易金額 | Amount of the transaction in TWD. Compare it with your order before fulfilling. | `amount` |
|
|
52
|
+
| `TradeDate` | 訂單成立時間 | When ECPay created the transaction. | `ordered_at` |
|
|
53
|
+
| `PaymentDate` | 付款時間 | When the customer paid. Empty until then. | `paid_at` |
|
|
54
|
+
| `PaymentTypeChargeFee` | 交易手續費金額 | ECPay's fee for this transaction. | `fee` |
|
|
55
|
+
| `HandlingCharge` | 手續費合計 | Total handling charge, as reported by a query. | `raw["HandlingCharge"]` |
|
|
56
|
+
| `SimulatePaid` | 是否為模擬付款 | 1 when the callback was triggered from the back office "simulate payment" button. No money moved, so do not ship. | `simulated?` |
|
|
57
|
+
| `TradeStatus` | 交易狀態 | In a query, 0 is unpaid and 1 is paid. Anything else is a failure code. | `status_code`, `status` |
|
|
58
|
+
| `TimeStamp` | 驗證時間 | Unix time of a query. ECPay rejects it after three minutes. | set automatically |
|
|
59
|
+
| `BankCode` | 繳費銀行代碼 | Bank code of the virtual account the customer must transfer to. | `payment_instructions.bank_code` |
|
|
60
|
+
| `vAccount` | 繳費虛擬帳號 | Virtual account number for the ATM transfer. | `payment_instructions.virtual_account` |
|
|
61
|
+
| `PaymentNo` | 繳費代碼 | Code the customer enters at a convenience store kiosk. | `payment_instructions.payment_no` |
|
|
62
|
+
| `Barcode1` | 條碼第一段號碼 | First of three barcodes the customer shows at a convenience store counter. | `payment_instructions.barcodes` |
|
|
63
|
+
| `Barcode2` | 條碼第二段號碼 | See Barcode1. | `payment_instructions.barcodes` |
|
|
64
|
+
| `Barcode3` | 條碼第三段號碼 | See Barcode1. | `payment_instructions.barcodes` |
|
|
65
|
+
| `Action` | 執行動作 | Card operation. C captures (關帳), R refunds (退刷), E cancels a capture (取消關帳), N releases the authorisation (放棄). | `capture`, `refund`, `cancel_capture`, `void_authorization` |
|
data/docs/rails.md
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
# Rails
|
|
2
|
+
|
|
3
|
+
`taiwan_payments` is plain Ruby. When Rails is present it also loads a small integration layer: a view helper, a controller concern, a generator and an `ActiveSupport::Notifications` bridge. Rails 7.1 through 8.1 are tested.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```ruby
|
|
8
|
+
# Gemfile
|
|
9
|
+
gem "taiwan_payments"
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
bin/rails generate taiwan_payments:install
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
The generator creates:
|
|
17
|
+
|
|
18
|
+
- `config/initializers/taiwan_payments.rb`
|
|
19
|
+
- `app/controllers/webhooks/ecpay_controller.rb`
|
|
20
|
+
- the route `post "webhooks/ecpay/payment", to: "webhooks/ecpay#payment"`
|
|
21
|
+
|
|
22
|
+
Pass `--skip-webhooks` to get only the initializer.
|
|
23
|
+
|
|
24
|
+
## Configuration
|
|
25
|
+
|
|
26
|
+
```ruby
|
|
27
|
+
# config/initializers/taiwan_payments.rb
|
|
28
|
+
TaiwanPayments.configure do |config|
|
|
29
|
+
if Rails.env.production?
|
|
30
|
+
config.env = :production
|
|
31
|
+
config.ecpay_payments = Rails.application.credentials.dig(:ecpay, :payments)
|
|
32
|
+
config.ecpay_invoices = Rails.application.credentials.dig(:ecpay, :invoices)
|
|
33
|
+
else
|
|
34
|
+
config.ecpay_payments = :sandbox
|
|
35
|
+
config.ecpay_invoices = :sandbox
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
`:sandbox` means ECPay's public test merchant. For production, store your keys with `bin/rails credentials:edit --environment production`:
|
|
41
|
+
|
|
42
|
+
```yaml
|
|
43
|
+
ecpay:
|
|
44
|
+
payments:
|
|
45
|
+
merchant_id: "1234567"
|
|
46
|
+
hash_key: "................"
|
|
47
|
+
hash_iv: "................"
|
|
48
|
+
invoices:
|
|
49
|
+
merchant_id: "1234567"
|
|
50
|
+
hash_key: "................"
|
|
51
|
+
hash_iv: "................"
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Then use the configured clients anywhere:
|
|
55
|
+
|
|
56
|
+
```ruby
|
|
57
|
+
TaiwanPayments.ecpay_payments # => TaiwanPayments::ECPay::PaymentClient
|
|
58
|
+
TaiwanPayments.ecpay_invoices # => TaiwanPayments::ECPay::InvoiceClient
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Clients are built the first time they are used, so the app still boots where credentials are absent, such as asset precompilation and CI. A missing key raises `TaiwanPayments::ConfigurationError` at that first use.
|
|
62
|
+
|
|
63
|
+
`configure` is a convenience for apps with one merchant per product. If you have several merchants, build clients yourself with `PaymentClient.new` and skip it. Nothing in the gem reads the global configuration implicitly.
|
|
64
|
+
|
|
65
|
+
## Checkout form
|
|
66
|
+
|
|
67
|
+
```ruby
|
|
68
|
+
class CheckoutsController < ApplicationController
|
|
69
|
+
def create
|
|
70
|
+
order = current_user.orders.find(params[:order_id])
|
|
71
|
+
@form = TaiwanPayments.ecpay_payments.checkout(
|
|
72
|
+
order_no: order.number,
|
|
73
|
+
amount: order.total,
|
|
74
|
+
description: "Order #{order.number}",
|
|
75
|
+
items: order.line_items.map(&:name),
|
|
76
|
+
return_url: webhooks_ecpay_payment_url,
|
|
77
|
+
client_back_url: order_url(order)
|
|
78
|
+
)
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
```erb
|
|
84
|
+
<%# app/views/checkouts/create.html.erb %>
|
|
85
|
+
<p>Taking you to ECPay...</p>
|
|
86
|
+
<%= taiwan_payments_form(@form) %>
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
`taiwan_payments_form` renders hidden inputs and a script that submits the form, with a `<noscript>` button as a fallback. If your app sets a Content-Security-Policy with nonces, the helper adds the request's nonce to the script. Without it, a strict policy would block the script and leave the customer on a blank page.
|
|
90
|
+
|
|
91
|
+
Options are passed to `HostedForm#to_html`:
|
|
92
|
+
|
|
93
|
+
```erb
|
|
94
|
+
<%= taiwan_payments_form(@form, auto_submit: false, submit_text: "Pay with ECPay") %>
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
`return_url` must be an absolute public URL, so use the `_url` route helper and make sure `default_url_options` has your real host. In development, pass your tunnel's URL.
|
|
98
|
+
|
|
99
|
+
## Webhook controller
|
|
100
|
+
|
|
101
|
+
```ruby
|
|
102
|
+
module Webhooks
|
|
103
|
+
class EcpayController < ApplicationController
|
|
104
|
+
include TaiwanPayments::Webhooks
|
|
105
|
+
|
|
106
|
+
def payment
|
|
107
|
+
notification = ecpay_payment_notification
|
|
108
|
+
# ... update the order ...
|
|
109
|
+
render_ecpay_ack
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Including `TaiwanPayments::Webhooks`:
|
|
116
|
+
|
|
117
|
+
- **switches CSRF protection off for that controller.** ECPay cannot send your CSRF token. The signature check replaces it. Keep callbacks in a controller of their own for this reason.
|
|
118
|
+
- adds `ecpay_payment_notification`, which verifies the POST body and returns the notification. Pass a client as the argument if you do not use the global configuration.
|
|
119
|
+
- adds `render_ecpay_ack`, which renders the plain text `1|OK`.
|
|
120
|
+
- rescues `TaiwanPayments::SignatureError` with HTTP 400 and a warning in the log.
|
|
121
|
+
|
|
122
|
+
If `ApplicationController` requires a login, skip that filter in the webhook controller. ECPay cannot log in:
|
|
123
|
+
|
|
124
|
+
```ruby
|
|
125
|
+
skip_before_action :authenticate_user!
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
It works in `ActionController::API` controllers too.
|
|
129
|
+
|
|
130
|
+
See [Webhooks](webhooks.md) for what to do inside the action.
|
|
131
|
+
|
|
132
|
+
## Issuing the invoice
|
|
133
|
+
|
|
134
|
+
Issue invoices from a background job after the order is paid, so that a slow or failed call to ECPay never blocks the payment callback:
|
|
135
|
+
|
|
136
|
+
```ruby
|
|
137
|
+
class IssueInvoiceJob < ApplicationJob
|
|
138
|
+
retry_on TaiwanPayments::TransportError, wait: :polynomially_longer
|
|
139
|
+
|
|
140
|
+
def perform(order)
|
|
141
|
+
return if order.invoice_no.present?
|
|
142
|
+
|
|
143
|
+
invoice = TaiwanPayments::Invoice.new(
|
|
144
|
+
order_no: order.number,
|
|
145
|
+
buyer: {email: order.email, tax_id: order.tax_id, name: order.company_name, address: order.address},
|
|
146
|
+
carrier: order.mobile_barcode.presence,
|
|
147
|
+
donation_code: order.donation_code.presence,
|
|
148
|
+
items: order.line_items.map { |line| {name: line.name, quantity: line.quantity, unit_price: line.unit_price} }
|
|
149
|
+
)
|
|
150
|
+
result = TaiwanPayments.ecpay_invoices.issue(invoice)
|
|
151
|
+
order.update!(invoice_no: result.invoice_no, invoice_issued_at: result.issued_at)
|
|
152
|
+
rescue TaiwanPayments::ApiError => error
|
|
153
|
+
raise unless error.code == "5070357" # already issued for this order number
|
|
154
|
+
|
|
155
|
+
record = TaiwanPayments.ecpay_invoices.find(order_no: order.number)
|
|
156
|
+
order.update!(invoice_no: record.invoice_no, invoice_issued_at: record.issued_at)
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
The gem never retries on its own. ECPay refuses a second invoice for the same `order_no`, which is what makes retrying the job safe.
|
|
162
|
+
|
|
163
|
+
## Instrumentation
|
|
164
|
+
|
|
165
|
+
Every HTTP call to a vendor is published as `request.taiwan_payments`:
|
|
166
|
+
|
|
167
|
+
```ruby
|
|
168
|
+
ActiveSupport::Notifications.subscribe("request.taiwan_payments") do |event|
|
|
169
|
+
Rails.logger.info(
|
|
170
|
+
"[taiwan_payments] #{event.payload[:provider]} #{event.payload[:operation]} " \
|
|
171
|
+
"status=#{event.payload[:status]} #{event.payload[:duration_ms]}ms"
|
|
172
|
+
)
|
|
173
|
+
end
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
The payload has `:provider`, `:operation`, `:url`, `:status`, `:duration_ms`, `:error` when the call raised, and `:params`, the request fields before encryption. Keys, signatures and customer details (name, address, phone, email, carrier number) are replaced with `[REDACTED]` before any subscriber sees them.
|
|
177
|
+
|
|
178
|
+
## Errors in the user's language
|
|
179
|
+
|
|
180
|
+
```ruby
|
|
181
|
+
around_action { |_controller, action| TaiwanPayments.with_locale(I18n.locale, &action) }
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
`error.message` then follows the request's locale: Chinese for any `zh` locale, Japanese for `ja`, English otherwise. `message_en`, `message_zh` and `message_ja` are always available.
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# Sandbox setup
|
|
2
|
+
|
|
3
|
+
How to get a working test environment for each provider.
|
|
4
|
+
|
|
5
|
+
## ECPay: nothing to set up
|
|
6
|
+
|
|
7
|
+
ECPay publishes shared test merchants for its stage environment. They are built into the gem:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
TaiwanPayments::ECPay::PaymentClient.sandbox
|
|
11
|
+
TaiwanPayments::ECPay::InvoiceClient.sandbox
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
| Product | Merchant ID | HashKey | HashIV | Host |
|
|
15
|
+
| --- | --- | --- | --- | --- |
|
|
16
|
+
| Payments | `3002607` | `pwFHCqoQZGmho4w6` | `EkRm7iFT261dpevs` | `payment-stage.ecpay.com.tw` |
|
|
17
|
+
| E-invoices | `2000132` | `ejCk326UnaZWKisg` | `q9jcZX8Ib9LM8wYk` | `einvoice-stage.ecpay.com.tw` |
|
|
18
|
+
|
|
19
|
+
These are public values from ECPay's documentation, not secrets.
|
|
20
|
+
|
|
21
|
+
### Make a test payment
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
taiwan_payments ecpay checkout --open
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
This writes a checkout page and opens it in your browser. Pay with ECPay's test card:
|
|
28
|
+
|
|
29
|
+
- Card number `4311-9522-2222-2222`
|
|
30
|
+
- Any expiry date in the future
|
|
31
|
+
- Security code `222`
|
|
32
|
+
- SMS verification code `1234`
|
|
33
|
+
|
|
34
|
+
Then ask ECPay what happened:
|
|
35
|
+
|
|
36
|
+
```sh
|
|
37
|
+
taiwan_payments ecpay query ORDER_NO
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Receive the callback on your machine
|
|
41
|
+
|
|
42
|
+
ECPay's servers must be able to reach `return_url`, so `localhost` will not work. Run a tunnel and pass its public URL:
|
|
43
|
+
|
|
44
|
+
```sh
|
|
45
|
+
ngrok http 3000
|
|
46
|
+
taiwan_payments ecpay checkout --open --return-url https://YOUR-TUNNEL.ngrok.app/webhooks/ecpay/payment
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Issue a test invoice
|
|
50
|
+
|
|
51
|
+
```sh
|
|
52
|
+
taiwan_payments ecpay invoice
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### The stage back office
|
|
56
|
+
|
|
57
|
+
ECPay's stage back office at `https://vendor-stage.ecpay.com.tw` lists test orders and invoices, and has the "simulate payment" (模擬付款) button for ATM and convenience store orders. The login for each test merchant is published on ECPay's developer site under 測試介接資訊 (`https://developers.ecpay.com.tw`).
|
|
58
|
+
|
|
59
|
+
### What the stage environment does not do
|
|
60
|
+
|
|
61
|
+
- It is shared by every developer integrating with ECPay. Do not send real customer data to it.
|
|
62
|
+
- A successful card capture or refund cannot be produced from a script, because it needs a completed card payment.
|
|
63
|
+
- It answers yes to every well-formed mobile barcode and donation code.
|
|
64
|
+
- It accepts a zero-rated invoice without `ZeroTaxRateReason`. This gem requires the reason anyway.
|
|
65
|
+
|
|
66
|
+
### Going live
|
|
67
|
+
|
|
68
|
+
1. Apply for an ECPay merchant account. Payments and e-invoices are separate applications and give you separate credentials.
|
|
69
|
+
2. Copy the merchant ID, HashKey and HashIV for each product from ECPay's production back office.
|
|
70
|
+
3. For e-invoices, make sure your invoice number tracks (字軌) are set up for the current period.
|
|
71
|
+
4. Build clients with `env: :production`, or set `config.env = :production`. Never commit production keys.
|
|
72
|
+
|
|
73
|
+
## NewebPay: planned for 0.2
|
|
74
|
+
|
|
75
|
+
NewebPay (藍新金流) has no shared test merchant. You register your own on its test site. These steps come from our research and are not yet exercised by the gem. The sites are in Chinese only, and registration expects a Taiwanese mobile number for SMS verification.
|
|
76
|
+
|
|
77
|
+
1. Register a member account at the test site, `https://cwww.newebpay.com`. The `c` prefix marks the test environment.
|
|
78
|
+
2. In 會員中心, open 商店管理 and choose 開立商店 to create a store. Choosing 個人 (individual) avoids uploading company documents.
|
|
79
|
+
3. Open the store's details. Under API 串接金鑰 you will find the 商店代號 (merchant ID, starting with `MS`), the HashKey (32 characters) and the HashIV (16 characters).
|
|
80
|
+
4. Enable the payment methods you want to test on the store: 信用卡, WebATM, ATM 轉帳, 超商代碼.
|
|
81
|
+
5. Download the current MPG integration manual from the back office.
|
|
82
|
+
6. Keep the keys out of the repository:
|
|
83
|
+
|
|
84
|
+
```sh
|
|
85
|
+
export NEWEBPAY_MERCHANT_ID=MS...
|
|
86
|
+
export NEWEBPAY_HASH_KEY=...
|
|
87
|
+
export NEWEBPAY_HASH_IV=...
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## ezPay: planned for 0.3
|
|
91
|
+
|
|
92
|
+
ezPay (簡單付) e-invoices also need your own test merchant. These steps come from our research and are not yet exercised by the gem.
|
|
93
|
+
|
|
94
|
+
1. Register at the test site, `https://cinv.ezpay.com.tw`.
|
|
95
|
+
2. Create a store under 商店管理 and copy its merchant ID, HashKey and HashIV from the API key page.
|
|
96
|
+
3. If the test back office asks for them, add test invoice number tracks (字軌) before issuing.
|
|
97
|
+
4. Download the e-invoice API manual from the back office.
|
|
98
|
+
5. Keep the keys out of the repository:
|
|
99
|
+
|
|
100
|
+
```sh
|
|
101
|
+
export EZPAY_MERCHANT_ID=...
|
|
102
|
+
export EZPAY_HASH_KEY=...
|
|
103
|
+
export EZPAY_HASH_IV=...
|
|
104
|
+
```
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# 台湾決済入門
|
|
2
|
+
|
|
3
|
+
[English](taiwan-payments-101.md) | [繁體中文](taiwan-payments-101.zh-TW.md) | **日本語**
|
|
4
|
+
|
|
5
|
+
台湾でのオンライン販売が初めての開発者に向けた、10 分で読める概要です。`taiwan_payments` やベンダーのドキュメントに出てくるフィールドの背景にある考え方を説明します。用語を個別に調べたいときは、[用語集](glossary/concepts.ja.md)を参照してください。
|
|
6
|
+
|
|
7
|
+
これは開発者向けの手引きであり、税務や法律に関する助言ではありません。自社の義務については、会計の専門家(會計師または記帳士)に確認してください。
|
|
8
|
+
|
|
9
|
+
## 要点
|
|
10
|
+
|
|
11
|
+
1. 銀行と直接やり取りすることはありません。ECPay(綠界科技)や NewebPay(藍新金流)などの決済プロバイダー(金流公司)と契約し、加盟店 ID とキーを受け取ります。
|
|
12
|
+
2. 決済を受けるには、購入者のブラウザーをプロバイダーのページへ送ります。結果は、プロバイダーが署名付きのコールバック(Webhook 通知)でサーバーに知らせます。
|
|
13
|
+
3. よく使われる支払い方法のいくつかは、即時に完了しません。購入者は銀行口座番号かコンビニ決済コード(超商代碼)を受け取り、数時間後や数日後に支払います。
|
|
14
|
+
4. すべての販売に統一発票(統一發票)が必要です。オンラインショップはプロバイダーを通じて電子的に発行し、プロバイダーが財政部に報告します。
|
|
15
|
+
5. 発票は宝くじ券でもあるので、購入者は自分の発票の行き先を気にします。キャリア(載具)と寄付コード(愛心碼)は、そのための仕組みです。
|
|
16
|
+
|
|
17
|
+
## 第 1 部:決済
|
|
18
|
+
|
|
19
|
+
### 決済の流れ
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
your server customer's browser provider
|
|
23
|
+
| build signed form -> | |
|
|
24
|
+
| | POST the form ------------> |
|
|
25
|
+
| | pays on the provider's page
|
|
26
|
+
| <---------------- signed callback (server to server) ------ |
|
|
27
|
+
| answer "1|OK" -------------------------------------------> |
|
|
28
|
+
| | <---- sent back to your site |
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
この流れから、2 つのことがわかります。
|
|
32
|
+
|
|
33
|
+
**サーバーへのコールバックを正とします。** 購入者は、サイトに戻される前にタブを閉じることがあります。ブラウザーが戻ってきたからといって、注文を支払い済みにしてはいけません。署名付きのコールバックが支払い済みを知らせてきたときに、支払い済みにします。`taiwan_payments` では、署名の検証が済むまで内容を読めません。
|
|
34
|
+
|
|
35
|
+
**決済リクエストはブラウザーから送る必要があります。** プロバイダーは、サーバーから送られた決済リクエストを拒否します。`checkout` が HTTP 呼び出しを行わず、出力用のフォームを返すのはそのためです。
|
|
36
|
+
|
|
37
|
+
### 支払い方法
|
|
38
|
+
|
|
39
|
+
| 支払い方法 | 中国語 | 購入者の支払い方 | 即時か |
|
|
40
|
+
| --- | --- | --- | --- |
|
|
41
|
+
| クレジットカード | 信用卡 | プロバイダーのページでカード番号を入力します。通常は SMS の認証コードも使います。 | はい |
|
|
42
|
+
| 分割払い | 分期付款 | カード決済を 3〜30 か月に分割します。 | はい |
|
|
43
|
+
| ATM 振込 | ATM 轉帳、虛擬帳號 | プロバイダーが、1 回限りの ATM 振込用のバーチャル口座を発行します。購入者は ATM か銀行アプリでその口座に振り込みます。 | いいえ |
|
|
44
|
+
| WebATM | 網路 ATM | 購入者のパソコンに接続したカードリーダーに、キャッシュカードを挿して支払います。 | はい |
|
|
45
|
+
| コンビニ決済コード | 超商代碼 | 購入者が店頭端末にコードを入力し、レジで現金で支払います。 | いいえ |
|
|
46
|
+
| コンビニバーコード | 超商條碼 | 購入者がレジで 3 つのバーコードを提示し、現金で支払います。 | いいえ |
|
|
47
|
+
| モバイルウォレット | Apple Pay、TWQR 等 | アプリ内で支払うか、コードを読み取って支払います。 | はい |
|
|
48
|
+
|
|
49
|
+
コンビニ(7-ELEVEN、FamilyMart、Hi-Life、OK Mart)はどこにでもあり、24 時間営業です。オンラインでカード番号を入力するより、コンビニで現金で支払うほうを好む人も多くいます。消費者向けに販売するなら、注文のかなりの割合が即時でない支払い方法になると考えておいてください。
|
|
50
|
+
|
|
51
|
+
### 即時に完了しない決済
|
|
52
|
+
|
|
53
|
+
ATM 振込とコンビニ決済では、流れに手順が 1 つ加わります。
|
|
54
|
+
|
|
55
|
+
1. 購入者が支払い方法を選びます。プロバイダーが支払い用の番号を発行します。これを取號と呼びます。
|
|
56
|
+
2. サーバーが、番号が発行されたことを知らせるコールバックを受け取ります。`taiwan_payments` では `notification.awaiting_payment?` がこれにあたります。`notification.payment_instructions` には、口座番号またはコードと、その支払い期限が入っています。
|
|
57
|
+
3. 購入者が支払います。数日後になることもあります。サーバーが、注文が支払い済みになったことを知らせる 2 回目のコールバックを受け取ります。
|
|
58
|
+
4. 期限を過ぎた場合は、何も届きません。注文の期限切れは自分で処理してください。
|
|
59
|
+
|
|
60
|
+
注文の状態は、これを前提に設計してください。作成済み、支払い待ち、支払い済み、期限切れの 4 つです。
|
|
61
|
+
|
|
62
|
+
### カード決済の用語
|
|
63
|
+
|
|
64
|
+
- **オーソリ**(与信。中国語では授權)は、カードの利用枠を確保します。
|
|
65
|
+
- **売上確定**(請款。ECPay では關帳)は、確保した金額を実際に回収します。ほとんどの加盟店では、毎日自動で行われます。
|
|
66
|
+
- **返金**(退款。カードの場合は退刷)は、売上確定のあとにお金を返します。
|
|
67
|
+
- **ポイント利用**(紅利折抵)は、購入者がカードのポイントで代金の一部を支払える仕組みです。
|
|
68
|
+
|
|
69
|
+
### テスト
|
|
70
|
+
|
|
71
|
+
各プロバイダーには本番とは別のテスト環境があり、専用のキーを使います。ECPay は、誰でも使える共用のテスト加盟店を公開しています。`PaymentClient.sandbox` が設定なしで動くのはそのためです。プロバイダーの管理画面には「支払いシミュレーション」(模擬付款)ボタンもあります。押すと、偽の支払い済みコールバックが送信されます。`taiwan_payments` は、これを `simulated?` として報告し、`paid?` にはしません。このボタンは本番環境にもあるからです。
|
|
72
|
+
|
|
73
|
+
## 第 2 部:発票
|
|
74
|
+
|
|
75
|
+
### 統一発票
|
|
76
|
+
|
|
77
|
+
台湾の事業者は、販売のたびに統一発票(統一發票)を発行しなければなりません。統一発票は、営業税(營業稅)のための公式な販売記録です。営業税は 5% で、消費者向けの価格にはすでに含まれています。
|
|
78
|
+
|
|
79
|
+
財政部は、すべての発票を宝くじ券にしています。各発票には、`AB-12345678` のように英字 2 文字と数字 8 桁の番号が付いています。2 か月ごとに当せん番号の抽せんがあり、賞金は 200 新台湾ドル(TWD)から 1,000 万新台湾ドルまでです。この制度の目的は、消費者が発票を求めるようにして、売上が帳簿に残るようにすることです。制度は実際に機能しています。そのため、発票が届かなかったり、違う場所に届いたりすると、購入者は気づきます。
|
|
80
|
+
|
|
81
|
+
### 電子発票
|
|
82
|
+
|
|
83
|
+
電子発票(電子發票)は、電子的に発行する統一発票です。オンラインショップは、発票を自分では印刷しません。ECPay や ezPay(簡單付)などの認定プロバイダー(加值服務中心)を呼び出します。プロバイダーは番号を割り当て、発票を財政部に報告し、購入者に通知します。
|
|
84
|
+
|
|
85
|
+
発票番号は、発票番号の割当枠(字軌)から採番されます。割当枠は、税務当局が 2 か月の期間ごとに事業者へ割り当てます。アカウントの設定が済めば、割当枠はプロバイダーが管理します。字軌に関するエラーは、割当枠がないか、使い切ったことを意味します。
|
|
86
|
+
|
|
87
|
+
### 購入者の発票はどこへ行くのか
|
|
88
|
+
|
|
89
|
+
発票に関するフィールドのほとんどは、この問いに答えるためのものです。答えは 4 つあります。
|
|
90
|
+
|
|
91
|
+
**キャリア(載具)。** キャリアは、ペーパーレスの発票をひも付けておく識別子です。購入者はこれを使って発票を探し、宝くじの当せん金を受け取れます。
|
|
92
|
+
|
|
93
|
+
- **モバイルバーコード**(手機條碼)が最も一般的です。`/ABC+123` のように、スラッシュと 7 文字からなります。財政部に一度登録すれば、買い物のたびにバーコードを提示するだけです。
|
|
94
|
+
- **自然人憑証**(自然人憑證)は、政府が発行する身分証明カードです。その番号(英字 2 文字と数字 14 桁)もキャリアとして使えます。
|
|
95
|
+
- **会員キャリア**(會員載具)は、プロバイダーが運営するキャリアです。購入者のメールアドレスか電話番号で識別するので、購入者から番号を受け取る必要がありません。発票はプロバイダーが購入者にメールで送ります。購入者が何も指定しないときにショップが使うのがこのキャリアで、`taiwan_payments` の既定値でもあります。
|
|
96
|
+
|
|
97
|
+
**慈善団体。** 購入者が、3〜7 桁の寄付コード(愛心碼。捐贈碼とも呼ばれます)を伝えます。発票は、当せんした場合の賞金も含めて、その慈善団体のものになります。寄付した発票は印刷できません。寄付の取り消しもできません。
|
|
98
|
+
|
|
99
|
+
**紙。** 購入者が印刷(列印)を希望します。印刷した発票は、キャリアには保存されません。
|
|
100
|
+
|
|
101
|
+
**会社の帳簿。** 次の節を参照してください。
|
|
102
|
+
|
|
103
|
+
### 会社向けの発票と統一編号
|
|
104
|
+
|
|
105
|
+
すべての会社と団体は、8 桁の統一編号(統一編號、略して統編)を持っています。会社のために購入する人は、この番号を伝えます。これにより、発票を会社の帳簿に計上でき、その税額を控除できます。統一編号を発票に入れてもらうことを「打統編」と呼びます。台湾の購入手続きページには、ほぼ必ずそのための入力欄があります。
|
|
106
|
+
|
|
107
|
+
統一編号付きの発票には、次の特徴があります。
|
|
108
|
+
|
|
109
|
+
- 税額が別に表示されます。
|
|
110
|
+
- 寄付できません。
|
|
111
|
+
- 宝くじの対象になりません。
|
|
112
|
+
- 通常は印刷するか、会社に送付します。モバイルバーコードに保存することもできます。
|
|
113
|
+
|
|
114
|
+
統一編号の 8 桁には検算のルールがあります。`TaiwanPayments::TaxId.valid?` で検証できるので、自分のフォームで入力ミスを検出できます。
|
|
115
|
+
|
|
116
|
+
### 間違いの訂正:無効化と折譲
|
|
117
|
+
|
|
118
|
+
- **無効化**(作廢)は、発票を完全に取り消します。販売そのものが取り消された場合に使います。発票が属する 2 か月の期間がまだ締め切られていない間だけ可能です。
|
|
119
|
+
- **折譲**(折讓、発行後の減額)は、発票に対するクレジットノートです。一部返金の場合や、期間が締め切られたあとの返金に使います。
|
|
120
|
+
|
|
121
|
+
`taiwan_payments` では、それぞれ `void` と `allowance` です。
|
|
122
|
+
|
|
123
|
+
### 課税区分
|
|
124
|
+
|
|
125
|
+
| 区分 | 中国語 | 適用される場合 |
|
|
126
|
+
| --- | --- | --- |
|
|
127
|
+
| 課税 | 應稅 | 国内で販売するほぼすべてのもの。5% の営業税が価格に含まれます。 |
|
|
128
|
+
| ゼロ税率 | 零稅率 | 主に輸出。通関方式の指定が必要です。2026 年からは法定の理由コードも必要です。 |
|
|
129
|
+
| 免税 | 免稅 | 税法で定められた特定の商品とサービス。 |
|
|
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.ja.md)、[ECPay の決済フィールド](glossary/ecpay-payment.ja.md)、[ECPay の電子発票フィールド](glossary/ecpay-invoice.ja.md)があります。
|
|
138
|
+
- ターミナルからは `taiwan_payments glossary 載具` で調べられます。
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# Taiwan payments 101
|
|
2
|
+
|
|
3
|
+
**English** | [繁體中文](taiwan-payments-101.zh-TW.md) | [日本語](taiwan-payments-101.ja.md)
|
|
4
|
+
|
|
5
|
+
A ten-minute briefing for developers who are new to selling online in Taiwan. It explains the ideas behind the fields you will meet in `taiwan_payments` and in the vendors' documentation. For a term-by-term lookup, see the [concepts glossary](glossary/concepts.md).
|
|
6
|
+
|
|
7
|
+
This is a developer's orientation, not tax or legal advice. Ask your accountant (會計師 or 記帳士) about your own obligations.
|
|
8
|
+
|
|
9
|
+
## The short version
|
|
10
|
+
|
|
11
|
+
1. You do not talk to banks. You sign up with a payment provider (金流公司) such as ECPay (綠界) or NewebPay (藍新), which gives you a merchant ID and keys.
|
|
12
|
+
2. To take a payment you send the customer's browser to the provider's page. The provider tells your server the result with a signed callback.
|
|
13
|
+
3. Several popular payment methods are not instant. The customer gets a bank account number or a convenience store code and pays hours or days later.
|
|
14
|
+
4. Every sale needs a uniform invoice (統一發票). Online shops issue it electronically through a provider, which reports it to the Ministry of Finance.
|
|
15
|
+
5. Invoices are lottery tickets, so customers care where theirs goes. That is what carriers (載具) and donation codes (捐贈碼) are about.
|
|
16
|
+
|
|
17
|
+
## Part 1: Payments
|
|
18
|
+
|
|
19
|
+
### How a payment flows
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
your server customer's browser provider
|
|
23
|
+
| build signed form -> | |
|
|
24
|
+
| | POST the form ------------> |
|
|
25
|
+
| | pays on the provider's page
|
|
26
|
+
| <---------------- signed callback (server to server) ------ |
|
|
27
|
+
| answer "1|OK" -------------------------------------------> |
|
|
28
|
+
| | <---- sent back to your site |
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Two things follow from this shape.
|
|
32
|
+
|
|
33
|
+
**The server callback is the truth.** The customer may close the tab before being sent back to you, so never mark an order paid because the browser returned. Mark it paid when the signed callback says so. `taiwan_payments` verifies the signature before it lets you read anything.
|
|
34
|
+
|
|
35
|
+
**The checkout must come from the browser.** Providers reject a checkout request made from your server. That is why `checkout` returns a form for you to render instead of making an HTTP call.
|
|
36
|
+
|
|
37
|
+
### Payment methods
|
|
38
|
+
|
|
39
|
+
| Method | Chinese | How the customer pays | Instant? |
|
|
40
|
+
| --- | --- | --- | --- |
|
|
41
|
+
| Credit card | 信用卡 | Card number on the provider's page, usually with an SMS code | Yes |
|
|
42
|
+
| Instalments | 分期付款 | Card payment split over 3 to 30 months | Yes |
|
|
43
|
+
| ATM transfer | ATM 轉帳、虛擬帳號 | The provider issues a one-off account number. The customer transfers to it at an ATM or in a banking app. | No |
|
|
44
|
+
| WebATM | 網路 ATM | A bank card in a card reader attached to the customer's computer | Yes |
|
|
45
|
+
| Convenience store code | 超商代碼 | The customer types a code into the store's kiosk, then pays cash at the counter | No |
|
|
46
|
+
| Convenience store barcode | 超商條碼 | The customer shows three barcodes at the counter and pays cash | No |
|
|
47
|
+
| Mobile wallets | Apple Pay、TWQR 等 | In an app or by scanning a code | Yes |
|
|
48
|
+
|
|
49
|
+
Convenience stores (7-ELEVEN, FamilyMart, Hi-Life, OK Mart) are everywhere and open all night, and many people prefer paying cash there to typing a card number online. If you sell to consumers, expect a meaningful share of orders to use the methods that are not instant.
|
|
50
|
+
|
|
51
|
+
### Payments that are not instant
|
|
52
|
+
|
|
53
|
+
For ATM and convenience store payments the flow has an extra step:
|
|
54
|
+
|
|
55
|
+
1. The customer chooses the method. The provider issues a payment code (取號).
|
|
56
|
+
2. Your server receives a callback that says a code was issued. In `taiwan_payments` this is `notification.awaiting_payment?`, and `notification.payment_instructions` holds the account number or code and its deadline.
|
|
57
|
+
3. The customer pays, perhaps days later. Your server receives a second callback that says the order is paid.
|
|
58
|
+
4. If the deadline passes, nothing arrives. Expire the order yourself.
|
|
59
|
+
|
|
60
|
+
Design your order states with this in mind: created, awaiting payment, paid, expired.
|
|
61
|
+
|
|
62
|
+
### Card vocabulary
|
|
63
|
+
|
|
64
|
+
- **Authorisation (授權)** reserves the money on the card.
|
|
65
|
+
- **Capture (請款, or 關帳 at ECPay)** actually collects it. Most merchants have this done automatically each day.
|
|
66
|
+
- **Refund (退款, or 退刷 for cards)** returns money after capture.
|
|
67
|
+
- **Reward points (紅利折抵)** let the customer pay part of the price with card points.
|
|
68
|
+
|
|
69
|
+
### Testing
|
|
70
|
+
|
|
71
|
+
Each provider has a separate test environment with its own keys. ECPay publishes shared test merchants that anyone may use, which is why `PaymentClient.sandbox` works with no setup. Providers also have a "simulate payment" (模擬付款) button in their back office that sends a fake paid callback. `taiwan_payments` reports those as `simulated?` and never as `paid?`, because the button exists in production too.
|
|
72
|
+
|
|
73
|
+
## Part 2: Invoices
|
|
74
|
+
|
|
75
|
+
### The uniform invoice
|
|
76
|
+
|
|
77
|
+
A business in Taiwan must issue a uniform invoice (統一發票) for each sale. It is the official record of the sale for business tax (營業稅), which is 5% and already included in consumer prices.
|
|
78
|
+
|
|
79
|
+
The Ministry of Finance turns every invoice into a lottery ticket. Each invoice has a number, two letters and eight digits such as `AB-12345678`. Every two months winning numbers are drawn, with prizes from NT$200 up to NT$10 million. The scheme exists to make consumers ask for invoices, which keeps sales on the books. It works, and that is why your customers will notice if their invoice is missing or goes to the wrong place.
|
|
80
|
+
|
|
81
|
+
### E-invoices
|
|
82
|
+
|
|
83
|
+
An e-invoice (電子發票) is a uniform invoice issued electronically. An online shop does not print invoices itself. It calls a certified provider (加值服務中心) such as ECPay or ezPay, which assigns the number, reports the invoice to the Ministry of Finance, and notifies the buyer.
|
|
84
|
+
|
|
85
|
+
Invoice numbers come from ranges (字軌) that the tax office allots to your business for each two-month period. Your provider manages them once your account is set up. An error about 字軌 means the range is missing or used up.
|
|
86
|
+
|
|
87
|
+
### Where does the buyer's invoice go?
|
|
88
|
+
|
|
89
|
+
This is the question behind most of the invoice fields. There are four answers.
|
|
90
|
+
|
|
91
|
+
**A carrier (載具).** A carrier is an identifier that paperless invoices are filed under, so the buyer can find them and collect lottery prizes.
|
|
92
|
+
|
|
93
|
+
- The **mobile barcode (手機條碼)** is the common one. It looks like `/ABC+123`: a slash and seven characters. A person registers once with the Ministry of Finance and shows the barcode whenever they buy something.
|
|
94
|
+
- The **citizen digital certificate (自然人憑證)** is a government ID card whose number, two letters and fourteen digits, also works as a carrier.
|
|
95
|
+
- The **member carrier (會員載具)** belongs to the provider. It is keyed by the buyer's email or phone, needs no number from the buyer, and the provider emails them the invoice. This is what shops use when the buyer gives nothing, and it is the default in `taiwan_payments`.
|
|
96
|
+
|
|
97
|
+
**A charity.** The buyer gives a donation code (捐贈碼, also called 愛心碼) of three to seven digits. The invoice, and any prize, goes to that charity. A donated invoice cannot be printed and cannot be taken back.
|
|
98
|
+
|
|
99
|
+
**Paper.** The buyer asks for a printed copy (列印). A printed invoice is not stored in a carrier.
|
|
100
|
+
|
|
101
|
+
**A company's books.** See the next section.
|
|
102
|
+
|
|
103
|
+
### Company invoices and the tax ID
|
|
104
|
+
|
|
105
|
+
Every company and organisation has an 8-digit tax ID (統一編號, often shortened to 統編). A buyer who is purchasing for a company gives this number so the invoice can go into the company's accounts and the tax on it can be deducted. Asking for this is called 打統編, and checkout pages in Taiwan nearly always offer a field for it.
|
|
106
|
+
|
|
107
|
+
An invoice with a tax ID:
|
|
108
|
+
|
|
109
|
+
- shows the tax amount separately,
|
|
110
|
+
- cannot be donated,
|
|
111
|
+
- takes no part in the lottery,
|
|
112
|
+
- is normally printed or sent to the company, and can also be stored under a mobile barcode.
|
|
113
|
+
|
|
114
|
+
A tax ID carries a checksum over its eight digits. `TaiwanPayments::TaxId.valid?` checks it, so you can catch typos in your own form.
|
|
115
|
+
|
|
116
|
+
### Fixing mistakes: void and allowance
|
|
117
|
+
|
|
118
|
+
- **Void (作廢)** cancels an invoice completely. It is for a sale that is cancelled outright, and it is only possible while the two-month period the invoice belongs to is still open.
|
|
119
|
+
- **Allowance (折讓)** is a credit note against an invoice. Use it for a partial refund, or for any refund after the period has closed.
|
|
120
|
+
|
|
121
|
+
In `taiwan_payments` these are `void` and `allowance`.
|
|
122
|
+
|
|
123
|
+
### Tax types
|
|
124
|
+
|
|
125
|
+
| Type | Chinese | When |
|
|
126
|
+
| --- | --- | --- |
|
|
127
|
+
| Taxable | 應稅 | Almost everything sold domestically. 5% business tax, included in the price. |
|
|
128
|
+
| Zero-rated | 零稅率 | Mostly exports. Needs a customs mark and, since 2026, a statutory reason code. |
|
|
129
|
+
| Exempt | 免稅 | Specific goods and services named in the tax law. |
|
|
130
|
+
|
|
131
|
+
Most shops only ever issue taxable invoices, which is the default.
|
|
132
|
+
|
|
133
|
+
## Where to go next
|
|
134
|
+
|
|
135
|
+
- [ECPay payments](ecpay-payments.md) and [ECPay e-invoices](ecpay-invoices.md) for the API.
|
|
136
|
+
- [Webhooks](webhooks.md) for receiving callbacks safely.
|
|
137
|
+
- The glossaries, to translate any field you meet: [concepts](glossary/concepts.md), [ECPay payment fields](glossary/ecpay-payment.md), [ECPay e-invoice fields](glossary/ecpay-invoice.md).
|
|
138
|
+
- From a terminal: `taiwan_payments glossary 載具`.
|