131 lines
4.3 KiB
Go
131 lines
4.3 KiB
Go
package paymentService
|
||
|
||
import (
|
||
"PaymentCenter/app/third/paymentService/payCommon"
|
||
"context"
|
||
"strconv"
|
||
)
|
||
|
||
type PayOrderRequest struct {
|
||
OrderId int64 `json:"order_id"` // 平台订单号
|
||
ChannelType int `json:"channel_type"` // 支付方式
|
||
Description string `json:"description"` // 商品描述
|
||
Amount int `json:"amount"` // 金额单位为分
|
||
PayerClientIp string `json:"payer_client_ip"` // 终端IP
|
||
Wx WxPay `json:"wx"`
|
||
Ali AliPay `json:"ali"`
|
||
}
|
||
|
||
type WxPay struct {
|
||
AppId string `json:"app_id"` // 应用ID
|
||
MchId string `json:"mch_id"` // 商户ID 或者服务商模式的 sp_mchid
|
||
SerialNo string `json:"serial_no"` // 商户证书的证书序列号
|
||
ApiV3Key string `json:"api_v_3_key"` // apiV3Key,商户平台获取
|
||
PrivateKey string `json:"private_key"` // 私钥 apiclient_key.pem 读取后的内容
|
||
}
|
||
|
||
type AliPay struct {
|
||
AppId string `json:"app_id"` // 应用ID
|
||
PrivateKey string `json:"private_key"` // 应用私钥
|
||
AppPublicCert []byte `json:"app_public_cert"` // 应用公钥
|
||
AlipayRootCert []byte `json:"alipay_root_cert"` // 支付宝根证书
|
||
AlipayPublicCert []byte `json:"alipay_public_cert"` // 支付宝公钥
|
||
}
|
||
|
||
type PayOrderResponse struct {
|
||
Code int `json:"code"`
|
||
ErrorMsg string `json:"error_msg"`
|
||
Result string `json:"result"`
|
||
}
|
||
|
||
// PaymentService 统一发起支付
|
||
func PaymentService(c context.Context, payOrderRequest PayOrderRequest) (payOrderResponse PayOrderResponse) {
|
||
switch payOrderRequest.ChannelType {
|
||
case payCommon.PAY_CHANNEL_WECHAT_H5:
|
||
// 微信H5支付
|
||
info, err := WxH5PayInfo(c, payOrderRequest)
|
||
if err != nil {
|
||
return PayOrderResponse{
|
||
Code: payCommon.PAY_ERROR_CODE,
|
||
ErrorMsg: err.Error(),
|
||
}
|
||
}
|
||
return PayOrderResponse{
|
||
Code: payCommon.PAY_SUCCESS_CODE,
|
||
ErrorMsg: "",
|
||
Result: info,
|
||
}
|
||
case payCommon.PAY_CHANNEL_ALIPAY_WEB:
|
||
// 支付宝H5支付
|
||
info, err := ALiH5PayInfo(c, payOrderRequest)
|
||
if err != nil {
|
||
return PayOrderResponse{
|
||
Code: payCommon.PAY_ERROR_CODE,
|
||
ErrorMsg: err.Error(),
|
||
}
|
||
}
|
||
return PayOrderResponse{
|
||
Code: payCommon.PAY_SUCCESS_CODE,
|
||
ErrorMsg: "",
|
||
Result: info,
|
||
}
|
||
default:
|
||
return PayOrderResponse{
|
||
Code: payCommon.PAY_NOT_FOUND_CODE,
|
||
ErrorMsg: "暂不支持该支付渠道,请后续再使用",
|
||
}
|
||
}
|
||
}
|
||
|
||
type PayOrderQueryRequest struct {
|
||
OrderId int64 `json:"order_id"` // 商户订单号
|
||
PayChannel int `json:"pay_channel"` // 支付渠道类型 1:wx,2:zfb
|
||
Wx WxPay `json:"wx"`
|
||
Ali AliPay `json:"ali"`
|
||
}
|
||
|
||
type PayOrderQueryResponse struct {
|
||
Code int `json:"code"`
|
||
ErrorMsg string `json:"error_msg"`
|
||
Result PayOrderQueryInfo `json:"result"`
|
||
}
|
||
|
||
type PayOrderQueryInfo struct {
|
||
AppId string `json:"app_id"` // 应用ID
|
||
OutTradeNo string `json:"out_trade_no"` // 商家订单号
|
||
TransactionId string `json:"transaction_id"` // 第三方订单号
|
||
TradeState string `json:"trade_state"` // 交易状态 SUCCESS:成功、REFUND:转入退款、NOTPAY:未支付、CLOSED:已关闭
|
||
TradeStateDesc string `json:"trade_state_desc"` // 交易描述
|
||
SuccessTime string `json:"success_time"` // 交易完成时间
|
||
AmountTotal int64 `json:"amount_total"` // 订单总金额,单位:分
|
||
PayerTotal int64 `json:"payer_total"` // 支付总金额,单位:分
|
||
}
|
||
|
||
// PayOrderQuery 查询订单状态
|
||
func PayOrderQuery(c context.Context, payOrderQueryRequest PayOrderQueryRequest) PayOrderQueryResponse {
|
||
var err error
|
||
var info PayOrderQueryInfo
|
||
switch payOrderQueryRequest.PayChannel {
|
||
case payCommon.PAY_CHANNLE_TYPE_WECHAT:
|
||
// 微信H5支付
|
||
info, err = WxOrderQuery(c, payOrderQueryRequest.Wx, strconv.FormatInt(payOrderQueryRequest.OrderId, 10))
|
||
case payCommon.PAY_CHANNLE_TYPE_ZFB:
|
||
info, err = ALiOrderQuery(c, payOrderQueryRequest.Ali, strconv.FormatInt(payOrderQueryRequest.OrderId, 10))
|
||
default:
|
||
return PayOrderQueryResponse{
|
||
Code: payCommon.PAY_ERROR_CODE,
|
||
ErrorMsg: "暂不支持该支付渠道,请后续再使用",
|
||
}
|
||
}
|
||
if err != nil {
|
||
return PayOrderQueryResponse{
|
||
Code: payCommon.PAY_ERROR_CODE,
|
||
ErrorMsg: err.Error(),
|
||
}
|
||
}
|
||
return PayOrderQueryResponse{
|
||
Code: payCommon.PAY_SUCCESS_CODE,
|
||
Result: info,
|
||
}
|
||
}
|