Skip to content

Latest commit

 

History

History
133 lines (117 loc) · 6.55 KB

README.md

File metadata and controls

133 lines (117 loc) · 6.55 KB

ECPay SDK for Go

license PkgGoDev Coverage Status GitHub Actions Go Report Card

Introduction

ECPay SDK for Golang.

ECPay is a third party payment service, providing lots of payment options, such as ATM, web ATM, credit card, GooglePay, convenience payment code, etc. For developer who wants to enable your app to have payment service, it would be a good choice :)

Documentation

Getting Started

Installation

go get github.com/toastcheng/ecpay-sdk-go/ecpay

Requirements

  • Go 1.12+

Quick Examples

Create a client

for dev (sandbox)

client, err := ecpay.NewClient("<MERCHANT_ID>", "<HASH_KEY>", "<HASH_IV>", ecpay.WithSandbox)

for prod

client, err := ecpay.NewClient("<MERCHANT_ID>", "<HASH_KEY>", "<HASH_IV>")

create an order

the order object is quite complex, make sure you check out the official document.

order := ecpay.Order{
    MerchantTradeNo: fmt.Sprintf("%s%d", uuid.New().String()[:5], time.Now().Unix()%10000),
    StoreID:           "",
    MerchantTradeDate: ecpay.FormatDatetime(time.Now()),
    PaymentType:       ecpay.PaymentTypeAIO,
    TotalAmount:       2000,
    TradeDesc:         "訂單測試",
    ItemName:          ecpay.FormatItemName([]string{"商品1", "商品2"}),
    ReturnURL:         "https://www.ecpay.com.tw/return_url.php",
    ChoosePayment:     ecpay.ChoosePaymentTypeAll,
    ClientBackURL:     "https://www.ecpay.com.tw/client_back_url.php",
    ItemURL:           "https://www.ecpay.com.tw/item_url.php'",
    Remark:            "交易備註",
    OrderResultURL:    "https://www.ecpay.com.tw/order_result_url.php",
    NeedExtraPaidInfo: ecpay.NeedExtraPaidInfoTypeYes,
    InvoiceMark:       ecpay.InvoiceMarkTypeNo,
    ATM: &ecpay.ATMParam{
        ExpireDate:     7,
        PaymentInfoURL: "https://www.ecpay.com.tw/payment_info_url.php",
    },
    CVSBarcode: &ecpay.CVSOrBarcodeParam{
        StoreExpireDate: 15,
        PaymentInfoURL:  "https://www.ecpay.com.tw/payment_info_url.php",
    },
    IgnorePayment: ecpay.FormatIgnorePayment(ecpay.IgnorePaymentOption{
        CVS: true,
    }),
    Credit: &ecpay.CreditParam{
        BindingCard: ecpay.BindingCardTypeNo,
    },
}

Since the ECPay API requires post form and needs to redirect the client to the ECPay host, which works more like PHP style. To make this work in Go, AioCheckOut returns a html text that triggers redirect on client side.

html, err := client.AioCheckOut(order)

You as a server, will need to pass this HTML to your frontend, make sure to add Content-Type: text/html; charset=utf-8. text/html makes the browser executes the html response instead of just displaying it; charset=utf-8 makes the CheckMacValue validation in ECPay service pass.

mux.HandleFunc("/checkout", func(w http.ResponseWriter, r *http.Request) {
    ...
    w.Header().Set("Content-Type", "text/html; charset=utf-8")
    fmt.Fprint(w, html)
}

The response will look something like this:

<form id="5d0eb44f-e36f-45e4-80c6-579c7feba847"
    action="https://payment-stage.ecpay.com.tw/Cashier/AioCheckOut/V5"
    method="post"
>
    <input type="hidden" name="MerchantTradeDate" id="MerchantTradeDate" value="2020/09/26 23:16:49" />
    <input type="hidden" name="StoreExpireDate" id="StoreExpireDate" value="15" />
    <input type="hidden" name="Remark" id="Remark" value="交易備註" />
    <input type="hidden" name="MerchantTradeNo" id="MerchantTradeNo" value="0174e3409" />
    <input type="hidden" name="BindingCard" id="BindingCard" value="0" />
    <input type="hidden" name="OrderResultURL" id="OrderResultURL" value="https://www.ecpay.com.tw/order_result_url.php" />
    <input type="hidden" name="ItemName" id="ItemName" value="商品1*#商品2" />
    <input type="hidden" name="ReturnURL" id="ReturnURL" value="https://www.ecpay.com.tw/return_url.php" />
    <input type="hidden" name="ItemURL" id="ItemURL" value="https://www.ecpay.com.tw/item_url.php" />
    <input type="hidden" name="ChoosePayment" id="ChoosePayment" value="ALL" />
    <input type="hidden" name="NeedExtraPaidInfo" id="NeedExtraPaidInfo" value="Y" />
    <input type="hidden" name="ExpireDate" id="ExpireDate" value="7" />
    <input type="hidden" name="TotalAmount" id="TotalAmount" value="2000" />
    <input type="hidden" name="IgnorePayment" id="IgnorePayment" value="CVS" />
    <input type="hidden" name="PaymentType" id="PaymentType" value="aio" />
    <input type="hidden" name="EncryptType" id="EncryptType" value="1" />
    <input type="hidden" name="MerchantID" id="MerchantID" value="2000132" />
    <input type="hidden" name="CheckMacValue" id="CheckMacValue" value="8D48DA612C0C70B9C453D73D3C7513EC9D3600389D822DF2A00EB0639940DAFD" />
    <input type="hidden" name="PaymentInfoURL" id="PaymentInfoURL" value="https://www.ecpay.com.tw/payment_info_url.php" />
    <input type="hidden" name="TradeDesc" id="TradeDesc" value="訂單測試" />
    <input type="hidden" name="InvoiceMark" id="InvoiceMark" value="N" />
    <input type="hidden" name="ClientBackURL" id="ClientBackURL" value="https://www.ecpay.com.tw/client_back_url.php" />
    <script type="text/javascript">document.getElementById("5d0eb44f-e36f-45e4-80c6-579c7feba847").submit();</script>
</form>

This will redirect the client to ECPay payment page.

There are more examples in ecpay/example_test.go, example/main.go and pkg.go.dev.

Contributing

This project is in development, any contributions, issues and feature requests are welcome! Please check out the issues page.

License

ecpay-sdk-go is available under the MIT license.