Skip to content

Instantly share code, notes, and snippets.

@omarabid
Created December 19, 2011 09:24
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omarabid/1496282 to your computer and use it in GitHub Desktop.
Save omarabid/1496282 to your computer and use it in GitHub Desktop.
PayPal ExpressCheckout for WordPress
<?php
/**
* PayPal ExpressCheckOut for WordPress
*
* This code is not licensed. Feel free to use it in your own open source and
* commercial projects. The code is provided "AS IS" without any warranty or
* conditions of any kind.
*
* @author Abid Omar
*/
class wp_adpress_paypal {
/**
* Gateway parameters
*
* @var array
*/
private $gateway;
/**
* PayPal API servers
* @var string
*/
private $server = 'https://api-3t.paypal.com';
/**
* PayPal Payment processing URL
* @var string
*/
private $redirect_url = 'https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&useraction=commit&token=';
/**
* Create a new instance of the PayPal class.
*
* @param array $param
* @param boolean $test_mode set to True for Sandbox mode
*/
function __construct ( $param, $test_mode = false ) {
/*
* Set the gateway array variables
*/
$this->gateway = array(
'USER' => $param['username'],
'PWD' => $param['password'],
'SIGNATURE' => $param['signature'],
'PAYMENTREQUEST_0_PAYMENTACTION' => $param['payment_action'],
'PAYMENTREQUEST_0_AMT' => $param['payment_amount'],
'PAYMENTREQUEST_0_CURRENCYCODE' => $param['currency'],
'RETURNURL' => $param['return_url'],
'CANCELURL' => $param['cancel_url'],
'VERSION' => $param['version'],
'NOSHIPPING' => 1,
'ALLOWNOTE' => 1
);
/*
* Change the server and redirect url if we are in a test mode
*/
if ( $test_mode ) {
$this->server = 'https://api-3t.sandbox.paypal.com/nvp';
$this->redirect_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&useraction=commit&token=';
}
}
/**
* Generate the redirect URL that will ask the user for payment permission
*
* @return string Redirect URL
*/
public function doExpressCheckout () {
$body = $this->gateway;
$body['METHOD'] = 'SetExpressCheckout';
$request = array(
'method' => 'POST',
'body' => $body,
'timeout' => 60,
'sslverify' => false
);
$response = wp_remote_post($this->server, $request);
if ( is_wp_error($response) ) {
return false;
}
parse_str(urldecode($response['body']), $response);
if ( strtolower($response['ACK']) === 'success' ) {
return ($this->redirect_url . $response['TOKEN']);
} else {
return false;
}
}
/**
* Process the payment.
*
* The function returns true if the user completed the payment, and false in the
* other case.
*
* @param string $token
* @param string $payer_id
* @return boolean
*/
public function processPayment ( $token, $payer_id ) {
$body = $this->gateway;
$body['METHOD'] = 'DoExpressCheckoutPayment';
$body['PAYERID'] = $payer_id;
$body['TOKEN'] = $token;
$request = array(
'method' => 'POST',
'body' => $body,
'timeout' => 60,
'sslverify' => false
);
$response = wp_remote_post($this->server, $request);
if ( is_wp_error($response) ) {
return false;
}
parse_str(urldecode($response['body']), $response);
if ( strtolower($response['ACK']) === 'success' && strtolower($response['PAYMENTINFO_0_PAYMENTSTATUS']) === 'completed' ) {
return true;
} else {
return false;
}
}
}
<?php
$gateway = array(
'username' => your_account_username,
'password' => your_account_password,
'signature' => your_account_signature,
'version' => '84.0',
'payment_action' => 'Sale',
'payment_amount' => '20.00',
'currency' => 'USD',
'return_url' => 'http://localhost/devpress/wp-admin/admin.php?page=adpress-paypal_redirect&action=success',
'cancel_url' => 'http://localhost/devpress/wp-admin/admin.php?page=adpress-paypal_redirect&action=cancel'
);
// Create a new instance of the class
$paypal = new wp_adpress_paypal($gateway, true);
// Get the redirect URL
$redirect_url = $paypal->doExpressCheckout();
// Process the payment
$payment = $paypal->processPayment();
PayPal ExpressCheckout for WordPress
Usage:
1. Declare an array with all the required parameters.
2. Create a new instance of the class. Set the second parameter to true if you want to enable SandBox mode.
3. Get the redirect URL with the doExpressCheckout function.
4. Redirect the user to that URL to request permission.
5. Use the processPayment function to process the payment.
@julien-c
Copy link

julien-c commented Mar 2, 2012

I'd like to improve this, any way you can make it a Github repo? Thanks!

@omarabid
Copy link
Author

omarabid commented Mar 2, 2012 via email

@julien-c
Copy link

julien-c commented Mar 2, 2012

Well, the thing is, I think your code misses a piece in the "three-way handshake" with Paypal (the one about getting Express Checkout Details)

@julien-c
Copy link

julien-c commented Mar 2, 2012

Sorry, Github code formatting is strange.

Other thing, CURRENCYCODE is deprecated, it's now PAYMENTREQUEST_0_CURRENCYCODE (your current code hardcodes USD)

@omarabid
Copy link
Author

omarabid commented Mar 2, 2012

Thanks for mentioning the CurrencyCode issue. I'm already aware of it, but it broke a few of my applications before I did.

For the "three-way handshake", the get ExpressCheckout Details is optional (last time I read the PayPal documentation). Do you know any advantages of using it. Most classes I saw did implement it, but I have that habit of going with the minimum required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment