gopaypal

package module
v0.0.0-...-aeccb6a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2020 License: MIT Imports: 10 Imported by: 1

README

gopaypal

Go client for the PayPal REST API

Usage

First we create a gopaypal client

client := NewClient(clientID, secretKey, URL)

Then we can start by getting PayPal OAuth2 token

tokenResponse, err := client.GetAccessToken()

You dont really need to get the token response. The client will save your token and update it when needed. After we have the token we can start calling the PayPal REST API. For more information check the tests

Missing endpoints

You can still use gopaypal even if the endpoint you look for is missing. Create a client and use AuthRequest or BasicRequest

Testing

Testing gopaypal involves the following mandatory command line arguments:

  • clientid Your application client identifier
  • secret Your application secret key
  • redirect Your application redirect URL. Must match your redirect URL of your PayPal application

The following arguments are optional:

  • token PayPal OAuth2 token. You can pass this argument to skip the OAuth test
  • iden Identifier code given by PayPal after a successful login using the identity API. You should pass this argument in order to test several identity API functions
  • paymentid Payment identifier to test the execution of an approved PayPal payment
  • payerid Payer identifier to test the execution of an approved PayPal payment

License

gopaypal is made available under the MIT license

Documentation

Index

Constants

View Source
const (
	SandBoxURL          = "https://api.sandbox.paypal.com/"
	LiveURL             = "https://api.paypal.com"
	IdentitySandBoxURL  = "https://www.sandbox.paypal.com"
	IdentityLiveURL     = "https://www.paypal.com"
	IdentityUserInfoURL = "/v1/identity/openidconnect/userinfo/?schema=openid"
	PaymentCreateURL    = "/v1/payments/payment"
	PaymentExecuteURL   = "/v1/payments/payment/%v/execute"
	PaymentInfoURL      = "/v1/payments/payment/%v"
	OAuthURL            = "/v1/oauth2/token"
	IdentityURL         = "/signin/authorize"
	IdentityTokenURL    = "/v1/identity/openidconnect/tokenservice"
)

Variables

This section is empty.

Functions

func CreateNonce

func CreateNonce() string

CreateNonce creates and returns an arbitrary ID that may only be used once

Types

type Amount

type Amount struct {
	Currency string  `json:"currency,omitempty"`
	Total    string  `json:"total,omitempty"`
	Details  Details `json:"details,omitempty"`
}

type Client

type Client struct {
	AccessToken *oauthResponse
	// contains filtered or unexported fields
}

Client gopaypal client for communicating with the PayPal REST API endpoints

func NewClient

func NewClient(clientID, secret, base string) Client

NewClient creates and returns a new gopaypal client with the given credentials

func (*Client) AuthRequest

func (c *Client) AuthRequest(endpoint string, b []byte, method string) (*http.Request, error)

AuthRequest creates a basic request to the PayPal endpoint with the Authorization header set

func (Client) BasicRequest

func (c Client) BasicRequest(endpoint string, b []byte, method string) (*http.Request, error)

BasicRequest creates a basic request to the PayPal endpoint without the Authorization header

func (Client) CreatePayment

func (c Client) CreatePayment(payment Payment) (*paymentCreateResponse, error)

CreatePayment creates a PayPal payment with the given payment object

func (Client) Execute

func (c Client) Execute(req *http.Request) ([]byte, error)

Execute runs the given HTTP request

func (Client) ExecutePayment

func (c Client) ExecutePayment(paymentID, payerID string) (*paymentCreateResponse, error)

func (Client) GenerateIdentityURL

func (c Client) GenerateIdentityURL(state string, ret string, scope []string) (*url.URL, error)

GenerateIdentityURL creates and returns an identity URL used to log-in into the PayPal services

func (*Client) GetAccessToken

func (c *Client) GetAccessToken() (*oauthResponse, error)

GetAccessToken gets the OAuth2 token from the PayPal endpoint

func (*Client) GetTokenFromIdentityCode

func (c *Client) GetTokenFromIdentityCode(code, ret string) (*IdentityAccessTokenResponse, error)

GetTokenFromIdentityCode returns the access token from the given identity login code

func (*Client) GetTokenFromRefreshToken

func (c *Client) GetTokenFromRefreshToken(refresh string) (*IdentityAccessTokenResponse, error)

GetTokenFromRefreshToken returns the access token from the given identity refresh token

func (Client) GetUserInfo

func (c Client) GetUserInfo(tkn string) (*IdentityUserInfoResponse, error)

GetUserInfo gets user profile attributes by the given access token

func (Client) PaymentInformation

func (c Client) PaymentInformation(paymentID string) (*paymentCreateResponse, error)

type Details

type Details struct {
	SubTotal         string `json:"subtotal,omitempty"`
	Shipping         string `json:"shipping,omitempty"`
	Tax              string `json:"tax,omitempty"`
	HandlingFee      string `json:"handling_fee,omitempty"`
	ShippingDiscount string `json:"shipping_discount,omitempty"`
	Insurance        string `json:"insurance,omitempty"`
	GiftWrap         string `json:"gift_wrap,omitempty"`
}

type IdentityAccessTokenResponse

type IdentityAccessTokenResponse struct {
	TokenType    string `json:"token_type"`
	Expires      string `json:"expires_in"`
	RefreshToken string `json:"refresh_token"`
	AccessToken  string `json:"access_token"`
	// contains filtered or unexported fields
}

type IdentityAddress

type IdentityAddress struct {
	StreetAddress string `json:"street_address"`
	Locality      string `json:"locality"`
	Region        string `json:"region"`
	PostalCode    string `json:"postal_code"`
	Country       string `json:"country"`
}

type IdentityUserInfoResponse

type IdentityUserInfoResponse struct {
	UserID          string          `json:"user_id"`
	Sub             string          `json:"sub"`
	Name            string          `json:"name"`
	GivenName       string          `json:"given_name"`
	FamilyName      string          `json:"family_name"`
	MiddleName      string          `json:"middle_name"`
	Picture         string          `json:"picture"`
	Email           string          `json:"email"`
	EmailVerified   bool            `json:"email_verified"`
	Gender          string          `json:"gender"`
	BirthDate       string          `json:"birthdate"`
	ZoneInfo        string          `json:"zoneinfo"`
	Locale          string          `json:"locale"`
	PhoneNumber     string          `json:"phone_number"`
	AccountVerified bool            `json:"verified_account"`
	Address         IdentityAddress `json:"address"`
	AccountType     string          `json:"account_type"`
	AgeRange        string          `json:"age_range"`
	PayerID         string          `json:"payer_id"`
}

type Item

type Item struct {
	Sku         string `json:"sku,omitempty"`
	Name        string `json:"name,omitempty"`
	Description string `json:"description,omitempty"`
	Quantity    int    `json:"quantity,omitempty"`
	Price       string `json:"price,omitempty"`
	Currency    string `json:"currency,omitempty"`
	Tax         string `json:"tax,omitempty"`
	URL         string `json:"url,omitempty"`
}

type ItemList

type ItemList struct {
	Items               []Item `json:"items,omitempty"`
	ShippingMethod      string `json:"shipping_method,omitempty"`
	ShippingPhoneNumber string `json:"shipping_phone_number,omitempty"`
}
type Link struct {
	Href   string `json:"href"`
	Rel    string `json:"rel"`
	Method string `json:"method"`
}

type PayPalError

type PayPalError struct {
	Name             string `json:"name"`
	Message          string `json:"message"`
	ErrorDescription string `json:"error_description"`
}

type Payer

type Payer struct {
	PaymentMethod string    `json:"payment_method,omitempty"`
	Status        string    `json:"status,omitempty"`
	Info          PayerInfo `json:"payer_info,omitempty"`
}

type PayerInfo

type PayerInfo struct {
	ID string `json:"payer_id,omitempty"`
}

type Payment

type Payment struct {
	Intent       string        `json:"intent,omitempty"`
	Payer        Payer         `json:"payer,omitempty"`
	Transactions []Transaction `json:"transactions,omitempty"`
	RedirectURL  RedirectURL   `json:"redirect_urls,omitempty"`
}

type PaymentOptions

type PaymentOptions struct {
	AllowedPaymentMethod string `json:"allowed_payment_method,omitempty"`
}

type RedirectURL

type RedirectURL struct {
	ReturnURL string `json:"return_url,omitempty"`
	CancelURL string `json:"cancel_url,omitempty"`
}

type RelatedResources

type RelatedResources struct {
	Sale Sale `json:"sale,omitempty"`
}

type Sale

type Sale struct {
	ID                      string `json:"id,omitempty"`
	PurchaseUnitReferenceID string `json:"purchase_unit_reference_id,omitempty"`
	Amount                  Amount `json:"amount,omitempty"`
	PaymentMode             string `json:"payment_mode,omitempty"`
	State                   string `json:"state,omitempty"`
	ReasonCode              string `json:"reason_code,omitempty"`
	ClearingTime            string `json:"clearing_time,omitempty"`
	ReceiptID               string `json:"receipt_id,omitempty"`
}

type Transaction

type Transaction struct {
	Amount           Amount              `json:"amount,omitempty"`
	Description      string              `json:"description,omitempty"`
	NoteToPayee      string              `json:"note_to_payee,omitempty"`
	Custom           string              `json:"custom,omitempty"`
	InvoiceNumber    string              `json:"invoice_number,omitempty"`
	SoftDescriptor   string              `json:"soft_descriptor,omitempty"`
	PaymentOptions   PaymentOptions      `json:"payment_options,omitempty"`
	ItemList         ItemList            `json:"item_list,omitempty"`
	NotifyURL        string              `json:"notify_url,omitempty"`
	OrderURL         string              `json:"order_url,omitempty"`
	RelatedResources []*RelatedResources `json:"related_resources,omitempty"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL