golang实现Fasapay支付网关XML API对接插件库fasapay-sdk-go的使用
Golang实现Fasapay支付网关XML API对接插件库fasapay-sdk-go的使用
Fasapay XML API SDK GO (非官方)
非官方的Fasapay支付网关XML API Golang客户端库
安装
go get -u github.com/kachit/fasapay-sdk-go
使用示例
初始化客户端
package main
import (
"fmt"
"context"
fasapay "github.com/kachit/fasapay-sdk-go"
)
func main(){
// 创建客户端实例
cfg := fasapay.NewConfig("Your API key", "Your API secret word")
client, err := fasapay.NewClientFromConfig(cfg, nil)
if err != nil {
fmt.Printf("config parameter error " + err.Error())
panic(err)
}
}
获取余额列表
ctx := context.Background()
currencies := []fasapay.CurrencyCode{fasapay.CurrencyCodeIDR, fasapay.CurrencyCodeUSD}
result, resp, err := client.Accounts().GetBalances(currencies, ctx, nil)
if err != nil {
fmt.Printf("Wrong API request " + err.Error())
panic(err)
}
// 输出原始响应
fmt.Println(response)
// 输出结果
fmt.Println(result.Balances.IDR)
fmt.Println(result.Balances.USD)
获取账户列表
ctx := context.Background()
accounts := []string{"FP0000001", "FP0000002"}
result, resp, err := client.Accounts().GetAccounts(accounts, ctx, nil)
if err != nil {
fmt.Printf("Wrong API request " + err.Error())
panic(err)
}
// 输出原始响应
fmt.Println(response)
// 输出结果
fmt.Println(result.Accounts[0].FullName)
fmt.Println(result.Accounts[0].Account)
fmt.Println(result.Accounts[0].Status)
创建转账
ctx := context.Background()
transfer1 := &CreateTransferRequestParams{
Id: "123",
To: "FP89680",
Amount: 1000.0,
Currency: CurrencyCodeIDR,
Note: "standart operation",
}
transfer2 := &CreateTransferRequestParams{
Id: "1234",
To: "FP89681",
Amount: 1001.0,
Currency: CurrencyCodeIDR,
Note: "standart operation",
}
result, resp, err := client.Transfers().CreateTransfer([]*CreateTransferRequestParams{transfer, transfer2}, ctx, nil)
if err != nil {
fmt.Printf("Wrong API request " + err.Error())
panic(err)
}
// 输出原始响应
fmt.Println(response)
// 输出结果
fmt.Println(result.Transfers[0].BatchNumber)
fmt.Println(result.Transfers[0].Datetime)
fmt.Println(result.Transfers[0].From)
fmt.Println(result.Transfers[0].To)
fmt.Println(result.Transfers[0].Amount)
fmt.Println(result.Transfers[0].Note)
fmt.Println(result.Transfers[1].BatchNumber)
fmt.Println(result.Transfers[1].Datetime)
fmt.Println(result.Transfers[1].From)
fmt.Println(result.Transfers[1].To)
fmt.Println(result.Transfers[1].Amount)
fmt.Println(result.Transfers[1].Note)
获取转账历史
ctx := context.Background()
history := &fasapay.GetHistoryRequestParams{StartDate: "2022-03-01", EndDate: "2022-03-28"}
result, resp, err := client.Transfers().GetHistory(history, ctx, nil)
if err != nil {
fmt.Printf("Wrong API request " + err.Error())
panic(err)
}
// 输出原始响应
fmt.Println(response)
// 输出结果
fmt.Println(result.History.Page.TotalItem)
fmt.Println(result.History.Page.PageCount)
fmt.Println(result.History.Page.CurrentPage)
fmt.Println(result.History.Details[0].BatchNumber)
fmt.Println(result.History.Details[0].Datetime)
fmt.Println(result.History.Details[0].From)
fmt.Println(result.History.Details[0].To)
fmt.Println(result.History.Details[0].Amount)
fmt.Println(result.History.Details[0].Note)
获取转账详情
ctx := context.Background()
var detail1 fasapay.GetDetailsRequestDetailParamsString = "TR0000000001"
var detail2 fasapay.GetDetailsRequestDetailParamsString = "TR0000000002"
details := []fasapay.GetDetailsDetailParamsInterface{&detail1, &detail2}
result, resp, err := client.Transfers().GetDetails(details, ctx, nil)
if err != nil {
fmt.Printf("Wrong API request " + err.Error())
panic(err)
}
// 输出原始响应
fmt.Println(response)
// 输出结果
fmt.Println(result.Details[0].BatchNumber)
fmt.Println(result.Details[0].Datetime)
fmt.Println(result.Details[0].From)
fmt.Println(result.Details[0].To)
fmt.Println(result.Details[0].Amount)
fmt.Println(result.Details[0].Note)
fmt.Println(result.Details[1].BatchNumber)
fmt.Println(result.Details[1].Datetime)
fmt.Println(result.Details[1].From)
fmt.Println(result.Details[1].To)
fmt.Println(result.Details[1].Amount)
fmt.Println(result.Details[1].Note)
以上示例展示了如何使用fasapay-sdk-go库与Fasapay支付网关API进行交互,包括获取余额、账户信息、创建转账以及查询转账历史和详情等功能。
更多关于golang实现Fasapay支付网关XML API对接插件库fasapay-sdk-go的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
1 回复
更多关于golang实现Fasapay支付网关XML API对接插件库fasapay-sdk-go的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
Fasapay支付网关XML API对接插件库fasapay-sdk-go使用指南
Fasapay是一种国际支付网关,支持多种货币交易。下面我将介绍如何使用Go语言实现Fasapay支付网关的XML API对接,并提供一个完整的示例。
安装fasapay-sdk-go
首先需要安装fasapay-sdk-go库:
go get github.com/fasapay/fasapay-sdk-go
基本配置
package main
import (
"fmt"
"github.com/fasapay/fasapay-sdk-go"
)
func main() {
// 初始化Fasapay客户端
client := fasapay.NewClient(
"your_store_name", // 商户名称
"your_security_word", // 安全词
"your_api_key", // API密钥
fasapay.Sandbox, // 使用沙盒环境
)
// 或者使用生产环境
// client := fasapay.NewClient("your_store_name", "your_security_word", "your_api_key", fasapay.Production)
}
创建支付请求
func createPayment(client *fasapay.Client) {
payment := &fasapay.PaymentRequest{
OrderID: "ORDER123456", // 你的订单ID
Amount: 100.00, // 金额
Currency: "USD", // 货币类型
Description: "Test Payment", // 描述
ReturnURL: "https://your-site.com/success", // 支付成功返回URL
CancelURL: "https://your-site.com/cancel", // 支付取消返回URL
NotifyURL: "https://your-site.com/notify", // 支付通知URL
BuyerEmail: "buyer@example.com", // 买家邮箱
}
response, err := client.CreatePayment(payment)
if err != nil {
fmt.Printf("创建支付请求失败: %v\n", err)
return
}
fmt.Printf("支付请求创建成功,跳转URL: %s\n", response.RedirectURL)
// 这里应该将用户重定向到response.RedirectURL
}
处理支付通知
Fasapay会发送支付通知到你的NotifyURL,你需要处理这些通知:
func handleNotification(client *fasapay.Client, requestBody []byte) {
notification, err := client.ParseNotification(requestBody)
if err != nil {
fmt.Printf("解析通知失败: %v\n", err)
return
}
// 验证通知签名
valid, err := client.VerifyNotification(notification)
if err != nil || !valid {
fmt.Printf("通知验证失败: %v\n", err)
return
}
// 处理不同类型的通知
switch notification.NotificationType {
case fasapay.NotificationTypePayment:
fmt.Printf("收到支付通知: 订单ID %s, 状态 %s, 金额 %f %s\n",
notification.OrderID, notification.Status, notification.Amount, notification.Currency)
// 更新订单状态为已支付
case fasapay.NotificationTypeRefund:
fmt.Printf("收到退款通知: 订单ID %s, 状态 %s\n", notification.OrderID, notification.Status)
// 处理退款逻辑
default:
fmt.Printf("收到未知类型的通知: %s\n", notification.NotificationType)
}
// 必须返回成功响应
fmt.Println("通知处理成功")
}
查询交易状态
func checkPaymentStatus(client *fasapay.Client, orderID string) {
status, err := client.GetPaymentStatus(orderID)
if err != nil {
fmt.Printf("查询支付状态失败: %v\n", err)
return
}
fmt.Printf("订单 %s 的支付状态: %s\n", orderID, status)
}
完整示例
package main
import (
"fmt"
"log"
"net/http"
"github.com/fasapay/fasapay-sdk-go"
)
func main() {
client := setupFasapayClient()
// 模拟创建支付
createPayment(client)
// 设置HTTP处理程序
http.HandleFunc("/notify", func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
handleNotification(client, body)
w.WriteHeader(http.StatusOK)
})
log.Println("服务器启动,监听8080端口...")
log.Fatal(http.ListenAndServe(":8080", nil))
}
func setupFasapayClient() *fasapay.Client {
return fasapay.NewClient(
"your_store_name",
"your_security_word",
"your_api_key",
fasapay.Sandbox,
)
}
// 其他函数同上...
注意事项
- 安全验证:确保正确处理和验证所有通知,防止伪造请求
- 错误处理:妥善处理API调用可能返回的各种错误
- 日志记录:记录所有交易和通知,便于对账和调试
- 测试:先在沙盒环境充分测试,再切换到生产环境
- 并发处理:如果交易量大,考虑使用goroutine处理通知
高级功能
如果需要更高级的功能,如批量查询、退款处理等,可以参考fasapay-sdk-go的完整文档:
// 批量查询交易状态
func batchCheckStatus(client *fasapay.Client, orderIDs []string) {
statuses, err := client.BatchGetPaymentStatus(orderIDs)
if err != nil {
fmt.Printf("批量查询失败: %v\n", err)
return
}
for orderID, status := range statuses {
fmt.Printf("订单 %s 状态: %s\n", orderID, status)
}
}
// 发起退款
func createRefund(client *fasapay.Client, orderID string, amount float64) {
refundID, err := client.CreateRefund(orderID, amount, "Refund reason")
if err != nil {
fmt.Printf("创建退款失败: %v\n", err)
return
}
fmt.Printf("退款创建成功,退款ID: %s\n", refundID)
}
通过以上代码示例,你可以快速实现Fasapay支付网关的对接。记得根据你的实际业务需求调整代码,并确保在生产环境中使用正确的API密钥和环境配置。