蛋蛋星球-制度模式
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.

paypal.md 6.0 KiB

1 month ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. ## PayPal
  2. > 具体API使用介绍,请参考`gopay/paypal/client_test.go`,`gopay/paypal/order_test.go`,`gopay/paypal/payment_test.go` 等xxx_test.go
  3. - 已实现API列表附录:[API List](https://github.com/go-pay/gopay/blob/main/doc/paypal.md#%E9%99%84%E5%BD%95)
  4. - PayPal官方文档:[Official Document](https://developer.paypal.com/api/rest)
  5. ---
  6. ### 1、初始化PayPal客户端并做配置(Init PayPal Client)
  7. ```go
  8. import (
  9. "github.com/go-pay/gopay/paypal"
  10. "github.com/go-pay/xlog"
  11. )
  12. // 初始化PayPal支付客户端
  13. client, err := paypal.NewClient(Clientid, Secret, false)
  14. if err != nil {
  15. xlog.Error(err)
  16. return
  17. }
  18. // 自定义配置http请求接收返回结果body大小,默认 10MB
  19. client.SetBodySize() // 没有特殊需求,可忽略此配置
  20. // 打开Debug开关,输出日志,默认关闭
  21. client.DebugSwitch = gopay.DebugOn
  22. ```
  23. ### 2、API 方法调用及入参(Call API)
  24. > Orders:[Orders API](https://developer.paypal.com/api/orders/v2/)
  25. > Payments:[Payments API](https://developer.paypal.com/api/payments/v2/)
  26. > Subscriptions:[Subscriptions API](https://developer.paypal.com/docs/api/subscriptions/v1/)
  27. - Create Orders example
  28. ```go
  29. import (
  30. "github.com/go-pay/gopay"
  31. "github.com/go-pay/gopay/paypal"
  32. "github.com/go-pay/util"
  33. "github.com/go-pay/xlog"
  34. )
  35. // Create Orders example
  36. var pus []*paypal.PurchaseUnit
  37. var item = &paypal.PurchaseUnit{
  38. ReferenceId: util.GetRandomString(16),
  39. Amount: &paypal.Amount{
  40. CurrencyCode: "USD",
  41. Value: "8",
  42. },
  43. }
  44. pus = append(pus, item)
  45. bm := make(gopay.BodyMap)
  46. bm.Set("intent", "CAPTURE").
  47. Set("purchase_units", pus).
  48. SetBodyMap("payment_source", func(b gopay.BodyMap) {
  49. b.SetBodyMap("paypal", func(bb gopay.BodyMap) {
  50. bb.SetBodyMap("experience_context", func(bbb gopay.BodyMap) {
  51. bbb.Set("brand_name", "gopay").
  52. Set("locale", "en-US").
  53. Set("shipping_preference", "NO_SHIPPING").
  54. Set("user_action", "PAY_NOW").
  55. Set("return_url", "http://xxx/return").
  56. Set("cancel_url", "http://xxx/cancel")
  57. })
  58. })
  59. })
  60. ppRsp, err := client.CreateOrder(ctx, bm)
  61. if err != nil {
  62. xlog.Error(err)
  63. return
  64. }
  65. if ppRsp.Code != 200 {
  66. // do something
  67. return
  68. }
  69. ```
  70. - Capture payment for order
  71. ```go
  72. import (
  73. "github.com/go-pay/gopay"
  74. "github.com/go-pay/xlog"
  75. )
  76. // Capture payment for order
  77. //bm := make(gopay.BodyMap)
  78. //bm.SetBodyMap("payment_source", func(b gopay.BodyMap) {
  79. // b.SetBodyMap("token", func(b gopay.BodyMap) {
  80. // b.Set("id", "The PayPal-generated ID for the token").
  81. // Set("type", "BILLING_AGREEMENT")
  82. // })
  83. //})
  84. ppRsp, err := client.OrderCapture(ctx, "4X223967G91314611", nil)
  85. if err != nil {
  86. xlog.Error(err)
  87. return
  88. }
  89. if ppRsp.Code != paypal.Success {
  90. // do something
  91. return
  92. }
  93. ```
  94. ---
  95. ## 附录:
  96. ### PayPal API
  97. * <font color='#003087' size='4'>AccessToken</font>
  98. * 获取AccessToken(Get AccessToken):`client.GetAccessToken()`
  99. * <font color='#003087' size='4'>发票</font>
  100. * 生成发票号码(Generate invoice number):`client.InvoiceNumberGenerate()`
  101. * 发票列表(List invoices):`client.InvoiceList()`
  102. * 创建虚拟发票(Create draft invoice):`client.InvoiceCreate()`
  103. * 删除发票(Delete invoice):`client.InvoiceDelete()`
  104. * 更新发票(Fully update invoice):`client.InvoiceUpdate()`
  105. * 获取发票详情(Show invoice details):`client.InvoiceDetail()`
  106. * 生成发票二维码(Generate QR code):`client.InvoiceGenerateQRCode()`
  107. * 发票付款记录(Record payment for invoice):`client.InvoicePaymentRecord()`
  108. * 发票付款删除(Delete external payment):`client.InvoicePaymentDelete()`
  109. * 发票退款记录(Record refund for invoice):`client.InvoiceRefundRecord()`
  110. * 发票退款删除(Delete external refund):`client.InvoiceRefundDelete()`
  111. * 发送发票提醒(Send invoice reminder):`client.InvoiceSendRemind()`
  112. * 发送发票(Send invoice):`client.InvoiceSend()`
  113. * 发票搜索(Search for invoices):`client.InvoiceSearch()`
  114. * 发票模板列表(List templates):`client.InvoiceTemplateList()`
  115. * 创建发票模板(Create template):`client.InvoiceTemplateCreate()`
  116. * 删除发票模板(Delete template):`client.InvoiceTemplateDelete()`
  117. * 更新发票模板(Fully update template):`client.InvoiceTemplateUpdate()`
  118. * <font color='#003087' size='4'>订单</font>
  119. * 创建订单(Create order):`client.CreateOrder()`
  120. * 订单详情(Show order details):`client.OrderDetail()`
  121. * 更新订单(Update order):`client.UpdateOrder()`
  122. * 订单支付授权(Authorize payment for order):`client.OrderAuthorize()`
  123. * 订单支付捕获(Capture payment for order):`client.OrderCapture()`
  124. * 订单支付确认(Confirm the Order):`client.OrderConfirm()`
  125. * <font color='#003087' size='4'>支付</font>
  126. * 支付授权详情(Show details for authorized payment):`client.PaymentAuthorizeDetail()`
  127. * 重新授权支付授权(Reauthorize authorized payment):`client.PaymentReauthorize()`
  128. * 作废支付授权(Void authorized payment):`client.PaymentAuthorizeVoid()`
  129. * 支付授权捕获(Capture authorized payment):`client.PaymentAuthorizeCapture()`
  130. * 支付捕获详情(Show captured payment details):`client.PaymentCaptureDetail()`
  131. * 支付捕获退款(Refund captured payment):`client.PaymentCaptureRefund()`
  132. * 支付退款详情(Show refund details):`client.PaymentRefundDetail()`
  133. * <font color='#003087' size='4'>支出</font>
  134. * 创建批量支出(Create batch payout):`client.CreateBatchPayout()`
  135. * 批量支出详情(Show payout batch details):`client.ShowPayoutBatchDetails()`
  136. * 批量支出项目详情(Show Payout Item Details):`client.ShowPayoutItemDetails()`
  137. * 取消批量支付中收款人无PayPal账号的项目(Cancel Unclaimed Payout Item):`client.CancelUnclaimedPayoutItem()`
  138. * <font color='#003087' size='4'>订阅</font>
  139. * 创建订阅计划(Create plan):`client.CreateBillingPlan()`