Skip to content

Instantly share code, notes, and snippets.

@divinity76
Created March 13, 2014 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save divinity76/9526637 to your computer and use it in GitHub Desktop.
Save divinity76/9526637 to your computer and use it in GitHub Desktop.
<?php
var_dump(convertCurrency("100000",'USD','GBP'));
function convertCurrency($currencyAmount, $currencyFrom, $currencyTo) {
// https://developer.paypal.com/docs/classic/api/adaptive-payments/ConvertCurrency_API_Operation/
$bigO = array();
$bigO['userName'] = 'jb-us-seller_api1.paypal.com'; //this is a official paypal test account..
$bigO['password'] = 'WX4WTU3S8MY44S7F';
$bigO['signature'] = 'AFcWxV21C7fd0v3bYYYRCpSSRl31A7yDhhsPUU2XhtMoZXsWHFxu-RWy';
$bigO['appId'] = 'APP-80W284485P519543T';
//$bigO['apiKey']=null;
//$bigO['apiSecret']=null;
//$bigO['authorization']=null;
$bigO['httpMethod'] = 'POST';
$bigO['oauth'] = '';
$bigO['methodUri'] = '';
$bigO['methodName'] = 'ConvertCurrency';
$bigO['dataFormat'] = '';
$bigO['params'] = array();
$bigO['params']['ConvertCurrencyRequest.requestEnvelope.errorLanguage'] = 'EN_US';
$bigO['params']['ConvertCurrencyRequest.requestEnvelope.detailLevel'] = '';
$bigO['params']['ConvertCurrencyRequest.baseAmountList.currency(0).code'] = $currencyFrom;
$bigO['params']['ConvertCurrencyRequest.baseAmountList.currency(0).amount'] = $currencyAmount;
$bigO['params']['ConvertCurrencyRequest.convertToCurrencyList(0).currencyCode'] = $currencyTo;
//PAYPAL $bigO['params']['ConvertCurrencyRequest.convertToCurrencyList(2).currencyCode']='GBP';
//THIS WOULD HAVE BEEN SO EASY
/*PAYPAL API SUCKS SO HARD,
it allows you to convert FROM multiple currencies to a single currency in a single request,
but it does NOT allow you to convert TO multiple currencies in a single request.
Its possible we could get clever and calculate it ourselves, but...
*/
$bigO['params']['ConvertCurrencyRequest.countryCode'] = '';
$bigO['params']['ConvertCurrencyRequest.conversionType'] = '';
$bigO['apiName'] = 'AdaptivePayments';
$headers = [
"X-PAYPAL-SECURITY-USERID: jb-us-seller_api1.paypal.com",
"X-PAYPAL-SECURITY-PASSWORD: WX4WTU3S8MY44S7F",
"X-PAYPAL-SECURITY-SIGNATURE: AFcWxV21C7fd0v3bYYYRCpSSRl31A7yDhhsPUU2XhtMoZXsWHFxu-RWy",
"X-PAYPAL-APPLICATION-ID: APP-80W284485P519543T",
"X-PAYPAL-REQUEST-DATA-FORMAT: JSON",
"X-PAYPAL-RESPONSE-DATA-FORMAT: JSON",
"X-PAYPAL-DEVICE-IPADDRESS: 127.0.0.1",
"X-PAYPAL-REQUEST-SOURCE: merchant-php-sdk-2.0.96",
"X-PAYPAL-SANDBOX-EMAIL-ADDRESS: Platform.sdk.seller@gmail.com",
"Content-Type: application/json",
//"Accept-Language: en-US,en;q=0.8,nb;q=0.6",
//"Cookie: filter=all; sort_method=activity",
//"Origin: http://example.org",
];
$url = "https://devtools-paypal.com/apiexplorer/processReq";
//$url="http://192.168.3.105/dumppost.php";
$ch = curl_init($url);
//static $curl_cookiefiles_arr=array();//workaround for https://bugs.php.net/bug.php?id=66014
// $curl_cookiefiles_arr[]=($tf=tmpfile());
// $tf=stream_get_meta_data($tf);
// $tf=$tf['uri'];
curl_setopt_array($ch, array(CURLOPT_AUTOREFERER => true,
CURLOPT_BINARYTRANSFER => true,
CURLOPT_COOKIESESSION => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_FORBID_REUSE => false,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 11,
CURLOPT_ENCODING => "",
// CURLOPT_COOKIEFILE=>$tf,
// CURLOPT_REFERER=>'http://example.org/',
//CURLOPT_USERAGENT=>'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36',
));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$json = json_encode($bigO);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
//shitty response. really. no kidding.
$shittyjsonresponse = curl_exec($ch);
if (curl_errno($ch)) {
throw new Exception('Curl error (curl_errno='.curl_errno($ch).
') on url '.var_export($url, true).
': '.curl_error($ch));
// echo 'Curl error: ' . curl_error($ch);
}
$invalid_json_response_ex_str = "invalid json response from paypal! ";
$d1 = json_decode($shittyjsonresponse, true);
if (null === $d1) {
throw new Exception($invalid_json_response_ex_str.
" cannot json_decode data!");
} //shit
$response = json_decode($d1['response'], true);
if ($response['responseEnvelope']['ack'] !== 'Success') {
throw new Exception('blabla');
}
//need more white power!
return $response['estimatedAmountTable']['currencyConversionList']['0']['currencyList']['currency']['0']['amount'];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment