From 14f3dc03a709d4a75438cfbea6c8235bdd08e11a Mon Sep 17 00:00:00 2001 From: Aliaksandr Pliutau Date: Mon, 2 Nov 2015 16:39:07 +0700 Subject: [PATCH] payment test function --- payment.go | 13 +++++++++---- payment_test.go | 23 +++++++++++++++++++++++ types.go | 13 ++----------- 3 files changed, 34 insertions(+), 15 deletions(-) create mode 100644 payment_test.go diff --git a/payment.go b/payment.go index a6ccf95..af8e2a5 100644 --- a/payment.go +++ b/payment.go @@ -2,13 +2,15 @@ package paypalsdk import ( "bytes" + "errors" "fmt" "net/http" + "strconv" ) // CreateDirectPaypalPayment sends request with payment -func (c *Client) CreateDirectPaypalPayment(payment PaypalPaymentRequest) (*PaymentResponse, error) { - buf := bytes.NewBuffer([]byte("")) +func (c *Client) CreateDirectPaypalPayment(amount Amount) (*PaymentResponse, error) { + buf := bytes.NewBuffer([]byte("{\"intent\":\"sale\",\"payer\":{\"payment_method\":\"paypal\"},\"transactions\":[{\"amount\":{\"total\":\"" + strconv.FormatFloat(amount.Total, 'f', 2, 64) + "\",\"currency\":\"" + amount.Currency + "\"}}]}")) req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/payment"), buf) if err != nil { return &PaymentResponse{}, err @@ -16,10 +18,13 @@ func (c *Client) CreateDirectPaypalPayment(payment PaypalPaymentRequest) (*Payme req.SetBasicAuth(c.ClientID, c.Secret) req.Header.Set("Authorization", "Bearer "+c.Token.Token) - req.Header.Set("Content-type", "application/x-www-form-urlencoded") p := PaymentResponse{} err = c.Send(req, &p) - return &p, nil + if p.ID == "" { + return &p, errors.New("Unable to create payment with this access token") + } + + return &p, err } diff --git a/payment_test.go b/payment_test.go new file mode 100644 index 0000000..bb22717 --- /dev/null +++ b/payment_test.go @@ -0,0 +1,23 @@ +package paypalsdk + +import ( + "testing" +) + +func TestCreateDirectPaypalPayment(t *testing.T) { + c, _ := NewClient("clid", "secret", APIBaseSandBox) + c.Token = &TokenResponse{ + Token: "invalidtoken", + } + + amount := Amount{ + Total: 15.1111, + Currency: "USD", + } + + _, err := c.CreateDirectPaypalPayment(amount) + + if err == nil { + t.Errorf("Error must be returned for invalid token") + } +} diff --git a/types.go b/types.go index 0bcd75b..ab4d2ac 100644 --- a/types.go +++ b/types.go @@ -47,24 +47,15 @@ type ( Issue string `json:"issue"` } - // PaypalPaymentRequest - All info about paypal type payment - PaypalPaymentRequest struct { - Transactions []Transaction - } - // PaymentResponse structure PaymentResponse struct { - } - - // Transaction element - Transaction struct { - Amount Amount + ID string `json:"id"` } // Amount to pay Amount struct { Currency string - Total float32 + Total float64 } )