微信相关接口
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
4.2 KiB

  1. package wechat
  2. import (
  3. "context"
  4. "crypto/rsa"
  5. "crypto/x509"
  6. "errors"
  7. "git.tetele.net/tgo/helper"
  8. "github.com/wechatpay-apiv3/wechatpay-go/core"
  9. "github.com/wechatpay-apiv3/wechatpay-go/core/option"
  10. "github.com/wechatpay-apiv3/wechatpay-go/services/refunddomestic"
  11. "github.com/wechatpay-apiv3/wechatpay-go/utils"
  12. "io/ioutil"
  13. "log"
  14. )
  15. /**
  16. * 商户订单号,退款单号,总金额,退款金额,微信商户id,微信商户平台API私钥,平台证书,商户平台证书序列号,商品名,商品id,退款原因,回调地址
  17. */
  18. func CreateRefund(order_sn, out_refund_no, total_amount, refund_amount, wx_mp_mch_id, wx_mch_apiclient_key,
  19. wx_pay_cert, mch_serial_no, title, uuid, reason, notify_url string) (map[string]interface{}, error) {
  20. total := helper.ToInt64(helper.FloatMul(total_amount, 100)) //总金额
  21. refund := helper.ToInt64(helper.FloatMul(refund_amount, 100)) //退款金额
  22. log.Println("refund", order_sn, out_refund_no, refund, total, notify_url)
  23. var (
  24. ctx context.Context = context.Background()
  25. client *core.Client
  26. )
  27. privateKey, err := utils.LoadPrivateKey(wx_mch_apiclient_key)
  28. if err != nil {
  29. log.Println("商户私钥有误:", err)
  30. return map[string]interface{}{}, errors.New("商户私钥有误")
  31. }
  32. wechatPayCertificate, err := utils.LoadCertificate(wx_pay_cert)
  33. if err != nil {
  34. log.Println("平台证书有误:", err)
  35. return map[string]interface{}{}, errors.New("商户平台证书有误")
  36. }
  37. client, err = CreateClient(ctx, wechatPayCertificate, privateKey, wx_mp_mch_id, mch_serial_no)
  38. if err != nil {
  39. return map[string]interface{}{}, errors.New("请检查支付参数配置")
  40. }
  41. // now := time.Now()
  42. svc := refunddomestic.RefundsApiService{Client: client}
  43. _, result, err := svc.Create(ctx,
  44. refunddomestic.CreateRequest{
  45. Amount: &refunddomestic.AmountReq{
  46. Currency: core.String("CNY"),
  47. // From: []refunddomestic.FundsFromItem{refunddomestic.FundsFromItem{
  48. // Account: refunddomestic.ACCOUNT_AVAILABLE.Ptr(),
  49. // Amount: core.Int64(444),
  50. // }},
  51. Refund: core.Int64(refund),
  52. Total: core.Int64(total),
  53. },
  54. // FundsAccount: refunddomestic.REQFUNDSACCOUNT_AVAILABLE.Ptr(),
  55. GoodsDetail: []refunddomestic.GoodsDetail{refunddomestic.GoodsDetail{
  56. GoodsName: core.String(title),
  57. MerchantGoodsId: core.String(uuid),
  58. RefundAmount: core.Int64(refund),
  59. RefundQuantity: core.Int64(1),
  60. UnitPrice: core.Int64(refund),
  61. // WechatpayGoodsId: core.String("1001"),
  62. }},
  63. NotifyUrl: core.String(notify_url),
  64. OutRefundNo: core.String(out_refund_no),
  65. OutTradeNo: core.String(order_sn),
  66. Reason: core.String(reason),
  67. // SubMchid: core.String("1900000109"),
  68. // TransactionId: core.String("1217752501201407033233368018"),
  69. },
  70. )
  71. res := map[string]interface{}{}
  72. log.Println("Response:", result.Response)
  73. log.Println("Response Body:", result.Response.Body)
  74. if err != nil {
  75. response_body := map[string]interface{}{}
  76. body, _ := ioutil.ReadAll(result.Response.Body)
  77. err_body := json.Unmarshal(body, &response_body)
  78. if err_body != nil {
  79. res["code"] = "RESPONSE_BODY_ERROR"
  80. res["message"] = "退款回调参数异常"
  81. return res, err_body
  82. }
  83. log.Println("response_body:", response_body)
  84. res["code"] = response_body["code"]
  85. res["message"] = response_body["message"]
  86. return res, errors.New(helper.ToStr(response_body["message"]))
  87. }
  88. res["code"] = "success"
  89. res["message"] = "退款成功"
  90. return res, nil
  91. }
  92. func CreateClient(ctx context.Context, wechatPayCertificate *x509.Certificate, privateKey *rsa.PrivateKey, mchID, mchSerialNo string) (*core.Client, error) {
  93. var client *core.Client
  94. var err error
  95. if wechatPayCertificate != nil {
  96. client, err = core.NewClient(
  97. ctx, option.WithMerchantCredential(mchID, mchSerialNo, privateKey),
  98. option.WithWechatPayCertificate([]*x509.Certificate{wechatPayCertificate}),
  99. )
  100. if err != nil {
  101. log.Println("创建 Client 失败:", err)
  102. return nil, err
  103. }
  104. } else {
  105. client, err = core.NewClient(
  106. ctx, option.WithMerchantCredential(mchID, mchSerialNo, privateKey), option.WithoutValidator(),
  107. )
  108. if err != nil {
  109. log.Println("创建 Client 失败:", err)
  110. return nil, err
  111. }
  112. }
  113. return client, nil
  114. }