2023-05-30 19:57:30 +08:00
|
|
|
// Copyright 2023 The Casdoor Authors. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package pp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-06-12 13:43:37 +08:00
|
|
|
"errors"
|
2023-05-30 19:57:30 +08:00
|
|
|
"net/http"
|
2023-06-12 13:43:37 +08:00
|
|
|
"strconv"
|
2023-05-30 19:57:30 +08:00
|
|
|
|
2023-07-30 11:54:42 +08:00
|
|
|
"github.com/casdoor/casdoor/conf"
|
|
|
|
|
2023-06-12 13:43:37 +08:00
|
|
|
"github.com/go-pay/gopay"
|
|
|
|
"github.com/go-pay/gopay/paypal"
|
|
|
|
"github.com/go-pay/gopay/pkg/util"
|
2023-05-30 19:57:30 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type PaypalPaymentProvider struct {
|
|
|
|
Client *paypal.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPaypalPaymentProvider(clientID string, secret string) (*PaypalPaymentProvider, error) {
|
|
|
|
pp := &PaypalPaymentProvider{}
|
2023-07-30 11:54:42 +08:00
|
|
|
isProd := false
|
|
|
|
if conf.GetConfigString("runmode") == "prod" {
|
|
|
|
isProd = true
|
|
|
|
}
|
|
|
|
client, err := paypal.NewClient(clientID, secret, isProd)
|
|
|
|
//if !isProd {
|
|
|
|
// client.DebugSwitch = gopay.DebugOn
|
|
|
|
//}
|
2023-05-30 19:57:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pp.Client = client
|
|
|
|
return pp, nil
|
|
|
|
}
|
|
|
|
|
2023-06-12 13:43:37 +08:00
|
|
|
func (pp *PaypalPaymentProvider) Pay(providerName string, productName string, payerName string, paymentName string, productDisplayName string, price float64, currency string, returnUrl string, notifyUrl string) (string, string, error) {
|
2023-07-30 11:54:42 +08:00
|
|
|
// https://github.com/go-pay/gopay/blob/main/doc/paypal.md
|
2023-06-12 13:43:37 +08:00
|
|
|
priceStr := strconv.FormatFloat(price, 'f', 2, 64)
|
2023-07-30 11:54:42 +08:00
|
|
|
units := make([]*paypal.PurchaseUnit, 0, 1)
|
|
|
|
unit := &paypal.PurchaseUnit{
|
2023-06-12 13:43:37 +08:00
|
|
|
ReferenceId: util.GetRandomString(16),
|
|
|
|
Amount: &paypal.Amount{
|
2023-07-30 11:54:42 +08:00
|
|
|
CurrencyCode: currency, // e.g."USD"
|
|
|
|
Value: priceStr, // e.g."100.00"
|
2023-06-12 13:43:37 +08:00
|
|
|
},
|
|
|
|
Description: joinAttachString([]string{productDisplayName, productName, providerName}),
|
|
|
|
}
|
2023-07-30 11:54:42 +08:00
|
|
|
units = append(units, unit)
|
2023-06-12 13:43:37 +08:00
|
|
|
|
|
|
|
bm := make(gopay.BodyMap)
|
|
|
|
bm.Set("intent", "CAPTURE")
|
2023-07-30 11:54:42 +08:00
|
|
|
bm.Set("purchase_units", units)
|
2023-06-13 23:33:03 +08:00
|
|
|
bm.SetBodyMap("application_context", func(b gopay.BodyMap) {
|
|
|
|
b.Set("brand_name", "Casdoor")
|
|
|
|
b.Set("locale", "en-PT")
|
|
|
|
b.Set("return_url", returnUrl)
|
2023-07-30 11:54:42 +08:00
|
|
|
b.Set("cancel_url", returnUrl)
|
2023-06-12 13:43:37 +08:00
|
|
|
})
|
2023-06-13 23:33:03 +08:00
|
|
|
|
2023-06-12 13:43:37 +08:00
|
|
|
ppRsp, err := pp.Client.CreateOrder(context.Background(), bm)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
if ppRsp.Code != paypal.Success {
|
|
|
|
return "", "", errors.New(ppRsp.Error)
|
2023-05-30 19:57:30 +08:00
|
|
|
}
|
2023-07-30 11:54:42 +08:00
|
|
|
// {"id":"9BR68863NE220374S","status":"CREATED",
|
|
|
|
// "links":[{"href":"https://api.sandbox.paypal.com/v2/checkout/orders/9BR68863NE220374S","rel":"self","method":"GET"},
|
|
|
|
// {"href":"https://www.sandbox.paypal.com/checkoutnow?token=9BR68863NE220374S","rel":"approve","method":"GET"},
|
|
|
|
// {"href":"https://api.sandbox.paypal.com/v2/checkout/orders/9BR68863NE220374S","rel":"update","method":"PATCH"},
|
|
|
|
// {"href":"https://api.sandbox.paypal.com/v2/checkout/orders/9BR68863NE220374S/capture","rel":"capture","method":"POST"}]}
|
2023-06-12 13:43:37 +08:00
|
|
|
return ppRsp.Response.Links[1].Href, ppRsp.Response.Id, nil
|
|
|
|
}
|
2023-05-30 19:57:30 +08:00
|
|
|
|
2023-07-30 11:54:42 +08:00
|
|
|
func (pp *PaypalPaymentProvider) Notify(request *http.Request, body []byte, authorityPublicKey string, orderId string) (*NotifyResult, error) {
|
|
|
|
captureRsp, err := pp.Client.OrderCapture(context.Background(), orderId, nil)
|
2023-06-12 13:43:37 +08:00
|
|
|
if err != nil {
|
2023-07-30 11:54:42 +08:00
|
|
|
return nil, err
|
2023-06-12 13:43:37 +08:00
|
|
|
}
|
2023-07-30 11:54:42 +08:00
|
|
|
if captureRsp.Code != paypal.Success {
|
|
|
|
// If order is already captured, just skip this type of error and check the order detail
|
|
|
|
if !(len(captureRsp.ErrorResponse.Details) == 1 && captureRsp.ErrorResponse.Details[0].Issue == "ORDER_ALREADY_CAPTURED") {
|
|
|
|
return nil, errors.New(captureRsp.ErrorResponse.Message)
|
|
|
|
}
|
2023-05-30 19:57:30 +08:00
|
|
|
}
|
2023-07-30 11:54:42 +08:00
|
|
|
// Check the order detail
|
|
|
|
detailRsp, err := pp.Client.OrderDetail(context.Background(), orderId, nil)
|
2023-05-30 19:57:30 +08:00
|
|
|
if err != nil {
|
2023-07-30 11:54:42 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if captureRsp.Code != paypal.Success {
|
|
|
|
return nil, errors.New(captureRsp.ErrorResponse.Message)
|
2023-05-30 19:57:30 +08:00
|
|
|
}
|
|
|
|
|
2023-07-30 11:54:42 +08:00
|
|
|
paymentName := detailRsp.Response.Id
|
|
|
|
price, err := strconv.ParseFloat(detailRsp.Response.PurchaseUnits[0].Amount.Value, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
currency := detailRsp.Response.PurchaseUnits[0].Amount.CurrencyCode
|
|
|
|
productDisplayName, productName, providerName, err := parseAttachString(detailRsp.Response.PurchaseUnits[0].Description)
|
2023-05-30 19:57:30 +08:00
|
|
|
if err != nil {
|
2023-07-30 11:54:42 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// TODO: status better handler, e.g.`hanging`
|
|
|
|
var paymentStatus PaymentState
|
|
|
|
switch detailRsp.Response.Status { // CREATED、SAVED、APPROVED、VOIDED、COMPLETED、PAYER_ACTION_REQUIRED
|
|
|
|
case "COMPLETED":
|
|
|
|
paymentStatus = PaymentStatePaid
|
|
|
|
default:
|
|
|
|
paymentStatus = PaymentStateError
|
2023-05-30 19:57:30 +08:00
|
|
|
}
|
2023-07-30 11:54:42 +08:00
|
|
|
notifyResult := &NotifyResult{
|
|
|
|
PaymentStatus: paymentStatus,
|
|
|
|
PaymentName: paymentName,
|
2023-05-30 19:57:30 +08:00
|
|
|
|
2023-07-30 11:54:42 +08:00
|
|
|
ProductName: productName,
|
|
|
|
ProductDisplayName: productDisplayName,
|
|
|
|
ProviderName: providerName,
|
|
|
|
Price: price,
|
|
|
|
Currency: currency,
|
|
|
|
|
|
|
|
OutOrderId: orderId,
|
|
|
|
}
|
|
|
|
return notifyResult, nil
|
2023-05-30 19:57:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pp *PaypalPaymentProvider) GetInvoice(paymentName string, personName string, personIdCard string, personEmail string, personPhone string, invoiceType string, invoiceTitle string, invoiceTaxId string) (string, error) {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pp *PaypalPaymentProvider) GetResponseError(err error) string {
|
|
|
|
if err == nil {
|
|
|
|
return "success"
|
|
|
|
} else {
|
|
|
|
return "fail"
|
|
|
|
}
|
|
|
|
}
|