Merge pull request #90 from logpacker/payment-source

#89: V2: Add payment-source type
This commit is contained in:
Alex Pliutau 2019-07-22 14:56:01 +02:00 committed by GitHub
commit 0862ea5ecc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 163 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

@ -7,13 +7,9 @@ import (
)
// All test values are defined here
var testClientID = "AZgwu4yt5Ba0gyTu1dGBH3txHCJbMuFNvrmQxBaQbfDncDiCs6W_rwJD8Ir-0pZrN-_eq7n9zVd8Y-5f"
var testSecret = "EBzA1wRl5t73OMugOieDj_tI3vihfJmGl47ukQT-cpctooIzDu0K7IPESNC0cKodlLSOXzwI8qXSM0rd"
var testAuthID = "2DC87612EK520411B"
var testOrderID = "O-0PW72302W3743444R"
var testSaleID = "4CF18861HF410323U"
var testPayerID = "CR87QHB7JTRSC"
var testUserID = "https://www.paypal.com/webapps/auth/identity/user/WEssgRpQij92sE99_F9MImvQ8FPYgUEjrvCja2qH2H8"
var testClientID = "AQzSx89isj-yV7BhuN_TY1s4phiQXlcUEwFPUYD7tWFxts-bf2Zf6f_S0K7J_suOkiZuIKSkNnB1rem-"
var testSecret = "EAW_tyBnkTLxC7RB8CHT39QYZYfT7LwyxPsWle0834O60KGo0A351iMLOFdQBQ5q95DbZM1hOlT9w8Yg"
var testUserID = "https://www.paypal.com/webapps/auth/identity/user/VBqgHcgZwb1PBs69ybjjXfIW86_Hr93aBvF_Rgbh2II"
var testCardID = "CARD-54E6956910402550WKGRL6EA"
func TestGetAccessToken(t *testing.T) {
@ -27,114 +23,13 @@ func TestGetAccessToken(t *testing.T) {
}
}
func TestGetAuthorization(t *testing.T) {
c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
c.GetAccessToken()
_, err := c.GetAuthorization(testAuthID)
if err == nil {
t.Errorf("GetAuthorization expects error")
}
}
func TestCaptureAuthorization(t *testing.T) {
c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
c.GetAccessToken()
_, err := c.CaptureAuthorization(testAuthID, &Amount{Total: "200", Currency: "USD"}, true)
if err == nil {
t.Errorf("Auth is expired, 400 error must be returned")
}
}
func TestVoidAuthorization(t *testing.T) {
c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
c.GetAccessToken()
_, err := c.VoidAuthorization(testAuthID)
if err == nil {
t.Errorf("Auth is expired, 400 error must be returned")
}
}
func TestReauthorizeAuthorization(t *testing.T) {
c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
c.GetAccessToken()
_, err := c.ReauthorizeAuthorization(testAuthID, &Amount{Total: "200", Currency: "USD"})
if err == nil {
t.Errorf("Reauthorization not allowed for this product, 500 error must be returned")
}
}
func TestGrantNewAccessTokenFromAuthCode(t *testing.T) {
c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
_, err := c.GrantNewAccessTokenFromAuthCode("123", "http://example.com/myapp/return.php")
if err == nil {
t.Errorf("GrantNewAccessTokenFromAuthCode must return error for invalid code")
}
}
func TestGrantNewAccessTokenFromRefreshToken(t *testing.T) {
c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
_, err := c.GrantNewAccessTokenFromRefreshToken("123")
if err == nil {
t.Errorf("GrantNewAccessTokenFromRefreshToken must return error for invalid refresh token")
}
}
func TestGetUserInfo(t *testing.T) {
c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
c.GetAccessToken()
u, err := c.GetUserInfo("openid")
if u.ID != testUserID || err != nil {
t.Errorf("GetUserInfo must return valid test ID=%s, error: %v", testUserID, err)
}
}
func TestGetOrder(t *testing.T) {
c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
c.GetAccessToken()
_, err := c.GetOrder(testOrderID)
if err == nil {
t.Errorf("GetOrder expects error")
}
}
func TestCreateOrder(t *testing.T) {
c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
c.GetAccessToken()
order, err := c.CreateOrder(OrderIntentCapture, nil, nil, nil)
if err == nil {
t.Errorf("CreateOrder expects error")
}
}
func TestAuthorizeOrder(t *testing.T) {
c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
c.GetAccessToken()
_, err := c.AuthorizeOrder(testOrderID, &Amount{Total: "7.00", Currency: "USD"})
if err == nil {
t.Errorf("Order is expired, 400 error must be returned")
}
}
func TestCaptureOrder(t *testing.T) {
c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
c.GetAccessToken()
_, err := c.CaptureOrder(testOrderID, &Amount{Total: "100", Currency: "USD"}, true, nil)
if err == nil {
t.Errorf("Order is expired, 400 error must be returned")
t.Errorf("GetUserInfo must return valid test ID %s, got %s, error: %v", testUserID, u.ID, err)
}
}
@ -160,45 +55,10 @@ func TestCreateSinglePayout(t *testing.T) {
},
}
_, err := c.CreateSinglePayout(payout)
payoutRes, err := c.CreateSinglePayout(payout)
if err != nil {
t.Errorf("Test single payout is not created, error: %v", err)
}
}
func TestGetSale(t *testing.T) {
c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
c.GetAccessToken()
_, err := c.GetSale(testSaleID)
if err == nil {
t.Errorf("404 must be returned for ID=%s", testSaleID)
}
}
func TestRefundSale(t *testing.T) {
c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
c.GetAccessToken()
_, err := c.RefundSale(testSaleID, nil)
if err == nil {
t.Errorf("404 must be returned for ID=%s", testSaleID)
}
_, err = c.RefundSale(testSaleID, &Amount{Total: "7.00", Currency: "USD"})
if err == nil {
t.Errorf("404 must be returned for ID=%s", testSaleID)
}
}
func TestGetRefund(t *testing.T) {
c, _ := NewClient(testClientID, testSecret, APIBaseSandBox)
c.GetAccessToken()
_, err := c.GetRefund("1")
if err == nil {
t.Errorf("404 must be returned for ID=%s", testSaleID)
t.Errorf("test single payout is not created, error: %v, payout: %v", err, payoutRes)
}
}

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

@ -463,6 +463,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"`