Add payment-source type

This commit is contained in:
Alex Pliutau 2019-06-24 13:01:15 +02:00
parent da794934b8
commit 3f993bc542
4 changed files with 43 additions and 19 deletions

View File

@ -118,13 +118,13 @@ order, err := c.CreateOrder(paypalsdk.OrderIntentCapture, []paypalsdk.PurchaseUn
### Authorize Order
```go
auth, err := c.AuthorizeOrder(orderID, &paypalsdk.Amount{Total: "7.00", Currency: "USD"})
auth, err := c.AuthorizeOrder(orderID, paypalsdk.PaymentSource{})
```
### Capture Order
```go
capture, err := c.CaptureOrder(orderID, &paypalsdk.Amount{Total: "7.00", Currency: "USD"}, true, nil)
capture, err := c.CaptureOrder(orderID, paypalsdk.PaymentSource{})
```
### Void Order

View File

@ -122,7 +122,7 @@ func TestAuthorizeOrder(t *testing.T) {
c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
c.GetAccessToken()
_, err := c.AuthorizeOrder(testOrderID, &Amount{Total: "7.00", Currency: "USD"})
_, err := c.AuthorizeOrder(testOrderID, PaymentSource{})
if err == nil {
t.Errorf("Order is expired, 400 error must be returned")
}
@ -132,7 +132,7 @@ func TestCaptureOrder(t *testing.T) {
c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
c.GetAccessToken()
_, err := c.CaptureOrder(testOrderID, &Amount{Total: "100", Currency: "USD"}, true, nil)
_, err := c.CaptureOrder(testOrderID, PaymentSource{})
if err == nil {
t.Errorf("Order is expired, 400 error must be returned")
}

View File

@ -42,14 +42,10 @@ func (c *Client) CreateOrder(intent string, purchaseUnits []PurchaseUnitRequest,
// AuthorizeOrder - Use this call to authorize an order.
// Endpoint: POST /v2/checkout/orders/ID/authorize
func (c *Client) AuthorizeOrder(orderID string, amount *Amount) (*Authorization, error) {
type authRequest struct {
Amount *Amount `json:"amount"`
}
func (c *Client) AuthorizeOrder(orderID string, paymentSource PaymentSource) (*Authorization, error) {
auth := &Authorization{}
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/checkout/orders/"+orderID+"/authorize"), authRequest{Amount: amount})
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/checkout/orders/"+orderID+"/authorize"), paymentSource)
if err != nil {
return auth, err
}
@ -61,18 +57,12 @@ func (c *Client) AuthorizeOrder(orderID string, amount *Amount) (*Authorization,
return auth, nil
}
// CaptureOrder - Use this call to capture a payment on an order. To use this call, an original payment call must specify an intent of order.
// CaptureOrder - Use this call to capture a payment on an order.
// Endpoint: POST /v2/checkout/orders/ID/capture
func (c *Client) CaptureOrder(orderID string, amount *Amount, isFinalCapture bool, currency *Currency) (*Capture, error) {
type captureRequest struct {
Amount *Amount `json:"amount"`
IsFinalCapture bool `json:"is_final_capture"`
Currency *Currency `json:"transaction_fee"`
}
func (c *Client) CaptureOrder(orderID string, paymentSource PaymentSource) (*Capture, error) {
capture := &Capture{}
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/checkout/orders/"+orderID+"/capture"), captureRequest{Amount: amount, IsFinalCapture: isFinalCapture, Currency: currency})
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/checkout/orders/"+orderID+"/capture"), paymentSource)
if err != nil {
return capture, err
}

View File

@ -429,6 +429,40 @@ type (
Links []Link `json:"links"`
}
// PaymentSource structure
PaymentSource struct {
Card *PaymentSourceCard `json:"card"`
Token *PaymentSourceToken `json:"token"`
}
// PaymentSourceCard structure
PaymentSourceCard struct {
ID string `json:"id"`
Name string `json:"name"`
Number string `json:"number"`
Expiry string `json:"expiry"`
SecurityCode string `json:"security_code"`
LastDigits string `json:"last_digits"`
CardType string `json:"card_type"`
BillingAddress *CardBillingAddress `json:"billing_address"`
}
// CardBillingAddress structure
CardBillingAddress struct {
AddressLine1 string `json:"address_line_1"`
AddressLine2 string `json:"address_line_2"`
AdminArea2 string `json:"admin_area_2"`
AdminArea1 string `json:"admin_area_1"`
PostalCode string `json:"postal_code"`
CountryCode string `json:"country_code"`
}
// PaymentSourceToken structure
PaymentSourceToken struct {
ID string `json:"id"`
Type string `json:"type"`
}
// Payout struct
Payout struct {
SenderBatchHeader *SenderBatchHeader `json:"sender_batch_header"`