paypal

package module
v0.0.0-...-0f12d2c Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2016 License: Apache-2.0 Imports: 6 Imported by: 0

README

paypal

  • This is a forked version of the origial library. This package adds the following methods:
SetExpressCheckoutSingle()
CreateRecurringPaymentsProfile()
BillOutstandingAmount()

paypal is a Go package that allows you to access PayPal APIs, with optional Google AppEngine support, using the "PayPal NVP" format.

Included is a method for using the Digital Goods for Express Checkout payment option.

This fork lets you choose to use AppEngine's urlfetch package to create the HTTP Client

Quick Start: Setting Up a PayPal Charge (Redirect)

####### Standard Go Usage

import (
  "fmt"
  "github.com/crowdmob/paypal"
)

func paypalExpressCheckoutHandler(w http.ResponseWriter, r *http.Request) {
  // An example to setup paypal express checkout for digital goods
  currencyCode := "USD"
  isSandbox    := true
  returnURL    := "http://example.com/returnURL"
  cancelURL    := "http://example.com/cancelURL"

  // Create the paypal Client with default http client
  client := paypal.NewDefaultClient("Your_Username", "Your_Password", "Your_Signature", isSandbox)

  // Make a array of your digital-goods
  testGoods := []paypal.PayPalDigitalGood{paypal.PayPalDigitalGood{
    Name: "Test Good",
    Amount: 200.000,
    Quantity: 5,
  }}

  // Sum amounts and get the token!
  response, err := client.SetExpressCheckoutDigitalGoods(paypal.SumPayPalDigitalGoodAmounts(&testGoods),
    currencyCode,
    returnURL,
    cancelURL,
    testGoods,
  )

  if err != nil {
    // ... gracefully handle error
  } else { // redirect to paypal
    http.Redirect(w, r, response.CheckoutUrl(), 301)
  }
}

####### App Engine Usage

import (
	"fmt"
	"github.com/crowdmob/paypal"
	"appengine"
	"appengine/urlfetch"
)

func paypalExpressCheckoutHandler(w http.ResponseWriter, r *http.Request) {
	// An example to setup paypal express checkout for digital goods
	currencyCode := "USD"
	isSandbox    := true
	returnURL    := "http://example.com/returnURL"
	cancelURL    := "http://example.com/cancelURL"

	// Create the paypal Client with urlfetch
	client := paypal.NewClient("Your_Username", "Your_Password", "Your_Signature", urlfetch.Client(appengine.NewContext(r)), isSandbox)

  // Make a array of your digital-goods
  testGoods := []paypal.PayPalDigitalGood{paypal.PayPalDigitalGood{
    Name: "Test Good",
    Amount: 200.000,
    Quantity: 5,
  }}

  // Sum amounts and get the token!
  response, err := client.SetExpressCheckoutDigitalGoods(paypal.SumPayPalDigitalGoodAmounts(&testGoods),
    currencyCode,
    returnURL,
    cancelURL,
    testGoods,
  )

  if err != nil {
  // ... gracefully handle error
  } else { // redirect to paypal
    http.Redirect(w, r, response.CheckoutUrl(), 301)
  }
}

Quick Start: Completing a PayPal Charge

According to their documentation (see bottom of page), you'll have to call their DoExpressCheckoutPayment api to successfully charge a transaction.

In the Return URL, PayPal will append a token and PayerId to your return URL as parameters, like this: token=XX-XXXXXXXXXXXXXXXXXX&PayerID=XXXXXXXXXXXXX.

Your controller for the Return URL typically will call the DoExpressCheckoutSale (or DoExpressCheckoutPayment for more control), as follows:

client := paypal.NewDefaultClient("Your_Username", "Your_Password", "Your_Signature", isSandbox)
response, err := client.DoExpressCheckoutSale(r.FormValue("token"), r.FormValue("PayerID"), "USD", AMOUNT_OF_SALE)

if err != nil { // handle error in charging
  http.Redirect(w, r, MY_CHARGE_ERROR_URL, 301)
} else { // success!
  // ... handle successful charge
  http.Redirect(w, r, fmt.Sprintf("%s?receipt-id=%s", MY_RECEIPT_URL, response.Values["PAYMENTREQUEST_0_TRANSACTIONID"][0]), 301)
}

Running Tests

There's a test suite included. To run it, simply run:

go test paypal_test.go

You'll have to have set the following environment variables to run the tests:

export PAYPAL_TEST_USERNAME=XXX
export PAYPAL_TEST_PASSWORD=XXX
export PAYPAL_TEST_SIGNATURE=XXX

Tests currently run in sandbox.

PayPal Documentation

PayPal documentation is scattered at best. Here's the best link I've found that describes it: How to Create One-Time Payments Using Express Checkout

PayPal NVP Express Checkout Flow

Documentation

Index

Constants

View Source
const (
	NVP_SANDBOX_URL         = "https://api-3t.sandbox.paypal.com/nvp"
	NVP_PRODUCTION_URL      = "https://api-3t.paypal.com/nvp"
	CHECKOUT_SANDBOX_URL    = "https://www.sandbox.paypal.com/cgi-bin/webscr"
	CHECKOUT_PRODUCTION_URL = "https://www.paypal.com/cgi-bin/webscr"
	NVP_VERSION             = "84"
)

Variables

This section is empty.

Functions

func SumPayPalDigitalGoodAmounts

func SumPayPalDigitalGoodAmounts(goods *[]PayPalDigitalGood) (sum float64)

Types

type Action

type Action string
const (
	Cancel     Action = "Cancel"
	Suspend    Action = "Suspend"
	Reactivate Action = "Reactivate"
)

type ExpressCheckoutSingleArgs

type ExpressCheckoutSingleArgs struct {
	Amount                             float64
	CurrencyCode, ReturnURL, CancelURL string
	Recurring                          bool
	Item                               *PayPalDigitalGood
}

func NewExpressCheckoutSingleArgs

func NewExpressCheckoutSingleArgs() *ExpressCheckoutSingleArgs

type PayPalClient

type PayPalClient struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(username, password, signature string, usesSandbox bool, client *http.Client) *PayPalClient

func NewDefaultClient

func NewDefaultClient(username, password, signature string, usesSandbox bool) *PayPalClient

func NewDefaultClientEndpoint

func NewDefaultClientEndpoint(username, password, signature, endpoint string, usesSandbox bool) *PayPalClient

func (*PayPalClient) BillOutstandingAmount

func (pClient *PayPalClient) BillOutstandingAmount(profileId string) (*PayPalResponse, error)

func (*PayPalClient) CreateRecurringPaymentsProfile

func (pClient *PayPalClient) CreateRecurringPaymentsProfile(token string, params map[string]string) (*PayPalResponse, error)

func (*PayPalClient) DoExpressCheckoutPayment

func (pClient *PayPalClient) DoExpressCheckoutPayment(token, payerId, paymentType, currencyCode string, finalPaymentAmount float64) (*PayPalResponse, error)

paymentType can be "Sale" or "Authorization" or "Order" (ship later)

func (*PayPalClient) DoExpressCheckoutSale

func (pClient *PayPalClient) DoExpressCheckoutSale(token, payerId, currencyCode string, finalPaymentAmount float64) (*PayPalResponse, error)

Convenience function for Sale (Charge)

func (*PayPalClient) GetExpressCheckoutDetails

func (pClient *PayPalClient) GetExpressCheckoutDetails(token string) (*PayPalResponse, error)

func (*PayPalClient) GetRecurringPaymentsProfileDetails

func (pClient *PayPalClient) GetRecurringPaymentsProfileDetails(profileId string) (*PayPalResponse, error)

func (*PayPalClient) ManageRecurringPaymentsProfileStatus

func (pClient *PayPalClient) ManageRecurringPaymentsProfileStatus(profileId string, action Action) (*PayPalResponse, error)

func (*PayPalClient) PerformRequest

func (pClient *PayPalClient) PerformRequest(values url.Values) (*PayPalResponse, error)

func (*PayPalClient) ProfileTransactionSearch

func (pClient *PayPalClient) ProfileTransactionSearch(profileId string, startDate time.Time) (*PayPalResponse, error)

func (*PayPalClient) SetExpressCheckoutDigitalGoods

func (pClient *PayPalClient) SetExpressCheckoutDigitalGoods(paymentAmount float64, currencyCode string, returnURL, cancelURL string, goods []PayPalDigitalGood) (*PayPalResponse, error)

func (*PayPalClient) SetExpressCheckoutSingle

func (pClient *PayPalClient) SetExpressCheckoutSingle(args *ExpressCheckoutSingleArgs) (*PayPalResponse, error)

func (*PayPalClient) UpdateRecurringPaymentsProfile

func (pClient *PayPalClient) UpdateRecurringPaymentsProfile(profileId string, params map[string]string) (*PayPalResponse, error)

type PayPalDigitalGood

type PayPalDigitalGood struct {
	Name     string
	Amount   float64
	Quantity int16
}

func NewDigitalGood

func NewDigitalGood(name string, amount float64) *PayPalDigitalGood

type PayPalError

type PayPalError struct {
	Ack          string
	ErrorCode    string
	ShortMessage string
	LongMessage  string
	SeverityCode string
}

func (*PayPalError) Error

func (e *PayPalError) Error() string

type PayPalResponse

type PayPalResponse struct {
	Ack           string
	CorrelationId string
	Timestamp     string
	Version       string
	Build         string
	Values        url.Values
	// contains filtered or unexported fields
}

func (*PayPalResponse) CheckoutUrl

func (r *PayPalResponse) CheckoutUrl() string

Jump to

Keyboard shortcuts

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