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.

269 lines
7.3 KiB

package biz
import (
"bkb-payment/third_party/alipay"
"bkb-payment/third_party/paypal"
"context"
"github.com/go-kratos/kratos/v2/errors"
)
type PaypalBot struct {
Token string
}
func newPaypalBot(token string) *PaypalBot {
return &PaypalBot{Token: token}
}
type AlipayGBot struct {
}
func newAlipayGBot() *AlipayGBot {
return &AlipayGBot{}
}
func (b *PaypalBot) Consult(ctx context.Context, info *ConsultPay) ([]PayMethod, error) {
return nil, nil
}
func (b *AlipayGBot) Consult(ctx context.Context, info *ConsultPay) ([]PayMethod, error) {
var (
err error
params alipay.ConsultPay
result = make([]PayMethod, 0)
)
params.ProductCode = info.ProductCode
params.PaymentAmount.Currency = info.Currency
params.PaymentAmount.Value = info.Amount
if info.UserRegion != "" {
params.AllowedPaymentMethodRegions = []string{info.UserRegion}
}
params.Env.TerminalType = info.TerminalType
params.Env.OsType = info.OsType
params.PaymentFactor.PresentmentMode = "TILE"
err, detail := alipay.Consult(&params)
if err != nil {
return result, err
}
for _, option := range detail.PaymentOptions {
result = append(result, PayMethod{LogoName: option.Logo.LogoName, LogoUrl: option.Logo.LogoUrl, Type: option.PaymentMethodType, Category: option.PaymentMethodCategory, Enabled: option.Enabled})
}
return result, err
}
func (b *PaypalBot) CreateOrder(ctx context.Context, info *CreateOrder) (PayResp, error) {
var (
params paypal.CreateOrderRequest
result PayResp
)
params.PurchaseUnits = []paypal.PurchaseUnit{{
Amount: paypal.PurchaseAmount{
Amount: paypal.Amount{
CurrencyCode: info.Currency,
Value: info.Amount,
}}}}
// 每个appid一套
params.ApplicationContext.ReturnURL = info.ReturnUrl
params.ApplicationContext.CancelURL = info.CancelUrl
err, detail := paypal.CreateOrder(b.Token, &params)
if err != nil {
return result, err
}
result.ID = detail.ID
result.Status = detail.Status
for _, link := range detail.Links {
if link.Rel == "approve" {
result.Link = link.Href
result.Method = link.Method
}
}
return result, err
}
func (b *AlipayGBot) CreateOrder(ctx context.Context, info *CreateOrder) (PayResp, error) {
var (
err error
params alipay.CreatePay
result PayResp
)
params.ProductCode = "CASHIER_PAYMENT"
params.PaymentRequestID = info.TransactionId //
params.Order.OrderAmount.Currency = info.Currency
params.Order.OrderAmount.Value = info.Amount
params.Order.ReferenceOrderID = info.TransactionId
params.Order.OrderDescription = info.Description
params.PaymentAmount.Currency = info.Currency
params.PaymentAmount.Value = info.Amount
params.PaymentMethod.PaymentMethodType = info.PaymentMethodType
params.PaymentRedirectURL = info.ReturnUrl
params.PaymentNotifyURL = info.NotifyUrl
params.Env.TerminalType = info.TerminalType
params.Env.OsType = info.OsType
//params.SettlementStrategy.SettlementCurrency = "USD"
err, detail := alipay.CreateOrder(&params)
if err != nil {
return result, err
}
result.ID = detail.PaymentID
//if info.TerminalType == "WEB" {
//}
result.Method = detail.RedirectActionForm.Method
result.Link = detail.RedirectActionForm.RedirectUrl
switch detail.Result.ResultStatus {
case "F":
result.Status = "FAIL"
default:
result.Status = "SUCCESS"
}
return result, err
}
func (b *PaypalBot) CaptureOrder(ctx context.Context, order *Order) (bool, string, string) {
var (
err error
params paypal.PaymentSourceRequest
detail paypal.OrderDetail
captureId string
)
err, detail = paypal.CapturePaymentOrder(b.Token, order.OrderId, &params)
if err != nil {
return false, "", err.Error()
}
if detail.Status != "COMPLETED" { // 支付未完成
return false, "", "payment not completed"
}
if len(detail.PurchaseUnits) != 0 && len(detail.PurchaseUnits[0].Payments.Captures) != 0 {
captureId = detail.PurchaseUnits[0].Payments.Captures[0].ID
}
return true, captureId, "ok"
}
func (b *AlipayGBot) CaptureOrder(ctx context.Context, order *Order) (bool, string, string) {
var (
err error
params alipay.CapturePay
captureId string
detail alipay.CaptureDetail
)
params.CaptureRequestID = order.OrderId
params.PaymentID = order.PayId
params.CaptureAmount.Currency = order.Currency
params.CaptureAmount.Value = order.Value
err, detail = alipay.CaptureOrder(&params)
if err != nil {
return false, "", err.Error()
}
if detail.Result.ResultStatus != "S" {
return false, "", "payment not completed"
}
captureId = detail.CaptureID
return true, captureId, "ok"
}
func (b *PaypalBot) Payouts(ctx context.Context, info *CreatePayout) (PayResp, error) {
var (
err error
params paypal.PayoutRequest
result PayResp
)
params.Items = []paypal.PayoutItem{{
RecipientType: "EMAIL",
Note: "打款",
Receiver: info.PayerName,
Amount: paypal.PayoutAmount{
Currency: info.Currency,
Value: info.Amount,
},
}}
params.SenderBatchHeader.SenderBatchId = info.SenderBatchId
err, detail := paypal.CreateBatchPayouts(b.Token, &params)
if err != nil {
return result, err
}
result.ID = detail.BatchHeader.PayoutBatchId
result.Status = detail.BatchHeader.BatchStatus
//for _, link := range detail.Links {
// if link.Rel == "self" {
// result.Link = link.Href
// result.Method = link.Method
// }
//}
if len(detail.Links) != 0 {
result.Link = detail.Links[0].Href
result.Method = detail.Links[0].Method
}
return result, nil
}
func (b *AlipayGBot) Payouts(ctx context.Context, info *CreatePayout) (PayResp, error) {
var (
err error
result PayResp
)
return result, err
}
func (b *PaypalBot) ShowPayoutsDetail(ctx context.Context, info *Payouts) (bool, string) {
var (
err error
detail paypal.PayoutResponse
)
err, detail = paypal.ShowPayoutsDetail(b.Token, info.PayoutBatchId)
if err != nil {
return false, err.Error()
}
if detail.BatchHeader.BatchStatus != "SUCCESS" {
return false, "payout not completed"
}
return true, "ok"
}
func (b *AlipayGBot) ShowPayoutsDetail(ctx context.Context, info *Payouts) (bool, string) {
var (
//err error
)
return true, "ok"
}
func (b *PaypalBot) CancelOrder(ctx context.Context, payId string) (bool, error) {
return true, nil
}
func (b *AlipayGBot) CancelOrder(ctx context.Context, payId string) (bool, error) {
var (
err error
)
err, detail := alipay.Cancel(payId)
if err != nil {
return false, err
}
if detail.Result.ResultStatus != "S" {
return false, errors.New(400, "", "cancel failed")
}
return true, err
}
func (b *PaypalBot) RefundOrder(ctx context.Context, info *Refund) (RefundResp, error) {
return RefundResp{}, nil
}
func (b *AlipayGBot) RefundOrder(ctx context.Context, info *Refund) (RefundResp, error) {
var (
err error
params alipay.RefundRequest
result RefundResp
)
params.RefundRequestId = info.TransactionId
params.PaymentId = info.PayId
params.ReferenceRefundId = "R-" + info.TransactionId
params.RefundAmount.Currency = info.Currency
params.RefundAmount.Value = info.Amount
params.RefundReason = info.Reason
params.RefundNotifyUrl = info.NotifyUrl
err, detail := alipay.Refund(&params)
if err != nil {
return result, err
}
if detail.Result.ResultStatus == "S" {
result.Status = "SUCCESS"
} else if detail.Result.ResultStatus == "F" {
result.Status = "FAIL"
} else {
result.Status = "UNKNOWN"
}
result.RefundId = detail.RefundId
return result, err
}