diff --git a/.gitignore b/.gitignore index daf913b..bf0289d 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ _testmain.go *.exe *.test *.prof +*.project diff --git a/LICENSE b/LICENSE index 5eb7861..a07fdf5 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2014 fundary +Copyright (c) 2015 Lee Benson. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 4e67c69..9ee75ed 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ # Payment REST API Go client - -[![Coverage Status](https://coveralls.io/repos/fundary/paypal/badge.png)](https://coveralls.io/r/fundary/paypal) [![GoDoc](https://godoc.org/github.com/fundary/paypal?status.svg)](https://godoc.org/github.com/fundary/paypal) + [![GoDoc](https://godoc.org/github.com/leebenson/paypal?status.svg)](https://godoc.org/github.com/leebenson/paypal) This is a client for the Paypal REST API ([https://developer.paypal.com/webapps/developer/docs/api/](https://developer.paypal.com/webapps/developer/docs/api/) @@ -13,7 +12,7 @@ This is a client for the Paypal REST API ([https://developer.paypal.com/webapps/ ## Usage ```bash -go get github.com/fundary/paypal +go get github.com/leebenson/paypal ``` Import into your app and start using it: @@ -26,18 +25,18 @@ import ( "log" "os" - "github.com/fundary/paypal" + "github.com/leebenson/paypal" ) func main() { clientID := os.Getenv("PAYPAL_CLIENTID") if clientID == "" { - panic("Paypal clientID is missing") + panic("PayPal clientID is missing") } secret := os.Getenv("PAYPAL_SECRET") if secret == "" { - panic("Paypal secret is missing") + panic("PayPal secret is missing") } client := paypal.NewClient(clientID, secret, paypal.APIBaseLive) diff --git a/paymenttype.go b/paymenttype.go index 5f792c0..7004f82 100644 --- a/paymenttype.go +++ b/paymenttype.go @@ -132,6 +132,7 @@ type ( // Authorization maps to the authorization object Authorization struct { + Id int64 `json:"-"` Amount *Amount `json:"amount,omitempty"` CreateTime *time.Time `json:"create_time,omitempty"` UpdateTime *time.Time `json:"update_time,omitempty"` @@ -147,6 +148,7 @@ type ( // Capture maps to the capture object Capture struct { + Id int64 `json:"-"` Amount *Amount `json:"amount,omitempty"` IsFinalCapture bool `json:"is_final_capture"` CreateTime *time.Time `json:"create_time,omitempty"` @@ -159,6 +161,7 @@ type ( // Details maps to the details object Details struct { + Id int64 `json:"-"` Shipping string `json:"shipping,omitempty"` Subtotal string `json:"subtotal"` Tax string `json:"tax,omitempty"` @@ -216,7 +219,7 @@ type ( // Item maps to item object Item struct { - Quantity string `json:"quantity"` + Quantity int `json:"quantity"` Name string `json:"name"` Price string `json:"price"` Currency string `json:"currency"` @@ -268,15 +271,15 @@ type ( // Payment maps to payment object Payment struct { - Intent PaymentIntent `json:"intent"` - Payer *Payer `json:"payer"` - Transactions []Transaction `json:"transactions"` - RedirectURLs []RedirectURLs `json:"redirect_urls,omitempty"` - ID string `json:"id,omitempty"` - CreateTime *time.Time `json:"create_time,omitempty"` - State PaymentState `json:"state,omitempty"` - UpdateTime *time.Time `json:"update_time,omitempty"` - ExperienceProfileID string `json:"experience_profile_id,omitempty"` + Intent PaymentIntent `json:"intent"` + Payer *Payer `json:"payer"` + Transactions []Transaction `json:"transactions"` + RedirectURLs *RedirectURLs `json:"redirect_urls,omitempty"` + ID string `json:"id,omitempty"` + CreateTime *time.Time `json:"create_time,omitempty"` + State PaymentState `json:"state,omitempty"` + UpdateTime *time.Time `json:"update_time,omitempty"` + ExperienceProfileID string `json:"experience_profile_id,omitempty"` } // PaymentExecution maps to payment_execution object diff --git a/paypal.go b/paypal.go index 871b879..1454a96 100644 --- a/paypal.go +++ b/paypal.go @@ -36,15 +36,15 @@ type ( // HTTP response that caused this error Response *http.Response `json:"-"` - Name string `json:"name"` - DebugID string `json:"debug_id"` - Message string `json:"message"` - InformationLink string `json:"information_link"` - Details ErrorDetails `json:"details"` + Name string `json:"name"` + DebugID string `json:"debug_id"` + Message string `json:"message"` + InformationLink string `json:"information_link"` + Details []ErrorDetail `json:"details"` } // ErrorDetails map to error_details object - ErrorDetails struct { + ErrorDetail struct { Field string `json:"field"` Issue string `json:"issue"` } @@ -134,6 +134,7 @@ func (c *Client) Send(req *http.Request, v interface{}) error { if c := resp.StatusCode; c < 200 || c > 299 { errResp := &ErrorResponse{Response: resp} data, err := ioutil.ReadAll(resp.Body) + if err == nil && len(data) > 0 { json.Unmarshal(data, errResp) } diff --git a/refund.go b/refund.go index b3c887d..1119d5f 100644 --- a/refund.go +++ b/refund.go @@ -4,7 +4,7 @@ import "fmt" // GetRefund returns a refund by ID func (c *Client) GetRefund(refundID string) (*Refund, error) { - req, err := NewRequest("GET", fmt.Sprintf("%s/refund/%s", c.APIBase, refundID), nil) + req, err := NewRequest("GET", fmt.Sprintf("%s/payments/refund/%s", c.APIBase, refundID), nil) if err != nil { return nil, err } diff --git a/vault.go b/vault.go new file mode 100644 index 0000000..76581b2 --- /dev/null +++ b/vault.go @@ -0,0 +1,44 @@ +package paypal + +import ( + "fmt" + "time" +) + +// https://developer.paypal.com/docs/api/#vault + +type ( + // VaultRequest maps to vault_request object + VaultRequest struct { + CreditCard + MerchantID string `json:"merchant_id,omitempty"` + ExternalCardID string `json:"external_card_id,omitempty"` + } + + // VaultResponse maps to vault_response object + VaultResponse struct { + VaultRequest + CreateTime *time.Time `json: "create_time"` + UpdateTime *time.Time `json: "update_time"` + State string `json: "state"` + ValidUntil string `json: "valid_until"` + Links []Links `json:"links"` + } +) + +// StoreInVault will store credit card details with PayPal. +func (c *Client) StoreInVault(cc VaultRequest) (*VaultResponse, error) { + + req, err := NewRequest("POST", fmt.Sprintf("%s/vault/credit-cards", c.APIBase), cc) + + if err != nil { + return nil, err + } + v := &VaultResponse{} + + err = c.SendWithAuth(req, v) + if err != nil { + return nil, err + } + return v, nil +}