|
|
package alipay
|
|
|
|
|
|
import (
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
alipay2 "git.oa00.com/go/alipay"
|
|
|
"github.com/smartwalle/alipay/v3"
|
|
|
"io/ioutil"
|
|
|
"log"
|
|
|
"net/http"
|
|
|
"net/url"
|
|
|
"recook/internal/v2/logic/pay"
|
|
|
"recook/internal/v2/model/recook/after"
|
|
|
"strings"
|
|
|
"time"
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
"github.com/shopspring/decimal"
|
|
|
)
|
|
|
|
|
|
const (
|
|
|
AppID = "2021003107650938"
|
|
|
RefundURL = "https://openapi.alipay.com/gateway.do" // 退款
|
|
|
)
|
|
|
|
|
|
type RefundPublicParam struct {
|
|
|
AppID string `json:"app_id"` // 支付宝分配给开发者的应用ID
|
|
|
Method string `json:"method"` // 接口名称
|
|
|
Charset string `json:"charset"` // 请求使用的编码格式,如utf-8
|
|
|
SignType string `json:"sign_type"` // 商户生成签名字符串所使用的签名算法类型,目前支持RSA2和RSA,推荐使用RSA2
|
|
|
Sign string `json:"sign"` // 商户请求参数的签名串
|
|
|
Timestamp string `json:"timestamp"` // 发送请求的时间,格式"yyyy-MM-dd HH:mm:ss"
|
|
|
Version string `json:"version"` // 调用的接口版本,固定为:1.0
|
|
|
BizContent string `json:"biz_content"`
|
|
|
}
|
|
|
|
|
|
func newPublicParam(d *RefundDetailParam) RefundPublicParam {
|
|
|
ts := time.Now().Format("2006-01-02 15:04:05")
|
|
|
biz, _ := json.Marshal(d)
|
|
|
|
|
|
return RefundPublicParam{
|
|
|
AppID: AppID,
|
|
|
Method: "alipay.trade.refund",
|
|
|
Charset: "utf-8",
|
|
|
SignType: "RSA2",
|
|
|
Version: "1.0",
|
|
|
Timestamp: ts,
|
|
|
BizContent: string(biz),
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func (r *RefundPublicParam) generateRequestParam() string {
|
|
|
r.Sign = generateSign(r)
|
|
|
|
|
|
m := struct2MapWhenAlipay(r)
|
|
|
p := url.Values{}
|
|
|
|
|
|
for k, v := range m {
|
|
|
if len(v) > 0 {
|
|
|
p.Add(k, v)
|
|
|
}
|
|
|
}
|
|
|
return p.Encode()
|
|
|
}
|
|
|
|
|
|
type RefundDetailParam struct {
|
|
|
OutTradeNo string `json:"out_trade_no"`
|
|
|
RefundAmount string `json:"refund_amount"`
|
|
|
OutRequestNo string `json:"out_request_no"`
|
|
|
}
|
|
|
|
|
|
type RefundResult struct {
|
|
|
Response struct {
|
|
|
Code string `json:"code"`
|
|
|
} `json:"alipay_trade_refund_response"`
|
|
|
Sign string `json:"sign"`
|
|
|
}
|
|
|
|
|
|
func Refund(tx *gorm.DB, asGoods *after.RecookAfterSalesGoodsModel) error {
|
|
|
//这边判断是否有金额
|
|
|
fmt.Println("退款金额:", asGoods.RefundAmount.Truncate(2).String(), "========")
|
|
|
if asGoods.RefundAmount.GreaterThan(decimal.NewFromFloat(0.0)) {
|
|
|
param := alipay.TradeRefund{
|
|
|
OutTradeNo: asGoods.TradeNo,
|
|
|
RefundAmount: asGoods.RefundAmount.Truncate(2).String(),
|
|
|
OutRequestNo: asGoods.RefundNo,
|
|
|
}
|
|
|
refund, _ := alipay2.Alipay.TradeRefund(param)
|
|
|
if !refund.Content.Code.IsSuccess() {
|
|
|
return errors.New("支付宝退款结果不对")
|
|
|
}
|
|
|
//detailParam := RefundDetailParam{
|
|
|
// OutTradeNo: asGoods.TradeNo,
|
|
|
// RefundAmount: asGoods.RefundAmount.Truncate(2).String(),
|
|
|
// OutRequestNo: asGoods.RefundNo,
|
|
|
//}
|
|
|
//
|
|
|
//pub := newPublicParam(&detailParam)
|
|
|
//if err := RequestOrderRefund(&pub); err != nil {
|
|
|
// return err
|
|
|
//}
|
|
|
}
|
|
|
|
|
|
return pay.SyncRefundSuccessCallback(tx, asGoods)
|
|
|
}
|
|
|
|
|
|
func RequestOrderRefund(param *RefundPublicParam) error {
|
|
|
client := &http.Client{}
|
|
|
request, err := http.NewRequest("POST", RefundURL, strings.NewReader(param.generateRequestParam()))
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
request.Header.Set("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
|
|
|
response, err := client.Do(request)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
defer func() { _ = response.Body.Close() }()
|
|
|
result, err := ioutil.ReadAll(response.Body)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
fmt.Println("===============================")
|
|
|
fmt.Println(string(result))
|
|
|
|
|
|
var r RefundResult
|
|
|
err = json.Unmarshal(result, &r)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
if r.Response.Code != "10000" {
|
|
|
log.Println(r)
|
|
|
return errors.New("支付宝退款请求结果不对")
|
|
|
}
|
|
|
return nil
|
|
|
}
|