Created
December 19, 2011 09:24
-
-
Save omarabid/1496282 to your computer and use it in GitHub Desktop.
PayPal ExpressCheckout for WordPress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
gist can be forked. It's too small for a full repo.
Regards,
Abid Omar
(Sent from Google Nexus S)
…On Mar 2, 2012 5:39 PM, "Julien Chaumond" < ***@***.***> wrote:
I'd like to improve this, any way you can make it a Github repo? Thanks!
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1496282
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)
Sorry, Github code formatting is strange.
Other thing, CURRENCYCODE is deprecated, it's now PAYMENTREQUEST_0_CURRENCYCODE (your current code hardcodes USD)
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
I'd like to improve this, any way you can make it a Github repo? Thanks!