### Complete Payment Request Example Source: https://context7.com/morebuff/xunhupay/llms.txt A full implementation example for initiating a payment, including parameter construction, request execution, and JSON response parsing. ```go package main import ( "encoding/json" "fmt" "github.com/morebuff/xunhupay" ) type PaymentResponse struct { ErrCode int `json:"errcode"` ErrMsg string `json:"errmsg"` URL string `json:"url"` URLQR string `json:"url_qrcode"` } func main() { appId := "your_app_id" appSecret := "your_app_secret" paymentURL := "https://api.xunhupay.com/payment/do.html" client := xunhupay.NewHuPi(&appId, &appSecret) // 构建支付参数 // appid、time、nonce_str、hash 由 SDK 自动处理 params := map[string]string{ "version": "1.1", // API版本 "trade_order_id": "ORDER_20231201_001", // 商户订单号 "total_fee": "99.00", // 支付金额(元) "title": "高级会员月卡", // 商品标题 "notify_url": "https://api.your-domain.com/pay/notify", // 异步通知地址 "return_url": "https://www.your-domain.com/pay/success", // 同步跳转地址 "wap_name": "我的在线商城", // WAP站点名称 "callback_url": "https://www.your-domain.com/pay/cancel", // 取消支付跳转 } response, err := client.Execute(paymentURL, params) if err != nil { fmt.Printf("支付请求失败: %v\n", err) return } // 解析响应 var result PaymentResponse if err := json.Unmarshal([]byte(response), &result); err != nil { fmt.Printf("解析响应失败: %v\n", err) return } if result.ErrCode == 0 { fmt.Printf("支付页面URL: %s\n", result.URL) fmt.Printf("二维码URL: %s\n", result.URLQR) } else { fmt.Printf("支付失败: %s\n", result.ErrMsg) } } ``` -------------------------------- ### Installation Source: https://context7.com/morebuff/xunhupay/llms.txt Install the Xunhupay Go SDK using the go get command. ```APIDOC ## Installation ```shell go get github.com/morebuff/xunhupay ``` ``` -------------------------------- ### Complete Payment Request Example Source: https://context7.com/morebuff/xunhupay/llms.txt A comprehensive example demonstrating how to initiate a payment request, including all necessary parameters and error handling. Upon successful payment, a URL to the payment page will be returned. ```APIDOC ## Complete Payment Request Example This is a complete example of initiating a payment request, including all necessary parameters and error handling. After a successful payment, a URL to the payment page will be returned. Users need to be redirected to this URL to complete the payment. ### Method `Execute` (used internally) ### Parameters #### Request Body (for `Execute` method) - **host** (string) - Required - The payment gateway URL (e.g., `https://api.xunhupay.com/payment/do.html`). - **params** (map[string]string) - Required - A map containing payment-specific parameters. The SDK automatically handles `appid`, `time`, `nonce_str`, and `hash`. - **version** (string) - Required - API version, e.g., "1.1". - **trade_order_id** (string) - Required - Merchant's unique order ID. - **total_fee** (string) - Required - Payment amount in Yuan (e.g., "99.00"). - **title** (string) - Required - Product title. - **notify_url** (string) - Required - URL for asynchronous notifications. - **return_url** (string) - Required - URL for synchronous redirection after payment. - **wap_name** (string) - Optional - WAP site name. - **callback_url** (string) - Optional - URL to redirect to if payment is cancelled. ### Request Example ```go package main import ( "encoding/json" "fmt" "github.com/morebuff/xunhupay" ) type PaymentResponse struct { ErrCode int `json:"errcode"` ErrMsg string `json:"errmsg"` URL string `json:"url"` URLQR string `json:"url_qrcode"` } func main() { appId := "your_app_id" appSecret := "your_app_secret" paymentURL := "https://api.xunhupay.com/payment/do.html" client := xunhupay.NewHuPi(&appId, &appSecret) // Construct payment parameters // appid, time, nonce_str, hash are handled automatically by the SDK params := map[string]string{ "version": "1.1", // API version "trade_order_id": "ORDER_20231201_001", // Merchant order ID "total_fee": "99.00", // Payment amount (Yuan) "title": "Premium Monthly Membership", // Product title "notify_url": "https://api.your-domain.com/pay/notify", // Asynchronous notification URL "return_url": "https://www.your-domain.com/pay/success", // Synchronous redirect URL "wap_name": "My Online Store", // WAP site name "callback_url": "https://www.your-domain.com/pay/cancel", // Redirect URL upon cancellation } response, err := client.Execute(paymentURL, params) if err != nil { fmt.Printf("Payment request failed: %v\n", err) return } // Parse response var result PaymentResponse if err := json.Unmarshal([]byte(response), &result); err != nil { fmt.Printf("Failed to parse response: %v\n", err) return } if result.ErrCode == 0 { fmt.Printf("Payment Page URL: %s\n", result.URL) fmt.Printf("QR Code URL: %s\n", result.URLQR) } else { fmt.Printf("Payment failed: %s\n", result.ErrMsg) } } ``` ``` -------------------------------- ### Initialize HuPiPay Client Source: https://context7.com/morebuff/xunhupay/llms.txt Create a new client instance using your AppId and AppSecret obtained from the HuPiPay platform. ```go package main import ( "github.com/morebuff/xunhupay" ) func main() { appId := "your_app_id" appSecret := "your_app_secret" // 初始化客户端 client := xunhupay.NewHuPi(&appId, &appSecret) // 客户端已准备就绪,可用于执行支付或查询操作 } ``` -------------------------------- ### 安装 SDK Source: https://github.com/morebuff/xunhupay/blob/master/README.md 使用 go mod 命令安装 xunhupay 依赖包。 ```shell go get github.com/morebuff/xunhupay ``` -------------------------------- ### Initialize Payment Client Source: https://context7.com/morebuff/xunhupay/llms.txt Initialize a new Xunhupay payment client instance using your AppId and AppSecret obtained from the Xunhupay platform. ```APIDOC ## NewHuPi - Initialize Payment Client Creates a new Xunhupay payment client instance. This function takes `AppId` and `AppSecret` as parameters, which must be obtained from the Xunhupay platform. The returned client instance can be used to perform payment and query operations. ### Method `NewHuPi` ### Parameters #### Path Parameters - **appId** (string) - Required - Your Xunhupay Application ID. - **appSecret** (string) - Required - Your Xunhupay Application Secret. ### Request Example ```go package main import ( "github.com/morebuff/xunhupay" ) func main() { appId := "your_app_id" appSecret := "your_app_secret" // Initialize client client := xunhupay.NewHuPi(&appId, &appSecret) // The client is now ready to perform payment or query operations } ``` ``` -------------------------------- ### Execute API Request Source: https://context7.com/morebuff/xunhupay/llms.txt Send a POST request to the payment gateway. The SDK automatically handles signature generation and required security parameters. ```go package main import ( "fmt" "github.com/morebuff/xunhupay" ) func main() { appId := "your_app_id" appSecret := "your_app_secret" host := "https://api.xunhupay.com/payment/do.html" client := xunhupay.NewHuPi(&appId, &appSecret) params := map[string]string{ "version": "1.1", "trade_order_id": "ORDER_20231201_001", "total_fee": "9.99", "title": "商品名称", "notify_url": "https://your-domain.com/notify", "return_url": "https://your-domain.com/return", "wap_name": "我的店铺", "callback_url": "https://your-domain.com/callback", } // 执行请求,SDK 自动处理签名 response, err := client.Execute(host, params) if err != nil { fmt.Printf("请求失败: %v\n", err) return } fmt.Printf("响应结果: %s\n", response) // 响应示例: {"errcode":0,"errmsg":"success","url":"https://pay.xunhupay.com/..."} } ``` -------------------------------- ### 发起支付请求 Source: https://github.com/morebuff/xunhupay/blob/master/README.md 初始化 HuPi 客户端并传入支付参数以执行支付操作。 ```go appId := "xxxxxxxxxxxxxxxxxxx" //Appid appSecret := "YYYYYYYYYYYYYYYYYYYYYYY" //密钥 var host = "https://api.xunhupay.com/payment/do.html" //跳转支付页接口URL client := xunhupay.NewHuPi(&appId, &appSecret) //初始化调用 //支付参数,appid、time、nonce_str和hash这四个参数不用传,调用的时候执行方法内部已经处理 params := map[string]string{ "version": "1.1", "trade_order_id": "123456789", "total_fee": "0.1", "title": "测试标题", "notify_url": "http://xxxxxxx.com", "return_url": "http://xxxx.com", "wap_name": "XXX店铺", "callback_url": "", } execute, err := client.Execute(host, params) //执行支付操作 if err != nil { panic(err) } fmt.Println(execute) //打印支付结果 ``` -------------------------------- ### 查询订单状态 Source: https://github.com/morebuff/xunhupay/blob/master/README.md 通过订单号查询支付状态,无需手动处理签名参数。 ```go appId := "xxxxxxxxxxxxxxxxxxx" //Appid appSecret := "YYYYYYYYYYYYYYYYYYYYYYY" //密钥 var host = "https://api.xunhupay.com/payment/query.html" //查询接口URL client := xunhupay.NewHuPi(&appId, &appSecret) //初始化调用 //查询参数,appid、time、nonce_str和hash这四个参数不用传,调用的时候执行方法内部已经处理 params := map[string]string{ "out_trade_order": "52c0194467c459082e61e56fccd3ece7", //"open_order_id":"123456789", } execute, err := client.Execute(host, params) //执行查询操作 if err != nil { panic(err) } fmt.Println(execute) //打印查询结果 ``` -------------------------------- ### Query Order Status with Xunhupay SDK Source: https://context7.com/morebuff/xunhupay/llms.txt Use this snippet to query the status of an order using the Xunhupay Go SDK. You can query by either the merchant order number (out_trade_order) or the platform order number (open_order_id). Ensure you have initialized the client with your AppId and AppSecret. ```go package main import ( "encoding/json" "fmt" "github.com/morebuff/xunhupay" ) type QueryResponse struct { ErrCode int `json:"errcode"` ErrMsg string `json:"errmsg"` Status string `json:"status"` // 订单状态: OD(已支付), WP(待支付) TradeOrderID string `json:"trade_order_id"` // 商户订单号 TotalFee string `json:"total_fee"` // 支付金额 PayTime string `json:"pay_time"` // 支付时间 } func main() { appId := "your_app_id" appSecret := "your_app_secret" queryURL := "https://api.xunhupay.com/payment/query.html" client := xunhupay.NewHuPi(&appId, &appSecret) // 方式一:通过商户订单号查询 params := map[string]string{ "out_trade_order": "ORDER_20231201_001", } // 方式二:通过平台订单号查询(二选一) // params := map[string]string{ // "open_order_id": "HP2023120100001", // } response, err := client.Execute(queryURL, params) if err != nil { fmt.Printf("查询请求失败: %v\n", err) return } // 解析响应 var result QueryResponse if err := json.Unmarshal([]byte(response), &result); err != nil { fmt.Printf("解析响应失败: %v\n", err) return } if result.ErrCode == 0 { fmt.Printf("订单号: %s\n", result.TradeOrderID) fmt.Printf("订单状态: %s\n", result.Status) fmt.Printf("支付金额: %s 元\n", result.TotalFee) fmt.Printf("支付时间: %s\n", result.PayTime) } else { fmt.Printf("查询失败: %s\n", result.ErrMsg) } } ``` -------------------------------- ### Generate Request Signature Source: https://context7.com/morebuff/xunhupay/llms.txt Manually generate an MD5 signature for a set of parameters. Note that the Execute method performs this automatically. ```go package main import ( "fmt" "github.com/morebuff/xunhupay" ) func main() { appId := "your_app_id" appSecret := "your_app_secret" client := xunhupay.NewHuPi(&appId, &appSecret) params := map[string]string{ "version": "1.1", "trade_order_id": "123456789", "total_fee": "0.1", "title": "测试商品", "time": "1701388800", "nonce_str": "1701388800", } // 生成签名 signature := client.Sign(params) fmt.Printf("签名结果: %s\n", signature) // 输出: 签名结果: a1b2c3d4e5f6... (32位MD5哈希) } ``` -------------------------------- ### Execute API Request Source: https://context7.com/morebuff/xunhupay/llms.txt Execute core payment or query requests. This method automatically adds necessary security parameters like appid, time, nonce_str, and hash signature before sending a POST request. ```APIDOC ## Execute - Execute API Request This is the core method for executing payment or query requests. It takes the API endpoint URL and a map of parameters. The SDK automatically appends `appid`, `time`, `nonce_str`, and `hash` signature parameters before sending a POST request to the Xunhupay server. It returns the server's JSON response string and any potential errors. ### Method `Execute` ### Parameters #### Path Parameters - **host** (string) - Required - The URL of the API endpoint (e.g., `https://api.xunhupay.com/payment/do.html`). - **params** (map[string]string) - Required - A map containing the request parameters. ### Request Example ```go package main import ( "fmt" "github.com/morebuff/xunhupay" ) func main() { appId := "your_app_id" appSecret := "your_app_secret" host := "https://api.xunhupay.com/payment/do.html" client := xunhupay.NewHuPi(&appId, &appSecret) params := map[string]string{ "version": "1.1", "trade_order_id": "ORDER_20231201_001", "total_fee": "9.99", "title": "商品名称", "notify_url": "https://your-domain.com/notify", "return_url": "https://your-domain.com/return", "wap_name": "我的店铺", "callback_url": "https://your-domain.com/callback", } // Execute request, SDK automatically handles signature response, err := client.Execute(host, params) if err != nil { fmt.Printf("Request failed: %v\n", err) return } fmt.Printf("Response: %s\n", response) // Response example: {"errcode":0,"errmsg":"success","url":"https://pay.xunhupay.com/...?" } ``` ``` -------------------------------- ### Generate Request Signature Source: https://context7.com/morebuff/xunhupay/llms.txt Generate the MD5 signature for API requests. This method sorts parameters by key, concatenates them, and appends the AppSecret for MD5 hashing. Typically, the Execute method handles this automatically. ```APIDOC ## Sign - Generate Request Signature Generates the MD5 signature for an API request. This method sorts all parameters by key, concatenates them, and then appends the `AppSecret` before performing an MD5 hash. You typically do not need to call this method manually, as the `Execute` method will call it automatically. ### Method `Sign` ### Parameters #### Path Parameters - **params** (map[string]string) - Required - A map containing the parameters for which to generate the signature. ### Request Example ```go package main import ( "fmt" "github.com/morebuff/xunhupay" ) func main() { appId := "your_app_id" appSecret := "your_app_secret" client := xunhupay.NewHuPi(&appId, &appSecret) params := map[string]string{ "version": "1.1", "trade_order_id": "123456789", "total_fee": "0.1", "title": "Test Product", "time": "1701388800", "nonce_str": "1701388800", } // Generate signature signature := client.Sign(params) fmt.Printf("Signature: %s\n", signature) // Output: Signature: a1b2c3d4e5f6... (32-bit MD5 hash) } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.