Coder Social home page Coder Social logo

paypal's Introduction

paypal

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

paypal's People

Contributors

baz avatar crowdmatt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.