2015-10-23 04:29:36 +02:00
package paypalsdk
2015-11-02 04:34:16 +01:00
import (
"bytes"
2015-11-02 10:39:07 +01:00
"errors"
2015-11-02 04:34:16 +01:00
"fmt"
"net/http"
)
2015-10-23 04:29:36 +02:00
2015-11-25 11:30:25 +01:00
// ListPaymentsResp slice of payments
type ListPaymentsResp struct {
Payments [ ] Payment ` json:"payments" `
}
2015-12-29 10:21:11 +01:00
// CreatePaymentResp contains Payment Info and Links slice
2015-12-01 05:35:25 +01:00
type CreatePaymentResp struct {
* Payment
2016-12-19 06:55:00 +01:00
Links [ ] Link ` json:"links" `
2015-12-01 05:35:25 +01:00
}
2015-12-29 10:21:11 +01:00
// CreateDirectPaypalPayment sends request to create a payment with payment_method=paypal
2015-12-01 05:35:25 +01:00
// CreatePayment is more common function for any kind of payment
2015-12-29 10:21:11 +01:00
// Endpoint: POST /v1/payments/payment
2015-11-24 10:03:13 +01:00
func ( c * Client ) CreateDirectPaypalPayment ( amount Amount , redirectURI string , cancelURI string , description string ) ( * PaymentResponse , error ) {
2018-10-13 23:45:06 +02:00
buf := bytes . NewBuffer ( [ ] byte ( ` { "intent":"sale","payer": { "payment_method":"paypal"}, ` +
` "transactions":[ { "amount": { "total":" ` + amount . Total +
` ","currency":" ` + amount . Currency + ` "},"description":" ` + description + ` "}],"redirect_urls": { "return_url":" ` +
redirectURI + ` ","cancel_url":" ` + cancelURI + ` "}} ` ) )
2015-11-02 04:34:16 +01:00
req , err := http . NewRequest ( "POST" , fmt . Sprintf ( "%s%s" , c . APIBase , "/v1/payments/payment" ) , buf )
if err != nil {
return & PaymentResponse { } , err
}
req . SetBasicAuth ( c . ClientID , c . Secret )
req . Header . Set ( "Authorization" , "Bearer " + c . Token . Token )
p := PaymentResponse { }
2017-11-23 03:15:11 +01:00
if err = c . SendWithAuth ( req , & p ) ; err != nil {
2015-11-25 11:30:25 +01:00
return & p , err
}
2015-11-02 04:34:16 +01:00
2015-11-02 10:39:07 +01:00
if p . ID == "" {
return & p , errors . New ( "Unable to create payment with this access token" )
}
return & p , err
2015-10-23 04:29:36 +02:00
}
2015-11-16 06:11:27 +01:00
2015-12-01 05:35:25 +01:00
// CreatePayment creates a payment in Paypal
2015-12-29 10:21:11 +01:00
// Depending on the payment_method and the funding_instrument, you can use the payment resource for direct credit card payments, stored credit card payments, or PayPal account payments.
// Endpoint: POST /v1/payments/payment
2015-12-01 05:35:25 +01:00
func ( c * Client ) CreatePayment ( p Payment ) ( * CreatePaymentResp , error ) {
req , err := c . NewRequest ( "POST" , fmt . Sprintf ( "%s%s" , c . APIBase , "/v1/payments/payment" ) , p )
if err != nil {
return & CreatePaymentResp { } , err
}
response := & CreatePaymentResp { }
2017-11-23 03:15:11 +01:00
if err = c . SendWithAuth ( req , response ) ; err != nil {
2015-12-01 05:35:25 +01:00
return response , err
}
return response , nil
}
2015-12-29 10:21:11 +01:00
// ExecuteApprovedPayment - Use this call to execute (complete) a PayPal payment that has been approved by the payer. You can optionally update transaction information when executing the payment by passing in one or more transactions.
// Endpoint: POST /v1/payments/payment/paymentID/execute
2015-11-16 06:11:27 +01:00
func ( c * Client ) ExecuteApprovedPayment ( paymentID string , payerID string ) ( * ExecuteResponse , error ) {
2018-10-13 23:45:06 +02:00
buf := bytes . NewBuffer ( [ ] byte ( ` { "payer_id":" ` + payerID + ` "} ` ) )
2015-11-16 06:11:27 +01:00
req , err := http . NewRequest ( "POST" , fmt . Sprintf ( "%s%s" , c . APIBase , "/v1/payments/payment/" + paymentID + "/execute" ) , buf )
if err != nil {
return & ExecuteResponse { } , err
}
req . SetBasicAuth ( c . ClientID , c . Secret )
req . Header . Set ( "Authorization" , "Bearer " + c . Token . Token )
e := ExecuteResponse { }
2017-11-23 03:15:11 +01:00
if err = c . SendWithAuth ( req , & e ) ; err != nil {
2015-11-25 11:30:25 +01:00
return & e , err
}
2015-11-16 06:11:27 +01:00
if e . ID == "" {
return & e , errors . New ( "Unable to execute payment with paymentID=" + paymentID )
}
return & e , err
}
2015-11-25 11:30:25 +01:00
// GetPayment gets a payment from PayPal
2015-12-29 10:21:11 +01:00
// Endpoint: GET /v1/payments/payment/ID
2015-11-25 11:30:25 +01:00
func ( c * Client ) GetPayment ( paymentID string ) ( * Payment , error ) {
p := Payment { }
req , err := http . NewRequest ( "GET" , fmt . Sprintf ( "%s%s" , c . APIBase , "/v1/payments/payment/" + paymentID ) , nil )
if err != nil {
return & p , err
}
2017-11-23 03:15:11 +01:00
if err = c . SendWithAuth ( req , & p ) ; err != nil {
2015-11-25 11:30:25 +01:00
return & p , err
}
if p . ID == "" {
return & p , errors . New ( "Unable to get payment with paymentID=" + paymentID )
}
return & p , nil
}
2018-01-14 07:37:24 +01:00
// PatchPayment modifies some fields of a payment prior to execution
// Endpoint: PATCH /v1/payments/payment/ID
func ( c * Client ) PatchPayment ( paymentID string , p [ ] PaymentPatch ) ( * Payment , error ) {
req , err := c . NewRequest ( "PATCH" , fmt . Sprintf ( "%s/v1/payments/payment/%s" , c . APIBase , paymentID ) , p )
if err != nil {
return nil , err
}
response := Payment { }
if err = c . SendWithAuth ( req , & response ) ; err != nil {
return nil , err
}
return & response , nil
}
2015-11-25 11:30:25 +01:00
// GetPayments retrieve payments resources from Paypal
2015-12-29 10:21:11 +01:00
// Endpoint: GET /v1/payments/payment/
2015-11-25 11:30:25 +01:00
func ( c * Client ) GetPayments ( ) ( [ ] Payment , error ) {
var p ListPaymentsResp
req , err := http . NewRequest ( "GET" , fmt . Sprintf ( "%s%s" , c . APIBase , "/v1/payments/payment/" ) , nil )
if err != nil {
return p . Payments , err
}
2017-11-23 03:15:11 +01:00
if err = c . SendWithAuth ( req , & p ) ; err != nil {
2015-11-25 11:30:25 +01:00
return p . Payments , err
}
return p . Payments , nil
}