Skip to content

Commit

Permalink
feat: paypal add Interface of createBillingPlan (#250)
Browse files Browse the repository at this point in the history
* feat: paypal add Interface of createBillingPlan
  • Loading branch information
CrazyZard authored May 10, 2022
1 parent be9d9a9 commit 428385e
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 1 deletion.
4 changes: 3 additions & 1 deletion doc/paypal.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ return
### PayPal API

* <font color='#003087' size='4'>AccessToken</font>
* 获取AccessToken(Get AccessToken):`client.GetAccessToken()`
* 获取AccessToken(Get AccessToken):`client.GetAccessToken()`
* <font color='#003087' size='4'>订单</font>
* 创建订单(Create order):`client.CreateOrder()`
* 订单详情(Show order details):`client.OrderDetail()`
Expand All @@ -122,3 +122,5 @@ return
* 支付捕获详情(Show captured payment details):`client.PaymentCaptureDetail()`
* 支付捕获退款(Refund captured payment):`client.PaymentCaptureRefund()`
* 支付退款详情(Show refund details):`client.PaymentRefundDetail()`
* <font color='#003087' size='4'>订阅</font>
* 创建订单(Create order):`client.CreateBillingPlan()`
2 changes: 2 additions & 0 deletions paypal/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ const (
paymentCaptureRefund = "/v2/payments/captures/%s/refund" // capture_id 支付捕获退款 POST
paymentRefundDetail = "/v2/payments/refunds/%s" // refund_id 支付退款详情 GET

// 订阅相关
subscriptionCreate = "/v1/billing/plans" // 创建订阅 POST
)
56 changes: 56 additions & 0 deletions paypal/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,59 @@ type NetAmountBreakdown struct {
ConvertedAmount *Amount `json:"converted_amount,omitempty"`
ExchangeRate *ExchangeRate `json:"exchange_rate,omitempty"`
}

// Subscription Model

type Frequency struct {
IntervalUnit string `json:"interval_unit"`
IntervalCount int `json:"interval_count"`
}

type PricingScheme struct {
FixedPrice *FixedPrice `json:"fixed_price"`
}

type FixedPrice struct {
Value string `json:"value"`
CurrencyCode string `json:"currency_code"`
}

type BillingCycles struct {
Frequency *Frequency `json:"frequency"`
TenureType string `json:"tenure_type"`
Sequence int `json:"sequence"`
TotalCycles int `json:"total_cycles"`
PricingScheme *PricingScheme `json:"pricing_scheme"`
}

type Plans struct {
ProductId string `json:"product_id"`
Name string `json:"name"`
Description string `json:"description"`
BillingCycles []*BillingCycles `json:"billing_cycles"`
PaymentDefinitions *PaymentPreferences `json:"payment_preferences"`
}

type PaymentPreferences struct {
AutoBillOutstanding bool `json:"auto_bill_outstanding"`
SetupFeeFailureAction string `json:"setup_fee_failure_action"`
PaymentFailureThreshold int `json:"payment_failure_threshold"`
}

type CreateBillingRsp struct {
Code int `json:"-"`
Error string `json:"-"`
ErrorResponse *ErrorResponse `json:"-"`
Response *BillingDetail `json:"response,omitempty"`
}

type BillingDetail struct {
ID string `json:"id"`
ProductID string `json:"product_id"`
Name string `json:"name"`
Status string `json:"status"`
Description string `json:"description"`
UsageType string `json:"usage_type"`
CreateTime string `json:"create_time"`
Links []*Link `json:"links"`
}
34 changes: 34 additions & 0 deletions paypal/subscriptions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package paypal

import (
"context"
"encoding/json"
"fmt"
"github.com/go-pay/gopay"
"net/http"
)

// 创建订阅计划(CreateBillingPlan)
// Code = 0 is success
// 文档:https://developer.paypal.com/docs/api/subscriptions/v1/#plans_create
func (c *Client) CreateBillingPlan(ctx context.Context, bm gopay.BodyMap) (ppRsp *CreateBillingRsp, err error) {
if err = bm.CheckEmptyError("product_id", "billing_cycles"); err != nil {
return nil, err
}
res, bs, err := c.doPayPalPost(ctx, bm, subscriptionCreate)
if err != nil {
return nil, err
}
ppRsp = &CreateBillingRsp{Code: Success}
ppRsp.Response = new(BillingDetail)
if err = json.Unmarshal(bs, ppRsp.Response); err != nil {
return nil, fmt.Errorf("[%w]: %v, bytes: %s", gopay.UnmarshalErr, err, string(bs))
}
if res.StatusCode != http.StatusCreated {
ppRsp.Code = res.StatusCode
ppRsp.Error = string(bs)
ppRsp.ErrorResponse = new(ErrorResponse)
_ = json.Unmarshal(bs, ppRsp.ErrorResponse)
}
return ppRsp, nil
}
57 changes: 57 additions & 0 deletions paypal/subscriptions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package paypal

import (
"github.com/go-pay/gopay"
"github.com/go-pay/gopay/pkg/xlog"
"testing"
)

func TestClient_CreateBillingPlan(t *testing.T) {
var billingCycles []*BillingCycles
var billingCycle = &BillingCycles{
Frequency: &Frequency{
IntervalUnit: "MONTH",
IntervalCount: 1,
},
TenureType: "REGULAR",
Sequence: 1,
TotalCycles: 0,
PricingScheme: &PricingScheme{
FixedPrice: &FixedPrice{
Value: "101",
CurrencyCode: "USD",
},
},
}
billingCycles = append(billingCycles, billingCycle)

// 创建 PayPal 支付订单
bm := make(gopay.BodyMap)
bm.Set("product_id", "PROD-9TH539347F0791830").
Set("name", "gopay").
Set("billing_cycles", billingCycles).
Set("description", "Monthly subscription for premium users").
Set("payment_preferences", &PaymentPreferences{
AutoBillOutstanding: true,
SetupFeeFailureAction: "CONTINUE",
PaymentFailureThreshold: 3,
})

xlog.Debug("bm:", bm.JsonBody())

ppRsp, err := client.CreateBillingPlan(ctx, bm)
if err != nil {
xlog.Error(err)
return
}
if ppRsp.Code != Success {
xlog.Debugf("ppRsp.Code: %+v", ppRsp.Code)
xlog.Debugf("ppRsp.Error: %+v", ppRsp.Error)
xlog.Debugf("ppRsp.ErrorResponse: %+v", ppRsp.ErrorResponse)
return
}
xlog.Debugf("ppRsp.Response: %+v", ppRsp.Response)
for _, v := range ppRsp.Response.Links {
xlog.Debugf("ppRsp.Response.Links: %+v", v)
}
}

0 comments on commit 428385e

Please sign in to comment.