Sale functions

This commit is contained in:
Aliaksandr Pliutau 2015-12-17 10:56:49 +07:00
parent 4e5a04903e
commit 27411f215f
5 changed files with 132 additions and 5 deletions

View File

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

View File

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

42
sale.go Normal file
View File

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

28
sale_test.go Normal file
View File

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

View File

@ -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