diff --git a/auth.go b/auth.go index 5df876c..1b4bdf0 100644 --- a/auth.go +++ b/auth.go @@ -7,6 +7,8 @@ import ( ) // GetAccessToken returns struct of TokenResponse +// No need to call SetAccessToken to apply new access token for current Client +// Endpoint: POST /v1/oauth2/token func (c *Client) GetAccessToken() (*TokenResponse, error) { buf := bytes.NewBuffer([]byte("grant_type=client_credentials")) req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/oauth2/token"), buf) @@ -29,6 +31,7 @@ func (c *Client) GetAccessToken() (*TokenResponse, error) { } // GetAuthorization returns an authorization by ID +// Endpoint: GET /v1/payments/authorization/ID func (c *Client) GetAuthorization(authID string) (*Authorization, error) { buf := bytes.NewBuffer([]byte("")) req, err := http.NewRequest("GET", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/authorization/"+authID), buf) @@ -48,6 +51,7 @@ func (c *Client) GetAuthorization(authID string) (*Authorization, error) { // CaptureAuthorization captures and process an existing authorization. // To use this method, the original payment must have Intent set to "authorize" +// Endpoint: POST /v1/payments/authorization/ID/capture func (c *Client) CaptureAuthorization(authID string, a *Amount, isFinalCapture bool) (*Capture, error) { isFinalStr := "false" if isFinalCapture { @@ -70,6 +74,7 @@ func (c *Client) CaptureAuthorization(authID string, a *Amount, isFinalCapture b } // VoidAuthorization voids a previously authorized payment +// Endpoint: POST /v1/payments/authorization/ID/void func (c *Client) VoidAuthorization(authID string) (*Authorization, error) { buf := bytes.NewBuffer([]byte("")) req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/authorization/"+authID+"/void"), buf) @@ -89,6 +94,7 @@ func (c *Client) VoidAuthorization(authID string) (*Authorization, error) { // ReauthorizeAuthorization reauthorize a Paypal account payment. // PayPal recommends to reauthorize payment after ~3 days +// Endpoint: POST /v1/payments/authorization/ID/reauthorize func (c *Client) ReauthorizeAuthorization(authID string, a *Amount) (*Authorization, error) { buf := bytes.NewBuffer([]byte("{\"amount\":{\"currency\":\"" + a.Currency + "\",\"total\":\"" + a.Total + "\"}}")) req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/authorization/"+authID+"/reauthorize"), buf) @@ -104,5 +110,4 @@ func (c *Client) ReauthorizeAuthorization(authID string, a *Amount) (*Authorizat } return auth, nil - } diff --git a/before-commit.sh b/before-commit.sh new file mode 100755 index 0000000..afe577d --- /dev/null +++ b/before-commit.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +go fmt ./... +golint ./... +go vet ./... +go test -v ./... diff --git a/client.go b/client.go index 35259ab..515edf6 100644 --- a/client.go +++ b/client.go @@ -12,6 +12,7 @@ import ( ) // NewClient returns new Client struct +// APIBase is a base API URL, for testing you can use paypalsdk.APIBaseSandBox func NewClient(clientID string, secret string, APIBase string) (*Client, error) { if clientID == "" || secret == "" || APIBase == "" { return &Client{}, errors.New("ClientID, Secret and APIBase are required to create a Client") @@ -27,7 +28,8 @@ func NewClient(clientID string, secret string, APIBase string) (*Client, error) }, nil } -// SetLogFile func +// SetLogFile will set/change a full path to a log file +// If log file is set paypalsdk will log all requests and responses to this file func (c *Client) SetLogFile(filepath string) error { c.LogFile = filepath @@ -115,6 +117,7 @@ func (c *Client) NewRequest(method, url string, payload interface{}) (*http.Requ return http.NewRequest(method, url, buf) } +// log will dump request and response to the log file func (c *Client) log(req *http.Request, resp *http.Response) { if c.LogFile != "" { os.OpenFile(c.LogFile, os.O_CREATE, 0755) diff --git a/doc.go b/doc.go index 41a04f9..4170a81 100644 --- a/doc.go +++ b/doc.go @@ -1,6 +1,6 @@ /* Package paypalsdk provides a warepper to PayPal API (https://developer.paypal.com/webapps/developer/docs/api/). -The first thing you do is to create a Client. +The first thing you do is to create a Client (you can select API base URL using paypalsdk contsnts). c, err := paypalsdk.NewClient("clientID", "secretID", paypalsdk.APIBaseSandBox) Then you can get an access token from PayPal: accessToken, err := c.GetAccessToken() diff --git a/order.go b/order.go index 9ed6dfe..5c64e97 100644 --- a/order.go +++ b/order.go @@ -2,7 +2,8 @@ package paypalsdk import "fmt" -// GetOrder retreives order +// GetOrder retreives order by ID +// Endpoint: GET /v1/payments/orders/ID func (c *Client) GetOrder(orderID string) (*Order, error) { order := &Order{} @@ -19,7 +20,8 @@ func (c *Client) GetOrder(orderID string) (*Order, error) { return order, nil } -// AuthorizeOrder POST /v1/payments/orders//authorize +// AuthorizeOrder - Use this call to authorize an order. +// Endpoint: POST /v1/payments/orders/ID/authorize func (c *Client) AuthorizeOrder(orderID string, amount *Amount) (*Authorization, error) { type authRequest struct { Amount *Amount `json:"amount"` @@ -40,7 +42,8 @@ func (c *Client) AuthorizeOrder(orderID string, amount *Amount) (*Authorization, return auth, nil } -// CaptureOrder POST /v1/payments/orders//capture +// CaptureOrder - Use this call to capture a payment on an order. To use this call, an original payment call must specify an intent of order. +// Endpoint: POST /v1/payments/orders/ID/capture func (c *Client) CaptureOrder(orderID string, amount *Amount, isFinalCapture bool, currency *Currency) (*Capture, error) { type captureRequest struct { Amount *Amount `json:"amount"` @@ -63,7 +66,9 @@ func (c *Client) CaptureOrder(orderID string, amount *Amount, isFinalCapture boo return capture, nil } -// VoidOrder POST /v1/payments/orders//do-void +// VoidOrder - Use this call to void an existing order. +// Note: An order cannot be voided if payment has already been partially or fully captured. +// Endpoint: POST /v1/payments/orders/ID/do-void func (c *Client) VoidOrder(orderID string) (*Order, error) { order := &Order{} diff --git a/payment.go b/payment.go index a667244..3a8dbe8 100644 --- a/payment.go +++ b/payment.go @@ -12,14 +12,15 @@ type ListPaymentsResp struct { Payments []Payment `json:"payments"` } -// CreatePaymentResp returned by CreatePayment +// CreatePaymentResp contains Payment Info and Links slice type CreatePaymentResp struct { *Payment Links []Links `json:"links"` } -// CreateDirectPaypalPayment sends request with payment +// CreateDirectPaypalPayment sends request to create a payment with payment_method=paypal // CreatePayment is more common function for any kind of payment +// Endpoint: POST /v1/payments/payment func (c *Client) CreateDirectPaypalPayment(amount Amount, redirectURI string, cancelURI string, description string) (*PaymentResponse, error) { buf := bytes.NewBuffer([]byte("{\"intent\":\"sale\",\"payer\":{\"payment_method\":\"paypal\"}," + "\"transactions\":[{\"amount\":{\"total\":\"" + amount.Total + @@ -47,6 +48,8 @@ func (c *Client) CreateDirectPaypalPayment(amount Amount, redirectURI string, ca } // CreatePayment creates a payment in Paypal +// Depending on the payment_method and the funding_instrument, you can use the payment resource for direct credit card payments, stored credit card payments, or PayPal account payments. +// Endpoint: POST /v1/payments/payment func (c *Client) CreatePayment(p Payment) (*CreatePaymentResp, error) { req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/payment"), p) if err != nil { @@ -63,7 +66,8 @@ func (c *Client) CreatePayment(p Payment) (*CreatePaymentResp, error) { return response, nil } -// ExecuteApprovedPayment executes approved payment +// ExecuteApprovedPayment - Use this call to execute (complete) a PayPal payment that has been approved by the payer. You can optionally update transaction information when executing the payment by passing in one or more transactions. +// Endpoint: POST /v1/payments/payment/paymentID/execute func (c *Client) ExecuteApprovedPayment(paymentID string, payerID string) (*ExecuteResponse, error) { buf := bytes.NewBuffer([]byte("{\"payer_id\":\"" + payerID + "\"}")) req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/payment/"+paymentID+"/execute"), buf) @@ -88,6 +92,7 @@ func (c *Client) ExecuteApprovedPayment(paymentID string, payerID string) (*Exec } // GetPayment gets a payment from PayPal +// Endpoint: GET /v1/payments/payment/ID func (c *Client) GetPayment(paymentID string) (*Payment, error) { p := Payment{} @@ -109,6 +114,7 @@ func (c *Client) GetPayment(paymentID string) (*Payment, error) { } // GetPayments retrieve payments resources from Paypal +// Endpoint: GET /v1/payments/payment/ func (c *Client) GetPayments() ([]Payment, error) { var p ListPaymentsResp diff --git a/sale.go b/sale.go index c16f114..1fe6e0e 100644 --- a/sale.go +++ b/sale.go @@ -3,6 +3,9 @@ package paypalsdk import "fmt" // GetSale returns a sale by ID +// Use this call to get details about a sale transaction. +// Note: This call returns only the sales that were created via the REST API. +// Endpoint: GET /v1/payments/sale/ID func (c *Client) GetSale(saleID string) (*Sale, error) { sale := &Sale{} @@ -20,7 +23,8 @@ func (c *Client) GetSale(saleID string) (*Sale, error) { } // RefundSale refunds a completed payment. -// Amount can be sent to make a partial refund only +// Use this call to refund a completed payment. Provide the sale_id in the URI and an empty JSON payload for a full refund. For partial refunds, you can include an amount. +// Endpoint: POST /v1/payments/sale/ID/refund func (c *Client) RefundSale(saleID string, a *Amount) (*Refund, error) { type refundRequest struct { Amount *Amount `json:"amount"` @@ -42,6 +46,8 @@ func (c *Client) RefundSale(saleID string, a *Amount) (*Refund, error) { } // GetRefund by ID +// Use it to look up details of a specific refund on direct and captured payments. +// Endpoint: GET /v1/payments/refund/ID func (c *Client) GetRefund(refundID string) (*Refund, error) { refund := &Refund{} diff --git a/types.go b/types.go index 2614a31..212683f 100644 --- a/types.go +++ b/types.go @@ -15,7 +15,7 @@ const ( ) type ( - // Address struct + // Address https://developer.paypal.com/webapps/developer/docs/api/#address-object Address struct { Line1 string `json:"line1"` Line2 string `json:"line2,omitempty"` @@ -26,13 +26,13 @@ type ( Phone string `json:"phone,omitempty"` } - // Amount to pay + // Amount https://developer.paypal.com/webapps/developer/docs/api/#amount-object Amount struct { Currency string `json:"currency"` Total string `json:"total"` } - // Authorization represetns PayPal authorization + // Authorization rhttps://developer.paypal.com/webapps/developer/docs/api/#authorization-object Authorization struct { Amount *Amount `json:"amount,omitempty"` CreateTime *time.Time `json:"create_time,omitempty"` @@ -47,7 +47,7 @@ type ( ProtectionEligibilityType string `json:"protection_eligibility_type,omitempty"` } - // Capture struct + // Capture https://developer.paypal.com/webapps/developer/docs/api/#capture-object Capture struct { Amount *Amount `json:"amount,omitempty"` IsFinalCapture bool `json:"is_final_capture"` @@ -69,7 +69,7 @@ type ( Token *TokenResponse } - // CreditCard struct + // CreditCard https://developer.paypal.com/webapps/developer/docs/api/#creditcard-object CreditCard struct { ID string `json:"id,omitempty"` PayerID string `json:"payer_id,omitempty"` @@ -85,7 +85,7 @@ type ( ValidUntil string `json:"valid_until,omitempty"` } - // CreditCardToken struct + // CreditCardToken https://developer.paypal.com/webapps/developer/docs/api/#creditcardtoken-object CreditCardToken struct { CreditCardID string `json:"credit_card_id"` PayerID string `json:"payer_id,omitempty"` @@ -100,7 +100,7 @@ type ( Value string `json:"value,omitempty"` } - // ErrorResponse is used when a response has errors + // ErrorResponse https://developer.paypal.com/webapps/developer/docs/api/#error-object ErrorResponse struct { Response *http.Response `json:"-"` Name string `json:"name"` @@ -110,7 +110,7 @@ type ( Details []ErrorDetail `json:"details"` } - // ErrorDetail map to error_details object + // ErrorDetail https://developer.paypal.com/webapps/developer/docs/api/#errordetails-object ErrorDetail struct { Field string `json:"field"` Issue string `json:"issue"` @@ -123,13 +123,13 @@ type ( State string `json:"state"` } - // FundingInstrument struct + // FundingInstrument https://developer.paypal.com/webapps/developer/docs/api/#fundinginstrument-object FundingInstrument struct { CreditCard *CreditCard `json:"credit_card,omitempty"` CreditCardToken *CreditCardToken `json:"credit_card_token,omitempty"` } - // Item struct + // Item https://developer.paypal.com/webapps/developer/docs/api/#item-object Item struct { Quantity int `json:"quantity"` Name string `json:"name"` @@ -146,7 +146,7 @@ type ( ShippingAddress *ShippingAddress `json:"shipping_address,omitempty"` } - // Links struct + // Links https://developer.paypal.com/webapps/developer/docs/api/#itemlist-object Links struct { Href string `json:"href"` Rel string `json:"rel"` @@ -166,7 +166,7 @@ type ( Links []Links `json:"links,omitempty"` } - // Payer struct + // Payer https://developer.paypal.com/webapps/developer/docs/api/#payer-object Payer struct { PaymentMethod string `json:"payment_method"` FundingInstruments []FundingInstrument `json:"funding_instruments,omitempty"` @@ -174,7 +174,7 @@ type ( Status string `json:"payer_status,omitempty"` } - // PayerInfo struct + // PayerInfo https://developer.paypal.com/webapps/developer/docs/api/#itemlist-object PayerInfo struct { Email string `json:"email,omitempty"` FirstName string `json:"first_name,omitempty"` @@ -186,7 +186,7 @@ type ( TaxID string `json:"tax_id,omitempty"` } - // Payment struct + // Payment https://developer.paypal.com/webapps/developer/docs/api/#payment-object Payment struct { Intent string `json:"intent"` Payer *Payer `json:"payer"` @@ -199,7 +199,7 @@ type ( ExperienceProfileID string `json:"experience_profile_id,omitempty"` } - // PaymentLink structure + // PaymentLink https://developer.paypal.com/webapps/developer/docs/api/#paymentlink-object PaymentLink struct { Href string `json:"href"` Rel string `json:"rel"` @@ -211,13 +211,13 @@ type ( Links []PaymentLink `json:"links"` } - // RedirectURLs for redirect_urls + // RedirectURLs https://developer.paypal.com/webapps/developer/docs/api/#redirecturls-object RedirectURLs struct { ReturnURL string `json:"return_url,omitempty"` CancelURL string `json:"cancel_url,omitempty"` } - // Refund will be returned by RefundSale + // Refund https://developer.paypal.com/webapps/developer/docs/api/#refund-object Refund struct { ID string `json:"id,omitempty"` Amount *Amount `json:"amount,omitempty"` @@ -228,7 +228,7 @@ type ( UpdateTime *time.Time `json:"update_time,omitempty"` } - // Sale will be returned by GetSale + // Sale https://developer.paypal.com/webapps/developer/docs/api/#sale-object Sale struct { ID string `json:"id,omitempty"` Amount *Amount `json:"amount,omitempty"` @@ -246,7 +246,7 @@ type ( Links []Links `json:"links,omitempty"` } - // ShippingAddress for shipping_address + // ShippingAddress https://developer.paypal.com/webapps/developer/docs/api/#shippingaddredd-object ShippingAddress struct { RecipientName string `json:"recipient_name,omitempty"` Type string `json:"type,omitempty"` @@ -266,7 +266,7 @@ type ( Type string `json:"token_type"` } - // Transaction is for transaction object + // Transaction https://developer.paypal.com/webapps/developer/docs/api/#transaction-object Transaction struct { Amount *Amount `json:"amount"` Description string `json:"description,omitempty"`