mirror of
https://github.com/plutov/paypal.git
synced 2025-01-23 10:21:03 +01:00
payment test function
This commit is contained in:
parent
ab760e8561
commit
14f3dc03a7
13
payment.go
13
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
|
||||
}
|
||||
|
|
23
payment_test.go
Normal file
23
payment_test.go
Normal 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")
|
||||
}
|
||||
}
|
13
types.go
13
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
|
||||
}
|
||||
)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user