paypal/billing_test.go

112 lines
2.5 KiB
Go
Raw Normal View History

2019-08-21 15:50:20 +02:00
package paypal_test
2017-10-03 05:30:39 +02:00
import (
"fmt"
"time"
2019-08-21 15:53:29 +02:00
paypal "github.com/plutov/paypal"
2017-10-03 05:30:39 +02:00
)
func BillingExample() {
2019-08-21 15:53:29 +02:00
plan := paypal.BillingPlan{
2017-10-03 05:30:39 +02:00
Name: "Plan with Regular and Trial Payment Definitions",
Description: "Plan with regular and trial payment definitions.",
Type: "fixed",
2019-08-21 15:53:29 +02:00
PaymentDefinitions: []paypal.PaymentDefinition{
2019-08-19 15:33:46 +02:00
{
2017-10-03 05:30:39 +02:00
Name: "Regular payment definition",
Type: "REGULAR",
Frequency: "MONTH",
FrequencyInterval: "2",
2019-08-21 15:53:29 +02:00
Amount: paypal.AmountPayout{
2017-10-03 05:30:39 +02:00
Value: "100",
Currency: "USD",
},
Cycles: "12",
2019-08-21 15:53:29 +02:00
ChargeModels: []paypal.ChargeModel{
2019-08-19 15:33:46 +02:00
{
2017-10-03 05:30:39 +02:00
Type: "SHIPPING",
2019-08-21 15:53:29 +02:00
Amount: paypal.AmountPayout{
2017-10-03 05:30:39 +02:00
Value: "10",
Currency: "USD",
},
},
2019-08-19 15:33:46 +02:00
{
2017-10-03 05:30:39 +02:00
Type: "TAX",
2019-08-21 15:53:29 +02:00
Amount: paypal.AmountPayout{
2017-10-03 05:30:39 +02:00
Value: "12",
Currency: "USD",
},
},
},
},
2019-08-19 15:33:46 +02:00
{
2017-10-03 05:30:39 +02:00
Name: "Trial payment definition",
Type: "trial",
Frequency: "week",
FrequencyInterval: "5",
2019-08-21 15:53:29 +02:00
Amount: paypal.AmountPayout{
2017-10-03 05:30:39 +02:00
Value: "9.19",
Currency: "USD",
},
Cycles: "2",
2019-08-21 15:53:29 +02:00
ChargeModels: []paypal.ChargeModel{
2019-08-19 15:33:46 +02:00
{
2017-10-03 05:30:39 +02:00
Type: "SHIPPING",
2019-08-21 15:53:29 +02:00
Amount: paypal.AmountPayout{
2017-10-03 05:30:39 +02:00
Value: "1",
Currency: "USD",
},
},
2019-08-19 15:33:46 +02:00
{
2017-10-03 05:30:39 +02:00
Type: "TAX",
2019-08-21 15:53:29 +02:00
Amount: paypal.AmountPayout{
2017-10-03 05:30:39 +02:00
Value: "2",
Currency: "USD",
},
},
},
},
},
2019-08-21 15:53:29 +02:00
MerchantPreferences: &paypal.MerchantPreferences{
SetupFee: &paypal.AmountPayout{
2017-10-03 05:30:39 +02:00
Value: "1",
Currency: "USD",
},
ReturnURL: "http://www.paypal.com",
CancelURL: "http://www.paypal.com/cancel",
AutoBillAmount: "YES",
InitialFailAmountAction: "CONTINUE",
MaxFailAttempts: "0",
},
}
2019-08-21 15:53:29 +02:00
c, err := paypal.NewClient("clientID", "secretID", paypal.APIBaseSandBox)
2017-10-03 05:30:39 +02:00
if err != nil {
panic(err)
}
_, err = c.GetAccessToken()
if err != nil {
panic(err)
}
planResp, err := c.CreateBillingPlan(plan)
if err != nil {
panic(err)
}
err = c.ActivatePlan(planResp.ID)
fmt.Println(err)
2019-08-21 15:53:29 +02:00
agreement := paypal.BillingAgreement{
2017-10-03 05:30:39 +02:00
Name: "Fast Speed Agreement",
Description: "Agreement for Fast Speed Plan",
2019-08-21 15:53:29 +02:00
StartDate: paypal.JSONTime(time.Now().Add(time.Hour * 24)),
Plan: paypal.BillingPlan{ID: planResp.ID},
Payer: paypal.Payer{
2017-10-03 05:30:39 +02:00
PaymentMethod: "paypal",
},
}
resp, err := c.CreateBillingAgreement(agreement)
fmt.Println(err, resp)
2019-01-14 06:06:05 +01:00
2019-08-21 15:53:29 +02:00
bps, err := c.ListBillingPlans(paypal.BillingPlanListParams{Status: "ACTIVE"})
2019-01-14 06:06:05 +01:00
fmt.Println(err, bps)
2017-10-03 05:30:39 +02:00
}