payment test function

This commit is contained in:
Aliaksandr Pliutau 2015-11-02 16:39:07 +07:00
parent ab760e8561
commit 14f3dc03a7
3 changed files with 34 additions and 15 deletions

View File

@ -2,13 +2,15 @@ package paypalsdk
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"net/http" "net/http"
"strconv"
) )
// CreateDirectPaypalPayment sends request with payment // CreateDirectPaypalPayment sends request with payment
func (c *Client) CreateDirectPaypalPayment(payment PaypalPaymentRequest) (*PaymentResponse, error) { func (c *Client) CreateDirectPaypalPayment(amount Amount) (*PaymentResponse, error) {
buf := bytes.NewBuffer([]byte("")) 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) req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/payment"), buf)
if err != nil { if err != nil {
return &PaymentResponse{}, err return &PaymentResponse{}, err
@ -16,10 +18,13 @@ func (c *Client) CreateDirectPaypalPayment(payment PaypalPaymentRequest) (*Payme
req.SetBasicAuth(c.ClientID, c.Secret) req.SetBasicAuth(c.ClientID, c.Secret)
req.Header.Set("Authorization", "Bearer "+c.Token.Token) req.Header.Set("Authorization", "Bearer "+c.Token.Token)
req.Header.Set("Content-type", "application/x-www-form-urlencoded")
p := PaymentResponse{} p := PaymentResponse{}
err = c.Send(req, &p) 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
} }

23
payment_test.go Normal file
View File

@ -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")
}
}

View File

@ -47,24 +47,15 @@ type (
Issue string `json:"issue"` Issue string `json:"issue"`
} }
// PaypalPaymentRequest - All info about paypal type payment
PaypalPaymentRequest struct {
Transactions []Transaction
}
// PaymentResponse structure // PaymentResponse structure
PaymentResponse struct { PaymentResponse struct {
} ID string `json:"id"`
// Transaction element
Transaction struct {
Amount Amount
} }
// Amount to pay // Amount to pay
Amount struct { Amount struct {
Currency string Currency string
Total float32 Total float64
} }
) )