godoc comments

This commit is contained in:
Aliaksandr Pliutau 2015-12-29 16:21:11 +07:00
parent 74d2d9dcde
commit 3f5e093801
8 changed files with 62 additions and 31 deletions

View File

@ -7,6 +7,8 @@ import (
) )
// GetAccessToken returns struct of TokenResponse // 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) { func (c *Client) GetAccessToken() (*TokenResponse, error) {
buf := bytes.NewBuffer([]byte("grant_type=client_credentials")) buf := bytes.NewBuffer([]byte("grant_type=client_credentials"))
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/oauth2/token"), buf) 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 // GetAuthorization returns an authorization by ID
// Endpoint: GET /v1/payments/authorization/ID
func (c *Client) GetAuthorization(authID string) (*Authorization, error) { func (c *Client) GetAuthorization(authID string) (*Authorization, error) {
buf := bytes.NewBuffer([]byte("")) buf := bytes.NewBuffer([]byte(""))
req, err := http.NewRequest("GET", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/authorization/"+authID), buf) 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. // CaptureAuthorization captures and process an existing authorization.
// To use this method, the original payment must have Intent set to "authorize" // 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) { func (c *Client) CaptureAuthorization(authID string, a *Amount, isFinalCapture bool) (*Capture, error) {
isFinalStr := "false" isFinalStr := "false"
if isFinalCapture { if isFinalCapture {
@ -70,6 +74,7 @@ func (c *Client) CaptureAuthorization(authID string, a *Amount, isFinalCapture b
} }
// VoidAuthorization voids a previously authorized payment // VoidAuthorization voids a previously authorized payment
// Endpoint: POST /v1/payments/authorization/ID/void
func (c *Client) VoidAuthorization(authID string) (*Authorization, error) { func (c *Client) VoidAuthorization(authID string) (*Authorization, error) {
buf := bytes.NewBuffer([]byte("")) buf := bytes.NewBuffer([]byte(""))
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/authorization/"+authID+"/void"), buf) 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. // ReauthorizeAuthorization reauthorize a Paypal account payment.
// PayPal recommends to reauthorize payment after ~3 days // 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) { func (c *Client) ReauthorizeAuthorization(authID string, a *Amount) (*Authorization, error) {
buf := bytes.NewBuffer([]byte("{\"amount\":{\"currency\":\"" + a.Currency + "\",\"total\":\"" + a.Total + "\"}}")) 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) 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 return auth, nil
} }

6
before-commit.sh Executable file
View File

@ -0,0 +1,6 @@
#!/bin/bash
go fmt ./...
golint ./...
go vet ./...
go test -v ./...

View File

@ -12,6 +12,7 @@ import (
) )
// NewClient returns new Client struct // 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) { func NewClient(clientID string, secret string, APIBase string) (*Client, error) {
if clientID == "" || secret == "" || APIBase == "" { if clientID == "" || secret == "" || APIBase == "" {
return &Client{}, errors.New("ClientID, Secret and APIBase are required to create a Client") 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 }, 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 { func (c *Client) SetLogFile(filepath string) error {
c.LogFile = filepath c.LogFile = filepath
@ -115,6 +117,7 @@ func (c *Client) NewRequest(method, url string, payload interface{}) (*http.Requ
return http.NewRequest(method, url, buf) 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) { func (c *Client) log(req *http.Request, resp *http.Response) {
if c.LogFile != "" { if c.LogFile != "" {
os.OpenFile(c.LogFile, os.O_CREATE, 0755) os.OpenFile(c.LogFile, os.O_CREATE, 0755)

2
doc.go
View File

@ -1,6 +1,6 @@
/* /*
Package paypalsdk provides a warepper to PayPal API (https://developer.paypal.com/webapps/developer/docs/api/). 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) c, err := paypalsdk.NewClient("clientID", "secretID", paypalsdk.APIBaseSandBox)
Then you can get an access token from PayPal: Then you can get an access token from PayPal:
accessToken, err := c.GetAccessToken() accessToken, err := c.GetAccessToken()

View File

@ -2,7 +2,8 @@ package paypalsdk
import "fmt" import "fmt"
// GetOrder retreives order // GetOrder retreives order by ID
// Endpoint: GET /v1/payments/orders/ID
func (c *Client) GetOrder(orderID string) (*Order, error) { func (c *Client) GetOrder(orderID string) (*Order, error) {
order := &Order{} order := &Order{}
@ -19,7 +20,8 @@ func (c *Client) GetOrder(orderID string) (*Order, error) {
return order, nil return order, nil
} }
// AuthorizeOrder POST /v1/payments/orders/<Order-Id>/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) { func (c *Client) AuthorizeOrder(orderID string, amount *Amount) (*Authorization, error) {
type authRequest struct { type authRequest struct {
Amount *Amount `json:"amount"` Amount *Amount `json:"amount"`
@ -40,7 +42,8 @@ func (c *Client) AuthorizeOrder(orderID string, amount *Amount) (*Authorization,
return auth, nil return auth, nil
} }
// CaptureOrder POST /v1/payments/orders/<Order-Id>/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) { func (c *Client) CaptureOrder(orderID string, amount *Amount, isFinalCapture bool, currency *Currency) (*Capture, error) {
type captureRequest struct { type captureRequest struct {
Amount *Amount `json:"amount"` Amount *Amount `json:"amount"`
@ -63,7 +66,9 @@ func (c *Client) CaptureOrder(orderID string, amount *Amount, isFinalCapture boo
return capture, nil return capture, nil
} }
// VoidOrder POST /v1/payments/orders/<Order-Id>/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) { func (c *Client) VoidOrder(orderID string) (*Order, error) {
order := &Order{} order := &Order{}

View File

@ -12,14 +12,15 @@ type ListPaymentsResp struct {
Payments []Payment `json:"payments"` Payments []Payment `json:"payments"`
} }
// CreatePaymentResp returned by CreatePayment // CreatePaymentResp contains Payment Info and Links slice
type CreatePaymentResp struct { type CreatePaymentResp struct {
*Payment *Payment
Links []Links `json:"links"` 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 // 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) { func (c *Client) CreateDirectPaypalPayment(amount Amount, redirectURI string, cancelURI string, description string) (*PaymentResponse, error) {
buf := bytes.NewBuffer([]byte("{\"intent\":\"sale\",\"payer\":{\"payment_method\":\"paypal\"}," + buf := bytes.NewBuffer([]byte("{\"intent\":\"sale\",\"payer\":{\"payment_method\":\"paypal\"}," +
"\"transactions\":[{\"amount\":{\"total\":\"" + amount.Total + "\"transactions\":[{\"amount\":{\"total\":\"" + amount.Total +
@ -47,6 +48,8 @@ func (c *Client) CreateDirectPaypalPayment(amount Amount, redirectURI string, ca
} }
// CreatePayment creates a payment in Paypal // 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) { func (c *Client) CreatePayment(p Payment) (*CreatePaymentResp, error) {
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/payment"), p) req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/payment"), p)
if err != nil { if err != nil {
@ -63,7 +66,8 @@ func (c *Client) CreatePayment(p Payment) (*CreatePaymentResp, error) {
return response, nil 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) { func (c *Client) ExecuteApprovedPayment(paymentID string, payerID string) (*ExecuteResponse, error) {
buf := bytes.NewBuffer([]byte("{\"payer_id\":\"" + payerID + "\"}")) buf := bytes.NewBuffer([]byte("{\"payer_id\":\"" + payerID + "\"}"))
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/payment/"+paymentID+"/execute"), buf) 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 // GetPayment gets a payment from PayPal
// Endpoint: GET /v1/payments/payment/ID
func (c *Client) GetPayment(paymentID string) (*Payment, error) { func (c *Client) GetPayment(paymentID string) (*Payment, error) {
p := Payment{} p := Payment{}
@ -109,6 +114,7 @@ func (c *Client) GetPayment(paymentID string) (*Payment, error) {
} }
// GetPayments retrieve payments resources from Paypal // GetPayments retrieve payments resources from Paypal
// Endpoint: GET /v1/payments/payment/
func (c *Client) GetPayments() ([]Payment, error) { func (c *Client) GetPayments() ([]Payment, error) {
var p ListPaymentsResp var p ListPaymentsResp

View File

@ -3,6 +3,9 @@ package paypalsdk
import "fmt" import "fmt"
// GetSale returns a sale by ID // 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) { func (c *Client) GetSale(saleID string) (*Sale, error) {
sale := &Sale{} sale := &Sale{}
@ -20,7 +23,8 @@ func (c *Client) GetSale(saleID string) (*Sale, error) {
} }
// RefundSale refunds a completed payment. // 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) { func (c *Client) RefundSale(saleID string, a *Amount) (*Refund, error) {
type refundRequest struct { type refundRequest struct {
Amount *Amount `json:"amount"` Amount *Amount `json:"amount"`
@ -42,6 +46,8 @@ func (c *Client) RefundSale(saleID string, a *Amount) (*Refund, error) {
} }
// GetRefund by ID // 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) { func (c *Client) GetRefund(refundID string) (*Refund, error) {
refund := &Refund{} refund := &Refund{}

View File

@ -15,7 +15,7 @@ const (
) )
type ( type (
// Address struct // Address https://developer.paypal.com/webapps/developer/docs/api/#address-object
Address struct { Address struct {
Line1 string `json:"line1"` Line1 string `json:"line1"`
Line2 string `json:"line2,omitempty"` Line2 string `json:"line2,omitempty"`
@ -26,13 +26,13 @@ type (
Phone string `json:"phone,omitempty"` Phone string `json:"phone,omitempty"`
} }
// Amount to pay // Amount https://developer.paypal.com/webapps/developer/docs/api/#amount-object
Amount struct { Amount struct {
Currency string `json:"currency"` Currency string `json:"currency"`
Total string `json:"total"` Total string `json:"total"`
} }
// Authorization represetns PayPal authorization // Authorization rhttps://developer.paypal.com/webapps/developer/docs/api/#authorization-object
Authorization struct { Authorization struct {
Amount *Amount `json:"amount,omitempty"` Amount *Amount `json:"amount,omitempty"`
CreateTime *time.Time `json:"create_time,omitempty"` CreateTime *time.Time `json:"create_time,omitempty"`
@ -47,7 +47,7 @@ type (
ProtectionEligibilityType string `json:"protection_eligibility_type,omitempty"` ProtectionEligibilityType string `json:"protection_eligibility_type,omitempty"`
} }
// Capture struct // Capture https://developer.paypal.com/webapps/developer/docs/api/#capture-object
Capture struct { Capture struct {
Amount *Amount `json:"amount,omitempty"` Amount *Amount `json:"amount,omitempty"`
IsFinalCapture bool `json:"is_final_capture"` IsFinalCapture bool `json:"is_final_capture"`
@ -69,7 +69,7 @@ type (
Token *TokenResponse Token *TokenResponse
} }
// CreditCard struct // CreditCard https://developer.paypal.com/webapps/developer/docs/api/#creditcard-object
CreditCard struct { CreditCard struct {
ID string `json:"id,omitempty"` ID string `json:"id,omitempty"`
PayerID string `json:"payer_id,omitempty"` PayerID string `json:"payer_id,omitempty"`
@ -85,7 +85,7 @@ type (
ValidUntil string `json:"valid_until,omitempty"` ValidUntil string `json:"valid_until,omitempty"`
} }
// CreditCardToken struct // CreditCardToken https://developer.paypal.com/webapps/developer/docs/api/#creditcardtoken-object
CreditCardToken struct { CreditCardToken struct {
CreditCardID string `json:"credit_card_id"` CreditCardID string `json:"credit_card_id"`
PayerID string `json:"payer_id,omitempty"` PayerID string `json:"payer_id,omitempty"`
@ -100,7 +100,7 @@ type (
Value string `json:"value,omitempty"` 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 { ErrorResponse struct {
Response *http.Response `json:"-"` Response *http.Response `json:"-"`
Name string `json:"name"` Name string `json:"name"`
@ -110,7 +110,7 @@ type (
Details []ErrorDetail `json:"details"` Details []ErrorDetail `json:"details"`
} }
// ErrorDetail map to error_details object // ErrorDetail https://developer.paypal.com/webapps/developer/docs/api/#errordetails-object
ErrorDetail struct { ErrorDetail struct {
Field string `json:"field"` Field string `json:"field"`
Issue string `json:"issue"` Issue string `json:"issue"`
@ -123,13 +123,13 @@ type (
State string `json:"state"` State string `json:"state"`
} }
// FundingInstrument struct // FundingInstrument https://developer.paypal.com/webapps/developer/docs/api/#fundinginstrument-object
FundingInstrument struct { FundingInstrument struct {
CreditCard *CreditCard `json:"credit_card,omitempty"` CreditCard *CreditCard `json:"credit_card,omitempty"`
CreditCardToken *CreditCardToken `json:"credit_card_token,omitempty"` CreditCardToken *CreditCardToken `json:"credit_card_token,omitempty"`
} }
// Item struct // Item https://developer.paypal.com/webapps/developer/docs/api/#item-object
Item struct { Item struct {
Quantity int `json:"quantity"` Quantity int `json:"quantity"`
Name string `json:"name"` Name string `json:"name"`
@ -146,7 +146,7 @@ type (
ShippingAddress *ShippingAddress `json:"shipping_address,omitempty"` ShippingAddress *ShippingAddress `json:"shipping_address,omitempty"`
} }
// Links struct // Links https://developer.paypal.com/webapps/developer/docs/api/#itemlist-object
Links struct { Links struct {
Href string `json:"href"` Href string `json:"href"`
Rel string `json:"rel"` Rel string `json:"rel"`
@ -166,7 +166,7 @@ type (
Links []Links `json:"links,omitempty"` Links []Links `json:"links,omitempty"`
} }
// Payer struct // Payer https://developer.paypal.com/webapps/developer/docs/api/#payer-object
Payer struct { Payer struct {
PaymentMethod string `json:"payment_method"` PaymentMethod string `json:"payment_method"`
FundingInstruments []FundingInstrument `json:"funding_instruments,omitempty"` FundingInstruments []FundingInstrument `json:"funding_instruments,omitempty"`
@ -174,7 +174,7 @@ type (
Status string `json:"payer_status,omitempty"` Status string `json:"payer_status,omitempty"`
} }
// PayerInfo struct // PayerInfo https://developer.paypal.com/webapps/developer/docs/api/#itemlist-object
PayerInfo struct { PayerInfo struct {
Email string `json:"email,omitempty"` Email string `json:"email,omitempty"`
FirstName string `json:"first_name,omitempty"` FirstName string `json:"first_name,omitempty"`
@ -186,7 +186,7 @@ type (
TaxID string `json:"tax_id,omitempty"` TaxID string `json:"tax_id,omitempty"`
} }
// Payment struct // Payment https://developer.paypal.com/webapps/developer/docs/api/#payment-object
Payment struct { Payment struct {
Intent string `json:"intent"` Intent string `json:"intent"`
Payer *Payer `json:"payer"` Payer *Payer `json:"payer"`
@ -199,7 +199,7 @@ type (
ExperienceProfileID string `json:"experience_profile_id,omitempty"` ExperienceProfileID string `json:"experience_profile_id,omitempty"`
} }
// PaymentLink structure // PaymentLink https://developer.paypal.com/webapps/developer/docs/api/#paymentlink-object
PaymentLink struct { PaymentLink struct {
Href string `json:"href"` Href string `json:"href"`
Rel string `json:"rel"` Rel string `json:"rel"`
@ -211,13 +211,13 @@ type (
Links []PaymentLink `json:"links"` Links []PaymentLink `json:"links"`
} }
// RedirectURLs for redirect_urls // RedirectURLs https://developer.paypal.com/webapps/developer/docs/api/#redirecturls-object
RedirectURLs struct { RedirectURLs struct {
ReturnURL string `json:"return_url,omitempty"` ReturnURL string `json:"return_url,omitempty"`
CancelURL string `json:"cancel_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 { Refund struct {
ID string `json:"id,omitempty"` ID string `json:"id,omitempty"`
Amount *Amount `json:"amount,omitempty"` Amount *Amount `json:"amount,omitempty"`
@ -228,7 +228,7 @@ type (
UpdateTime *time.Time `json:"update_time,omitempty"` 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 { Sale struct {
ID string `json:"id,omitempty"` ID string `json:"id,omitempty"`
Amount *Amount `json:"amount,omitempty"` Amount *Amount `json:"amount,omitempty"`
@ -246,7 +246,7 @@ type (
Links []Links `json:"links,omitempty"` Links []Links `json:"links,omitempty"`
} }
// ShippingAddress for shipping_address // ShippingAddress https://developer.paypal.com/webapps/developer/docs/api/#shippingaddredd-object
ShippingAddress struct { ShippingAddress struct {
RecipientName string `json:"recipient_name,omitempty"` RecipientName string `json:"recipient_name,omitempty"`
Type string `json:"type,omitempty"` Type string `json:"type,omitempty"`
@ -266,7 +266,7 @@ type (
Type string `json:"token_type"` Type string `json:"token_type"`
} }
// Transaction is for transaction object // Transaction https://developer.paypal.com/webapps/developer/docs/api/#transaction-object
Transaction struct { Transaction struct {
Amount *Amount `json:"amount"` Amount *Amount `json:"amount"`
Description string `json:"description,omitempty"` Description string `json:"description,omitempty"`