go接入支付宝异步回调通知
1.接入三方支付包
"github.com/go-pay/gopay"
2.demo
//支付宝异步通知
func (s basicService) AliPayNotify(ctx context.Context, req api.PayNotifyRequest) string {
notifyReq, err := alipay.ParseNotifyToBodyMap(req.Request)
if err != nil {
return FAIL
}
// 支付宝异步通知验签(公钥模式)
ok, err := alipay.VerifySign(s.conf.AliPay.PublicKey, notifyReq)
if !ok {
mylog.WithContext(ctx).Error(err.Error())
return FAIL
}
mylog.Sugar().Debug(notifyReq)
errCode := ""
switch notifyReq.GetString("notify_type") {
case "trade_status_sync": //扣款通知(交易状态变更通知)
if notifyReq.GetString("refund_fee") != "" { //退款金额不为空,退款
errCode = s.aliPayNotifyRefundSuccess(ctx, notifyReq)
} else { //交易成功
errCode = s.aliPayTradeNotify(ctx, notifyReq)
}
case "dut_user_sign": //签约通知
errCode = s.aliPaySignNotify(ctx, notifyReq)
case "dut_user_unsign": //解约通知
errCode = s.aliPayNotifyCancelSubscription(ctx, notifyReq)
case "refund_failed": //拒绝退款
errCode = s.aliPayNotifyRefundFailed(ctx, notifyReq)
default:
errCode = FAIL
}
return errCode
}