paypale/examples/main.go

173 lines
4.2 KiB
Go
Raw Normal View History

2015-10-14 07:30:28 +02:00
package main
import (
2016-01-05 07:08:02 +01:00
paypalsdk "PayPal-Go-SDK"
2015-11-25 11:30:25 +01:00
"strconv"
2015-10-14 07:30:28 +02:00
2015-10-30 08:02:32 +01:00
"fmt"
2015-10-14 07:30:28 +02:00
)
func main() {
2016-01-18 09:42:42 +01:00
c, err := paypalsdk.NewClient("AZgwu4yt5Ba0gyTu1dGBH3txHCJbMuFNvrmQxBaQbfDncDiCs6W_rwJD8Ir-0pZrN-_eq7n9zVd8Y-5f", "EBzA1wRl5t73OMugOieDj_tI3vihfJmGl47ukQT-cpctooIzDu0K7IPESNC0cKodlLSOXzwI8qXSM0rd", paypalsdk.APIBaseSandBox)
2015-10-30 08:02:32 +01:00
if err == nil {
2016-01-18 09:42:42 +01:00
fmt.Println("DEBUG: ClientID=" + c.ClientID + " APIBase=" + c.APIBase)
2015-10-30 08:02:32 +01:00
} else {
fmt.Println("ERROR: " + err.Error())
}
2015-11-25 11:30:25 +01:00
2016-01-18 09:42:42 +01:00
token, err := c.GetAccessToken()
2015-11-25 11:30:25 +01:00
if err == nil {
fmt.Println("DEBUG: AccessToken=" + token.Token)
} else {
fmt.Println("ERROR: " + err.Error())
}
2016-01-18 09:42:42 +01:00
payment, err := c.GetPayment("PAY-TEST-123")
2015-11-25 11:30:25 +01:00
fmt.Println("DEBUG: PaymentID=" + payment.ID)
2016-01-18 09:42:42 +01:00
payments, err := c.GetPayments()
2015-11-25 11:30:25 +01:00
if err == nil {
fmt.Println("DEBUG: PaymentsCount=" + strconv.Itoa(len(payments)))
} else {
fmt.Println("ERROR: " + err.Error())
}
p := paypalsdk.Payment{
Intent: "sale",
Payer: &paypalsdk.Payer{
PaymentMethod: "credit_card",
FundingInstruments: []paypalsdk.FundingInstrument{paypalsdk.FundingInstrument{
CreditCard: &paypalsdk.CreditCard{
Number: "4111111111111111",
Type: "visa",
ExpireMonth: "11",
ExpireYear: "2020",
CVV2: "777",
FirstName: "John",
LastName: "Doe",
},
}},
},
Transactions: []paypalsdk.Transaction{paypalsdk.Transaction{
Amount: &paypalsdk.Amount{
Currency: "USD",
Total: "200",
},
Description: "My Payment",
}},
RedirectURLs: &paypalsdk.RedirectURLs{
ReturnURL: "http://...",
CancelURL: "http://...",
},
}
2016-01-18 09:42:42 +01:00
paymentResponse, err := c.CreatePayment(p)
if err == nil {
fmt.Println("DEBUG: CreatedPaymentID=" + paymentResponse.Payment.ID)
} else {
fmt.Println("ERROR: " + err.Error())
2015-12-17 04:56:49 +01:00
}
fmt.Println("OK")
2016-01-18 09:42:42 +01:00
sale, err := c.GetSale("1")
2015-12-17 04:56:49 +01:00
if err == nil {
fmt.Println("DEBUG: SaleID=" + sale.ID)
} else {
fmt.Println("ERROR: " + err.Error())
}
fmt.Println("OK")
2016-01-18 09:42:42 +01:00
refund, err := c.RefundSale("1", &paypalsdk.Amount{Total: "7.00", Currency: "USD"})
2015-12-17 04:56:49 +01:00
if err == nil {
fmt.Println("DEBUG: RefundID=" + refund.ID)
} else {
fmt.Println("ERROR: " + err.Error())
}
2015-11-25 11:30:25 +01:00
fmt.Println("OK")
2015-12-17 05:28:26 +01:00
2016-01-18 09:42:42 +01:00
refund, err = c.GetRefund("1")
2015-12-17 05:28:26 +01:00
if err == nil {
fmt.Println("DEBUG: RefundID=" + refund.ID)
} else {
fmt.Println("ERROR: " + err.Error())
}
fmt.Println("OK")
2015-12-17 08:50:25 +01:00
2016-01-18 09:42:42 +01:00
order, err := c.GetOrder("1")
2015-12-17 08:50:25 +01:00
if err == nil {
fmt.Println("DEBUG: OrderID=" + order.ID)
} else {
fmt.Println("ERROR: " + err.Error())
}
fmt.Println("OK")
2016-01-18 09:42:42 +01:00
auth, err := c.AuthorizeOrder("1", &paypalsdk.Amount{Total: "7.00", Currency: "USD"})
2015-12-17 08:50:25 +01:00
if err == nil {
fmt.Println("DEBUG: AuthID=" + auth.ID)
} else {
fmt.Println("ERROR: " + err.Error())
}
fmt.Println("OK")
2016-01-18 09:42:42 +01:00
capture, err := c.CaptureOrder("1", &paypalsdk.Amount{Total: "7.00", Currency: "USD"}, true, nil)
2015-12-17 08:50:25 +01:00
if err == nil {
fmt.Println("DEBUG: CaptureID=" + capture.ID)
} else {
fmt.Println("ERROR: " + err.Error())
}
fmt.Println("OK")
2016-01-18 09:42:42 +01:00
order, err = c.VoidOrder("1")
2015-12-17 08:50:25 +01:00
if err == nil {
fmt.Println("DEBUG: OrderID=" + order.ID)
} else {
fmt.Println("ERROR: " + err.Error())
}
fmt.Println("OK")
2016-01-18 09:42:42 +01:00
token1, err := c.GrantNewAccessTokenFromAuthCode("123", "http://example.com/myapp/return.php")
if err == nil {
fmt.Println("DEBUG: Token=" + token1.Token)
} else {
fmt.Println("ERROR: " + err.Error())
}
token2, err := c.GrantNewAccessTokenFromRefreshToken("123")
if err == nil {
fmt.Println("DEBUG: Token=" + token2.Token)
} else {
fmt.Println("ERROR: " + err.Error())
}
u, err := c.GetUserInfo("openid")
if err == nil {
fmt.Println("DEBUG: UserID=" + u.ID)
} else {
fmt.Println("ERROR: " + err.Error())
}
2016-02-17 05:10:49 +01:00
payout := paypalsdk.Payout{
SenderBatchHeader: &paypalsdk.SenderBatchHeader{
EmailSubject: "Subject will be displayed on PayPal",
},
Items: []paypalsdk.PayoutItem{
paypalsdk.PayoutItem{
RecipientType: "EMAIL",
Receiver: "single-email-payout@mail.com",
Amount: &paypalsdk.AmountPayout{
Value: "15.11",
Currency: "USD",
},
Note: "Optional note",
SenderItemID: "Optional Item ID",
},
},
}
payoutResp, err := c.CreateSinglePayout(payout)
if err == nil && len(payoutResp.Items) > 0 {
fmt.Println("DEBUG: PayoutItemID=" + payoutResp.Items[0].PayoutItemID)
} else {
fmt.Println("ERROR: " + err.Error())
}
2015-10-14 07:30:28 +02:00
}