印度包网
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.

166 lines
4.7 KiB

package luckyinpay
import (
"crypto"
"encoding/base64"
"errors"
"fmt"
"net/http"
"server/common"
"server/modules/pay/base"
"server/modules/pay/values"
"server/pb"
"strconv"
"strings"
"time"
"github.com/gogo/protobuf/proto"
"github.com/liangdas/mqant/log"
)
func NewSub(b *base.Base) {
sub := &Sub{
Base: b,
}
b.HttpType = base.HttpTypeJson
b.ShouldSignUpper = true
b.Channel = values.LuckyinPay
if b.Opt == base.OPTPay {
b.Resp = new(PayWithdrawResponse)
b.ReqURL = payApi
} else if b.Opt == base.OPTWithdraw {
b.Resp = new(PayWithdrawResponse)
b.ReqURL = withdrawApi
} else if b.Opt == base.OPTPayCB {
b.SignPassStr = []string{"sign"}
b.CallbackResp.Msg = strings.ToLower(statusSuccess)
b.CallbackReq = new(CallbackReq)
} else if b.Opt == base.OPTWithdrawCB {
b.SignPassStr = []string{"sign", "msg"}
b.CallbackResp.Msg = strings.ToLower(statusSuccess)
b.CallbackReq = new(CallbackReq)
}
b.Sub = sub
}
type Sub struct {
Base *base.Base
}
func (s *Sub) PackHeader(_ http.Header) {
// header.Set("Merchant-Id", config.GetConfig().Pay.IGeek.MID)
}
func (s *Sub) PackReq() interface{} {
if s.Base.Opt == base.OPTPay {
return s.PackPayReq()
}
return s.PackWithdrawReq()
}
func (s *Sub) GetResp() (proto.Message, error) {
if s.Base.Opt == base.OPTPay {
resp := s.Base.Resp.(*PayWithdrawResponse)
if resp.Message != statusSuccess || resp.Data.PayData == "" {
return nil, errors.New("pay fail")
}
return &pb.InnerRechargeResp{APIOrderID: fmt.Sprintf("%v", resp.Data.PayOrderNo), URL: resp.Data.PayData, Channel: uint32(values.LuckyinPay)}, nil
}
resp := s.Base.Resp.(*PayWithdrawResponse)
if s.Base.Status == 0 && resp.Message != statusSuccess {
return nil, errors.New("withdraw fail")
}
return &pb.InnerWithdrawResp{APIOrderID: fmt.Sprintf("%v", resp.Data.PayOrderNo), Channel: uint32(values.LuckyinPay)}, nil
}
func (s *Sub) PackPayReq() interface{} {
r := s.Base.PayReq
send := &PayReq{
MchNo: mchId,
MchOrderNo: r.OrderID,
Currency: "INR",
PayAmount: fmt.Sprintf("%."+strconv.Itoa(2)+"f", float64(r.Amount)/common.DecimalDigits),
AccountName: r.Name,
AccountEmail: r.Email,
AccountPhone: r.Phone,
CustomerIp: r.IP,
NotifyUrl: values.GetPayCallback(values.LuckyinPay),
SuccessPageUrl: "https://www.tp7k.com",
Summary: "",
ReqTime: fmt.Sprintf("%d", time.Now().UnixMilli()),
}
signStr := base.GetSignStr(send, s.Base.SignPassStr...)
sign, err := base.SignRSA(privateKey, crypto.SHA256, []byte(signStr))
if err != nil {
log.Error("err:%v", err)
}
send.Sign = sign
return send
}
func (s *Sub) PackWithdrawReq() interface{} {
r := s.Base.WithdrawReq
if r.PayType != int64(common.PayTypeBank) {
return nil
}
accountType := ""
if common.PayType(r.PayType) == common.PayTypeBank {
accountType = "IFSC"
} else if common.PayType(r.PayType) == common.PayTypeUPI {
accountType = "UPI"
}
send := &WithdrawReq{
MchNo: mchId,
MchOrderNo: r.OrderID,
Currency: "INR",
PayAmount: fmt.Sprintf("%."+strconv.Itoa(2)+"f", float64(r.Amount)/common.DecimalDigits),
AccountType: accountType,
AccountCode: r.PayCode,
AccountNo: r.CardNo,
AccountName: r.Name,
AccountEmail: r.Email,
AccountPhone: r.Phone,
CustomerIp: r.IP,
NotifyUrl: values.GetWithdrawCallback(values.LuckyinPay),
ReqTime: fmt.Sprintf("%d", time.Now().UnixMilli()),
}
signStr := base.GetSignStr(send, s.Base.SignPassStr...)
sign, err := base.SignRSA(privateKey, crypto.SHA256, []byte(signStr))
if err != nil {
log.Error("err:%v", err)
}
send.Sign = sign
return send
}
func (s *Sub) CheckSign(_ string) bool {
if s.Base.Opt == base.OPTPayCB {
req := s.Base.CallbackReq.(*CallbackReq)
log.Debug("checkSign pay:%+v", *req)
s.Base.CallbackResp.OrderID = req.MchOrderNo
s.Base.CallbackResp.Success = req.PayState == 2
signStr := base.GetSignStr(req, s.Base.SignPassStr...)
sign, _ := base64.StdEncoding.DecodeString(req.Sign)
err := base.RsaVerify(publicKey, crypto.SHA256, []byte(signStr), sign)
if err != nil {
log.Error("err:%v", err)
}
return err == nil
} else if s.Base.Opt == base.OPTWithdrawCB {
req := s.Base.CallbackReq.(*CallbackReq)
log.Debug("checkSign withdraw:%+v", *req)
s.Base.CallbackResp.OrderID = req.MchOrderNo
s.Base.CallbackResp.Success = req.PayState == 2
s.Base.CallbackResp.APIOrderID = req.PayOrderNo
s.Base.CallbackResp.FailMessage = req.ErrMsg
signStr := base.GetSignStr(req, s.Base.SignPassStr...)
sign, _ := base64.StdEncoding.DecodeString(req.Sign)
err := base.RsaVerify(publicKey, crypto.SHA256, []byte(signStr), sign)
if err != nil {
log.Error("err:%v", err)
}
return err == nil
}
return false
}