PayPal

具体API使用介绍,请参考gopay/paypal/client_test.go,gopay/paypal/order_test.go,gopay/paypal/payment_test.go


1、初始化PayPal客户端并做配置(Init PayPal Client)

  1. import (
  2. "github.com/go-pay/gopay/paypal"
  3. "github.com/go-pay/gopay/pkg/xlog"
  4. )
  5. // 初始化PayPal支付客户端
  6. client, err := paypal.NewClient(Clientid, Secret, false)
  7. if err != nil {
  8. xlog.Error(err)
  9. return
  10. }
  11. // 打开Debug开关,输出日志,默认关闭
  12. client.DebugSwitch = gopay.DebugOn

2、API 方法调用及入参(Call API)

Orders:Orders API

Payments:Payments API

  • Create Orders example
  1. import (
  2. "github.com/go-pay/gopay"
  3. "github.com/go-pay/gopay/paypal"
  4. "github.com/go-pay/gopay/pkg/util"
  5. "github.com/go-pay/gopay/pkg/xlog"
  6. )
  7. // Create Orders example
  8. var pus []*paypal.PurchaseUnit
  9. var item = &paypal.PurchaseUnit{
  10. ReferenceId: util.GetRandomString(16),
  11. Amount: &paypal.Amount{
  12. CurrencyCode: "USD",
  13. Value: "8",
  14. },
  15. }
  16. pus = append(pus, item)
  17. bm := make(gopay.BodyMap)
  18. bm.Set("intent", "CAPTURE").
  19. Set("purchase_units", pus).
  20. SetBodyMap("application_context", func(b gopay.BodyMap) {
  21. b.Set("brand_name", "gopay").
  22. Set("locale", "en-PT").
  23. Set("return_url", "https://example.com/returnUrl").
  24. Set("cancel_url", "https://example.com/cancelUrl")
  25. })
  26. ppRsp, err := client.CreateOrder(ctx, bm)
  27. if err != nil {
  28. xlog.Error(err)
  29. return
  30. }
  31. if ppRsp.Code != paypal.Success {
  32. // do something
  33. return
  34. }
  • Capture payment for order
  1. import (
  2. "github.com/go-pay/gopay"
  3. "github.com/go-pay/gopay/pkg/xlog"
  4. )
  5. // Capture payment for order
  6. //bm := make(gopay.BodyMap)
  7. //bm.SetBodyMap("payment_source", func(b gopay.BodyMap) {
  8. // b.SetBodyMap("token", func(b gopay.BodyMap) {
  9. // b.Set("id", "The PayPal-generated ID for the token").
  10. // Set("type", "BILLING_AGREEMENT")
  11. // })
  12. //})
  13. ppRsp, err := client.OrderCapture(ctx, "4X223967G91314611", nil)
  14. if err != nil {
  15. xlog.Error(err)
  16. return
  17. }
  18. if ppRsp.Code != paypal.Success {
  19. // do something
  20. return
  21. }

附录:

PayPal API

  • AccessToken
    • 获取AccessToken(Get AccessToken):client.GetAccessToken()
  • 订单
    • 创建订单(Create order):client.CreateOrder()
    • 订单详情(Show order details):client.OrderDetail()
    • 更新订单(Update order):client.UpdateOrder()
    • 订单支付授权(Authorize payment for order):client.OrderAuthorize()
    • 订单支付捕获(Capture payment for order):client.OrderCapture()
    • 订单支付确认(Confirm the Order):client.OrderConfirm()
  • 支付
    • 支付授权详情(Show details for authorized payment):client.PaymentAuthorizeDetail()
    • 重新授权支付授权(Reauthorize authorized payment):client.PaymentReauthorize()
    • 作废支付授权(Void authorized payment):client.PaymentAuthorizeVoid()
    • 支付授权捕获(Capture authorized payment):client.PaymentAuthorizeCapture()
    • 支付捕获详情(Show captured payment details):client.PaymentCaptureDetail()
    • 支付捕获退款(Refund captured payment):client.PaymentCaptureRefund()
    • 支付退款详情(Show refund details):client.PaymentRefundDetail()