Skip to content

Instantly share code, notes, and snippets.

@steverobbins
Last active April 22, 2024 12:18
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save steverobbins/c5aa62cf7cd755dbfa64 to your computer and use it in GitHub Desktop.
Save steverobbins/c5aa62cf7cd755dbfa64 to your computer and use it in GitHub Desktop.
PayPal API Requests
<?php
class Paypal
{
/**
* API Version
*/
const VERSION = 51.0;
/**
* List of valid API environments
* @var array
*/
private $allowedEnvs = array(
'beta-sandbox',
'live',
'sandbox'
);
/**
* Config storage from constructor
* @var array
*/
private $config = array();
/**
* URL storage based on environment
* @var string
*/
private $url;
/**
* Build PayPal API request
*
* @param string $username
* @param string $password
* @param string $signature
* @param string $environment
*/
public function __construct($username, $password, $signature, $environment = 'live')
{
if (!in_array($environment, $this->allowedEnvs)) {
throw new Exception('Specified environment is not allowed.');
}
$this->config = array(
'username' => $username,
'password' => $password,
'signature' => $signature,
'environment' => $environment
);
}
/**
* Make a request to the PayPal API
*
* @param string $method API method (e.g. GetBalance)
* @param array $params Additional fields to send in the request (e.g. array('RETURNALLCURRENCIES' => 1))
* @return array
*/
public function call($method, array $params = array())
{
$fields = $this->encodeFields(array_merge(
array(
'METHOD' => $method
'VERSION' => self::VERSION,
'USER' => $this->config['username'],
'PWD' => $this->config['password'],
'SIGNATURE' => $this->config['signature']
),
$params
));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->getUrl());
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
if (!$response) {
throw new Exception('Failed to contact PayPal API: ' . curl_error($ch) . ' (Error No. ' . curl_errno($ch) . ')');
}
curl_close($ch);
parse_str($response, $result);
return $this->decodeFields($result);
}
/**
* Prepare fields for API
*
* @param array $fields
* @return array
*/
private function encodeFields(array $fields)
{
return array_map('urlencode', $fields);
}
/**
* Make response readable
*
* @param array $fields
* @return array
*/
private function decodeFields(array $fields)
{
return array_map('urldecode', $fields);
}
/**
* Get API url based on environment
*
* @return string
*/
private function getUrl()
{
if (is_null($this->url)) {
switch ($this->config['environment']) {
case 'sandbox':
case 'beta-sandbox':
$this->url = "https://api-3t.$environment.paypal.com/nvp";
break;
default:
$this->url = 'https://api-3t.paypal.com/nvp';
}
}
return $this->url;
}
}
@steverobbins
Copy link
Author

Based on this Stack Overflow answer.

Get PayPal API credentials.

Example usage:

<?php

include 'Paypal.php';
$paypal = new Paypal('username', 'password', 'signature');
var_dump($paypal->call('GetBalance'));
// For currencies other than USD
//var_dump($paypal->call('GetBalance', array('RETURNALLCURRENCIES' => 1)));

Response (I'm broke):

$ php test.php 
array(7) {
  ["L_AMT0"]=>
  string(4) "0.00"
  ["L_CURRENCYCODE0"]=>
  string(3) "USD"
  ["TIMESTAMP"]=>
  string(20) "2014-12-01T21:43:06Z"
  ["CORRELATIONID"]=>
  string(13) "d0587ef83dd5f"
  ["ACK"]=>
  string(7) "Success"
  ["VERSION"]=>
  string(2) "51"
  ["BUILD"]=>
  string(8) "13834430"
}

@NEMONAUT
Copy link

Thank you for this! I did find a missing comma on line 64 however:

'METHOD' => $method

@banarsiamin
Copy link

And All schedure to send payment any some user

@airflo
Copy link

airflo commented Dec 11, 2016

Great work! But please fix the missing comma on line 64 as pointed out by Nemonaut. Otherwise, you will get an error message similar to this one:

Parse error: syntax error, unexpected ''VERSION'' (T_CONSTANT_ENCAPSED_STRING), expecting ')' in /path/Paypal.php on line 65

@GeeksEmpireOfficial
Copy link

You are a Great Time Save ;)

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