Skip to content

Commit

Permalink
Update Stripe PHP Library
Browse files Browse the repository at this point in the history
Update stripe-php to version 1.11.0, tweaked to keep PHP 5.1 support.
  • Loading branch information
franksmule committed Feb 10, 2014
1 parent 6c4850f commit d409938
Show file tree
Hide file tree
Showing 20 changed files with 510 additions and 68 deletions.
4 changes: 2 additions & 2 deletions stripejs/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<module>
<name>stripejs</name>
<displayName><![CDATA[Stripe]]></displayName>
<version><![CDATA[0.9.7]]></version>
<version><![CDATA[0.9.8]]></version>
<description><![CDATA[Accept payments by Credit Card with Stripe (Visa, Mastercard, Amex, Discover and Diners Club)]]></description>
<author><![CDATA[PrestaShop]]></author>
<author><![CDATA[PrestaShop + Ollie]]></author>
<tab><![CDATA[payments_gateways]]></tab>
<confirmUninstall>Warning: all the Stripe customers credit cards and transaction details saved in your database will be deleted. Are you sure you want uninstall this module?</confirmUninstall>
<is_configurable>1</is_configurable>
Expand Down
35 changes: 23 additions & 12 deletions stripejs/lib/Stripe.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
<?php

// Tested on PHP 5.2, 5.3

/* PrestaShop - Replaced all json_* by Tools::json* to ensure compatibility w/ PHP 5.x */

abstract class Stripe
{
public static $apiKey;
public static $apiBase = 'https://api.stripe.com/v1';
public static $verifySslCerts = true;
const VERSION = '1.7.7';

public static function getApiKey() { return self::$apiKey; }
public static function setApiKey($apiKey) { self::$apiKey = $apiKey; }
public static function getVerifySslCerts() { return self::$verifySslCerts; }
public static function setVerifySslCerts($verify) { self::$verifySslCerts = $verify; }
// This snippet (and some of the curl code) due to the Facebook SDK.
if (!function_exists('curl_init')) {
throw new Exception('Stripe needs the CURL PHP extension.');
}
if (!function_exists('json_decode')) {
throw new Exception('Stripe needs the JSON PHP extension.');
}
if (!function_exists('mb_detect_encoding')) {
throw new Exception('Stripe needs the Multibyte String PHP extension.');
}

// Stripe singleton
require(dirname(__FILE__) . '/Stripe/Stripe.php');

// Utilities
require(dirname(__FILE__) . '/Stripe/Util.php');
Expand All @@ -32,15 +35,23 @@ public static function setVerifySslCerts($verify) { self::$verifySslCerts = $ver
require(dirname(__FILE__) . '/Stripe/ApiRequestor.php');
require(dirname(__FILE__) . '/Stripe/ApiResource.php');
require(dirname(__FILE__) . '/Stripe/SingletonApiResource.php');
require(dirname(__FILE__) . '/Stripe/AttachedObject.php');
require(dirname(__FILE__) . '/Stripe/List.php');

// Stripe API Resources
require(dirname(__FILE__) . '/Stripe/Account.php');
require(dirname(__FILE__) . '/Stripe/Card.php');
require(dirname(__FILE__) . '/Stripe/Balance.php');
require(dirname(__FILE__) . '/Stripe/BalanceTransaction.php');
require(dirname(__FILE__) . '/Stripe/Charge.php');
require(dirname(__FILE__) . '/Stripe/Customer.php');
require(dirname(__FILE__) . '/Stripe/Invoice.php');
require(dirname(__FILE__) . '/Stripe/InvoiceItem.php');
require(dirname(__FILE__) . '/Stripe/Plan.php');
require(dirname(__FILE__) . '/Stripe/Subscription.php');
require(dirname(__FILE__) . '/Stripe/Token.php');
require(dirname(__FILE__) . '/Stripe/Coupon.php');
require(dirname(__FILE__) . '/Stripe/Event.php');
require(dirname(__FILE__) . '/Stripe/Transfer.php');
require(dirname(__FILE__) . '/Stripe/Transfer.php');
require(dirname(__FILE__) . '/Stripe/Recipient.php');
require(dirname(__FILE__) . '/Stripe/ApplicationFee.php');
46 changes: 31 additions & 15 deletions stripejs/lib/Stripe/ApiRequestor.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static function apiUrl($url='')

public static function utf8($value)
{
if (is_string($value))
if (is_string($value) && mb_detect_encoding($value, "UTF-8", TRUE) != "UTF-8")
return utf8_encode($value);
else
return $value;
Expand All @@ -26,24 +26,44 @@ public static function utf8($value)
private static function _encodeObjects($d)
{
if ($d instanceof Stripe_ApiResource) {
return $d->id;
return self::utf8($d->id);
} else if ($d === true) {
return 'true';
} else if ($d === false) {
return 'false';
} else if (is_array($d)) {
$res = array();
foreach ($d as $k => $v)
$res[$k] = self::_encodeObjects($v);
$res[$k] = self::_encodeObjects($v);
return $res;
} else {
return $d;
return self::utf8($d);
}
}

public static function encode($d)
public static function encode($arr, $prefix=null)
{
return http_build_query($d, null, '&');
if (!is_array($arr))
return $arr;

$r = array();
foreach ($arr as $k => $v) {
if (is_null($v))
continue;

if ($prefix && $k && !is_int($k))
$k = $prefix."[".$k."]";
else if ($prefix)
$k = $prefix."[]";

if (is_array($v)) {
$r[] = self::encode($v, $k, true);
} else {
$r[] = urlencode($k)."=".urlencode($v);
}
}

return implode("&", $r);
}

public function request($meth, $url, $params=null)
Expand Down Expand Up @@ -96,22 +116,18 @@ private function _requestRaw($meth, $url, $params)
'publisher' => 'stripe',
'uname' => $uname);
$headers = array('X-Stripe-Client-User-Agent: ' . Tools::jsonEncode($ua), /* PrestaShop */
'X-Stripe-Application: ca_0ZCKiqjiWpOYdxqHZPKaKQlRDnNPhd9P',
'User-Agent: Stripe/v1 PhpBindings/' . Stripe::VERSION,
'Authorization: Bearer ' . $myApiKey);
'User-Agent: Stripe/v1 PhpBindings/' . Stripe::VERSION,
'Authorization: Bearer ' . $myApiKey);
if (Stripe::$apiVersion)
$headers[] = 'Stripe-Version: ' . Stripe::$apiVersion;
list($rbody, $rcode) = $this->_curlRequest($meth, $absUrl, $headers, $params);
return array($rbody, $rcode, $myApiKey);
}

private function _interpretResponse($rbody, $rcode)
{
try {
$rbody = trim($rbody, '"');
$rbody = str_replace('\n', '', $rbody);
$rbody = str_replace("\n", '', $rbody);
$rbody = str_replace("\\", '', $rbody);

$resp = json_decode($rbody, true); /* PrestaShop */
$resp = json_decode($rbody, true);
} catch (Exception $e) {
throw new Stripe_ApiError("Invalid response body from API: $rbody (HTTP response code was $rcode)", $rcode, $rbody);
}
Expand Down
22 changes: 11 additions & 11 deletions stripejs/lib/Stripe/ApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public function refresh()
{
$requestor = new Stripe_ApiRequestor($this->_apiKey);
$url = $this->instanceUrl();
list($response, $apiKey) = $requestor->request('get', $url);

list($response, $apiKey) = $requestor->request('get', $url, $this->_retrieveOptions);
$this->refreshFrom($response, $apiKey);
return $this;
}
Expand All @@ -33,8 +34,8 @@ public static function className($class)

public static function classUrl($class)
{
$base = self::className($class);
return "/${base}s";
$base = self::_scopedLsb($class, 'className', $class);
return "/v1/${base}s";
}

public function instanceUrl()
Expand All @@ -45,7 +46,7 @@ public function instanceUrl()
throw new Stripe_InvalidRequestError("Could not determine which URL to request: $class instance has invalid ID: $id", null);
}
$id = Stripe_ApiRequestor::utf8($id);
$base = self::classUrl($class);
$base = $this->_lsb('classUrl', $class);
$extn = urlencode($id);
return "$base/$extn";
}
Expand All @@ -62,7 +63,7 @@ protected static function _scopedAll($class, $params=null, $apiKey=null)
{
self::_validateCall('all', $params, $apiKey);
$requestor = new Stripe_ApiRequestor($apiKey);
$url = self::classUrl($class);
$url = self::_scopedLsb($class, 'classUrl', $class);
list($response, $apiKey) = $requestor->request('get', $url, $params);
return Stripe_Util::convertToStripeObject($response, $apiKey);
}
Expand All @@ -71,19 +72,18 @@ protected static function _scopedCreate($class, $params=null, $apiKey=null)
{
self::_validateCall('create', $params, $apiKey);
$requestor = new Stripe_ApiRequestor($apiKey);
$url = self::classUrl($class);
$url = self::_scopedLsb($class, 'classUrl', $class);
list($response, $apiKey) = $requestor->request('post', $url, $params);
return Stripe_Util::convertToStripeObject($response, $apiKey);
}

protected function _scopedSave($class)
{
self::_validateCall('save');
if ($this->_unsavedValues) {
$requestor = new Stripe_ApiRequestor($this->_apiKey);
$params = array();
foreach ($this->_unsavedValues->toArray() as $k)
$params[$k] = $this->$k;
$requestor = new Stripe_ApiRequestor($this->_apiKey);
$params = $this->serializeParameters();

if (count($params) > 0) {
$url = $this->instanceUrl();
list($response, $apiKey) = $requestor->request('post', $url, $params);
$this->refreshFrom($response, $apiKey);
Expand Down
36 changes: 36 additions & 0 deletions stripejs/lib/Stripe/ApplicationFee.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

class Stripe_ApplicationFee extends Stripe_ApiResource
{
public static function className($class)
{
return 'application_fee';
}

public static function constructFrom($values, $apiKey=null)
{
$class = get_class();
return self::scopedConstructFrom($class, $values, $apiKey);
}

public static function retrieve($id, $apiKey=null)
{
$class = get_class();
return self::_scopedRetrieve($class, $id, $apiKey);
}

public static function all($params=null, $apiKey=null)
{
$class = get_class();
return self::_scopedAll($class, $params, $apiKey);
}

public function refund($params=null)
{
$requestor = new Stripe_ApiRequestor($this->_apiKey);
$url = $this->instanceUrl() . '/refund';
list($response, $apiKey) = $requestor->request('post', $url, $params);
$this->refreshFrom($response, $apiKey);
return $this;
}
}
17 changes: 17 additions & 0 deletions stripejs/lib/Stripe/AttachedObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

class Stripe_AttachedObject extends Stripe_Object
{
public function replaceWith($properties)
{
$removed = array_diff(array_keys($this->_values), array_keys($properties));
// Don't unset, but rather set to null so we send up '' for deletion.
foreach ($removed as $k) {
$this->$k = null;
}

foreach ($properties as $k => $v) {
$this->$k = $v;
}
}
}
16 changes: 16 additions & 0 deletions stripejs/lib/Stripe/Balance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

class Stripe_Balance extends Stripe_SingletonApiResource
{
public static function constructFrom($values, $apiKey=null)
{
$class = get_class();
return self::scopedConstructFrom($class, $values, $apiKey);
}

public static function retrieve($apiKey=null)
{
$class = get_class();
return self::_scopedSingletonRetrieve($class, $apiKey);
}
}
26 changes: 26 additions & 0 deletions stripejs/lib/Stripe/BalanceTransaction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

class Stripe_BalanceTransaction extends Stripe_ApiResource
{
public static function classUrl($class) {
return "/v1/balance/history";
}

public static function constructFrom($values, $apiKey=null)
{
$class = get_class();
return self::scopedConstructFrom($class, $values, $apiKey);
}

public static function retrieve($id, $apiKey=null)
{
$class = get_class();
return self::_scopedRetrieve($class, $id, $apiKey);
}

public static function all($params=null, $apiKey=null)
{
$class = get_class();
return self::_scopedAll($class, $params, $apiKey);
}
}
39 changes: 39 additions & 0 deletions stripejs/lib/Stripe/Card.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

class Stripe_Card extends Stripe_ApiResource
{
public static function constructFrom($values, $apiKey=null)
{
$class = get_class();
return self::scopedConstructFrom($class, $values, $apiKey);
}

public function instanceUrl()
{
$id = $this['id'];
$customer = $this['customer'];
$class = get_class($this);
if (!$id) {
throw new Stripe_InvalidRequestError("Could not determine which URL to request: $class instance has invalid ID: $id", null);
}
$id = Stripe_ApiRequestor::utf8($id);
$customer = Stripe_ApiRequestor::utf8($customer);

$base = self::classUrl('Stripe_Customer');
$customerExtn = urlencode($customer);
$extn = urlencode($id);
return "$base/$customerExtn/cards/$extn";
}

public function delete($params=null)
{
$class = get_class();
return self::_scopedDelete($class, $params);
}

public function save()
{
$class = get_class();
return self::_scopedSave($class);
}
}
24 changes: 24 additions & 0 deletions stripejs/lib/Stripe/Charge.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public static function create($params=null, $apiKey=null)
return self::_scopedCreate($class, $params, $apiKey);
}

public function save()
{
$class = get_class();
return self::_scopedSave($class);
}

public function refund($params=null)
{
$requestor = new Stripe_ApiRequestor($this->_apiKey);
Expand All @@ -43,4 +49,22 @@ public function capture($params=null)
$this->refreshFrom($response, $apiKey);
return $this;
}

public function updateDispute($params=null)
{
$requestor = new Stripe_ApiRequestor($this->_apiKey);
$url = $this->instanceUrl() . '/dispute';
list($response, $apiKey) = $requestor->request('post', $url, $params);
$this->refreshFrom(array('dispute' => $response), $apiKey, true);
return $this->dispute;
}

public function closeDispute()
{
$requestor = new Stripe_ApiRequestor($this->_apiKey);
$url = $this->instanceUrl() . '/dispute/close';
list($response, $apiKey) = $requestor->request('post', $url);
$this->refreshFrom($response, $apiKey);
return $this;
}
}
Loading

2 comments on commit d409938

@mistakool
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how do i install?

@franksmule
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.