From 27411f215fcb3143709bc47f66643ddbf391b238 Mon Sep 17 00:00:00 2001 From: Aliaksandr Pliutau Date: Thu, 17 Dec 2015 10:56:49 +0700 Subject: [PATCH] Sale functions --- README.md | 17 +++++++++++++++++ examples/main.go | 21 ++++++++++++++++----- sale.go | 42 ++++++++++++++++++++++++++++++++++++++++++ sale_test.go | 28 ++++++++++++++++++++++++++++ types.go | 29 +++++++++++++++++++++++++++++ 5 files changed, 132 insertions(+), 5 deletions(-) create mode 100644 sale.go create mode 100644 sale_test.go diff --git a/README.md b/README.md index f566436..184d577 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,8 @@ * POST /v1/payments/authorization/**ID**/capture * POST /v1/payments/authorization/**ID**/void * POST /v1/payments/authorization/**ID**/reauthorize + * GET /v1/payments/sale/**ID** + * POST /v1/payments/sale/**ID**/refund #### Create client @@ -129,3 +131,18 @@ auth, err := c.VoidAuthorization("AUTH-1") ```go auth, err := c.ReauthorizeAuthorization("AUTH-1", &paypalsdk.Amount{Total: "200", Currency: "USD"}) ``` + +#### Get Sale by ID + +```go +sale, err := c.GetSale("1") +``` + +#### Refund Sale by ID + +```go +// Full +refund, err := c.RefundSale("1", nil) +// Partial +refund, err := c.RefundSale("1", &paypalsdk.Amount{Total: "100", Currency: "USD"}) +``` diff --git a/examples/main.go b/examples/main.go index 642819a..9763f41 100644 --- a/examples/main.go +++ b/examples/main.go @@ -5,7 +5,6 @@ import ( "strconv" "fmt" - "os" ) func main() { @@ -14,7 +13,6 @@ func main() { fmt.Println("DEBUG: ClientID=" + client.ClientID + " APIBase=" + client.APIBase) } else { fmt.Println("ERROR: " + err.Error()) - os.Exit(1) } token, err := client.GetAccessToken() @@ -22,7 +20,6 @@ func main() { fmt.Println("DEBUG: AccessToken=" + token.Token) } else { fmt.Println("ERROR: " + err.Error()) - os.Exit(1) } payment, err := client.GetPayment("PAY-TEST-123") @@ -33,7 +30,6 @@ func main() { fmt.Println("DEBUG: PaymentsCount=" + strconv.Itoa(len(payments))) } else { fmt.Println("ERROR: " + err.Error()) - os.Exit(1) } p := paypalsdk.Payment{ @@ -69,7 +65,22 @@ func main() { fmt.Println("DEBUG: CreatedPaymentID=" + paymentResponse.Payment.ID) } else { fmt.Println("ERROR: " + err.Error()) - os.Exit(1) + } + fmt.Println("OK") + + sale, err := client.GetSale("1") + if err == nil { + fmt.Println("DEBUG: SaleID=" + sale.ID) + } else { + fmt.Println("ERROR: " + err.Error()) + } + fmt.Println("OK") + + refund, err := client.RefundSale("1", &paypalsdk.Amount{Total: "100", Currency: "USD"}) + if err == nil { + fmt.Println("DEBUG: RefundID=" + refund.ID) + } else { + fmt.Println("ERROR: " + err.Error()) } fmt.Println("OK") } diff --git a/sale.go b/sale.go new file mode 100644 index 0000000..0dfb4a1 --- /dev/null +++ b/sale.go @@ -0,0 +1,42 @@ +package paypalsdk + +import "fmt" + +// GetSale returns a sale by ID +func (c *Client) GetSale(saleID string) (*Sale, error) { + sale := &Sale{} + + req, err := c.NewRequest("GET", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/sale/"+saleID), nil) + if err != nil { + return sale, err + } + + err = c.SendWithAuth(req, sale) + if err != nil { + return sale, err + } + + return sale, nil +} + +// RefundSale refunds a completed payment. +// Amount can be sent to make a partial refund only +func (c *Client) RefundSale(saleID string, a *Amount) (*Refund, error) { + type refundRequest struct { + Amount *Amount `json:"amount"` + } + + refund := &Refund{} + + req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/sale/"+saleID+"/refund"), &refundRequest{Amount: a}) + if err != nil { + return refund, err + } + + err = c.SendWithAuth(req, refund) + if err != nil { + return refund, err + } + + return refund, nil +} diff --git a/sale_test.go b/sale_test.go new file mode 100644 index 0000000..ab29981 --- /dev/null +++ b/sale_test.go @@ -0,0 +1,28 @@ +package paypalsdk + +import "testing" + +func TestGetSale(t *testing.T) { + c, _ := NewClient("clid", "secret", APIBaseSandBox) + c.GetAccessToken() + + _, err := c.GetSale("1") + if err == nil { + t.Errorf("GetSale must be failed") + } +} + +func TestRefundSale(t *testing.T) { + c, _ := NewClient("clid", "secret", APIBaseSandBox) + c.GetAccessToken() + + _, err := c.RefundSale("1", nil) + if err == nil { + t.Errorf("RefundSale must be failed") + } + + _, err = c.RefundSale("1", &Amount{Total: "100", Currency: "USD"}) + if err == nil { + t.Errorf("RefundSale must be failed") + } +} diff --git a/types.go b/types.go index a0529eb..d66d06c 100644 --- a/types.go +++ b/types.go @@ -228,6 +228,35 @@ type ( Links []PaymentLink `json:"links"` State string `json:"state"` } + + // Sale will be returned by GetSale + Sale struct { + ID string `json:"id,omitempty"` + Amount *Amount `json:"amount,omitempty"` + Description string `json:"description,omitempty"` + CreateTime *time.Time `json:"create_time,omitempty"` + State string `json:"state,omitempty"` + ParentPayment string `json:"parent_payment,omitempty"` + UpdateTime *time.Time `json:"update_time,omitempty"` + PaymentMode string `json:"payment_mode,omitempty"` + PendingReason string `json:"pending_reason,omitempty"` + ReasonCode string `json:"reason_code,omitempty"` + ClearingTime string `json:"clearing_time,omitempty"` + ProtectionEligibility string `json:"protection_eligibility,omitempty"` + ProtectionEligibilityType string `json:"protection_eligibility_type,omitempty"` + Links []Links `json:"links,omitempty"` + } + + // Refund will be returned by RefundSale + Refund struct { + ID string `json:"id,omitempty"` + Amount *Amount `json:"amount,omitempty"` + CreateTime *time.Time `json:"create_time,omitempty"` + State string `json:"state,omitempty"` + CaptureID string `json:"capture_id,omitempty"` + ParentPayment string `json:"parent_payment,omitempty"` + UpdateTime *time.Time `json:"update_time,omitempty"` + } ) // Error method implementation for ErrorResponse struct