Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stripeintegration #265

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions libs/Stripe/Account.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

namespace Stripe;

/**
* Class Account
*
* @property string $id
* @property string $object
* @property mixed $business_logo
* @property string $business_name
* @property mixed $business_url
* @property bool $charges_enabled
* @property string $country
* @property bool $debit_negative_balances
* @property mixed $decline_charge_on
* @property string $default_currency
* @property bool $details_submitted
* @property string $display_name
* @property string $email
* @property mixed $external_accounts
* @property mixed $legal_entity
* @property bool $managed
* @property mixed $product_description
* @property mixed $statement_descriptor
* @property mixed $support_email
* @property mixed $support_phone
* @property string $timezone
* @property mixed $tos_acceptance
* @property mixed $transfer_schedule
* @property bool $transfers_enabled
* @property mixed $verification
* @property mixed $keys
*
* @package Stripe
*/
class Account extends ApiResource
{
public function instanceUrl()
{
if ($this['id'] === null) {
return '/v1/account';
} else {
return parent::instanceUrl();
}
}

/**
* @param string|null $id
* @param array|string|null $opts
*
* @return Account
*/
public static function retrieve($id = null, $opts = null)
{
if (!$opts && is_string($id) && substr($id, 0, 3) === 'sk_') {
$opts = $id;
$id = null;
}
return self::_retrieve($id, $opts);
}

/**
* @param array|null $params
* @param array|string|null $opts
*
* @return Account
*/
public static function create($params = null, $opts = null)
{
return self::_create($params, $opts);
}

/**
* @param string $id The ID of the account to update.
* @param array|null $params
* @param array|string|null $options
*
* @return Account The updated account.
*/
public static function update($id, $params = null, $options = null)
{
return self::_update($id, $params, $options);
}

/**
* @param array|string|null $opts
*
* @return Account
*/
public function save($opts = null)
{
return $this->_save($opts);
}

/**
* @param array|null $params
* @param array|string|null $opts
*
* @return Account The deleted account.
*/
public function delete($params = null, $opts = null)
{
return $this->_delete($params, $opts);
}

/**
* @param array|null $params
* @param array|string|null $opts
*
* @return Account The rejected account.
*/
public function reject($params = null, $opts = null)
{
$url = $this->instanceUrl() . '/reject';
list($response, $opts) = $this->_request('post', $url, $params, $opts);
$this->refreshFrom($response, $opts);
return $this;
}

/**
* @param array|null $params
* @param array|string|null $opts
*
* @return Collection of Accounts
*/
public static function all($params = null, $opts = null)
{
return self::_all($params, $opts);
}
}
13 changes: 13 additions & 0 deletions libs/Stripe/AlipayAccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Stripe;

/**
* Class AlipayAccount
*
* @package Stripe
*/
class AlipayAccount extends ExternalAccount
{

}
244 changes: 244 additions & 0 deletions libs/Stripe/ApiRequestor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
<?php

namespace Stripe;

/**
* Class ApiRequestor
*
* @package Stripe
*/
class ApiRequestor
{
private $_apiKey;

private $_apiBase;

private static $_httpClient;

public function __construct($apiKey = null, $apiBase = null)
{
$this->_apiKey = $apiKey;
if (!$apiBase) {
$apiBase = Stripe::$apiBase;
}
$this->_apiBase = $apiBase;
}

private static function _encodeObjects($d)
{
if ($d instanceof ApiResource) {
return Util\Util::utf8($d->id);
} elseif ($d === true) {
return 'true';
} elseif ($d === false) {
return 'false';
} elseif (is_array($d)) {
$res = array();
foreach ($d as $k => $v) {
$res[$k] = self::_encodeObjects($v);
}
return $res;
} else {
return Util\Util::utf8($d);
}
}

/**
* @param string $method
* @param string $url
* @param array|null $params
* @param array|null $headers
*
* @return array An array whose first element is an API response and second
* element is the API key used to make the request.
*/
public function request($method, $url, $params = null, $headers = null)
{
if (!$params) {
$params = array();
}
if (!$headers) {
$headers = array();
}
list($rbody, $rcode, $rheaders, $myApiKey) =
$this->_requestRaw($method, $url, $params, $headers);
$json = $this->_interpretResponse($rbody, $rcode, $rheaders);
$resp = new ApiResponse($rbody, $rcode, $rheaders, $json);
return array($resp, $myApiKey);
}

/**
* @param string $rbody A JSON string.
* @param int $rcode
* @param array $rheaders
* @param array $resp
*
* @throws Error\InvalidRequest if the error is caused by the user.
* @throws Error\Authentication if the error is caused by a lack of
* permissions.
* @throws Error\Card if the error is the error code is 402 (payment
* required)
* @throws Error\RateLimit if the error is caused by too many requests
* hitting the API.
* @throws Error\Api otherwise.
*/
public function handleApiError($rbody, $rcode, $rheaders, $resp)
{
if (!is_array($resp) || !isset($resp['error'])) {
$msg = "Invalid response object from API: $rbody "
. "(HTTP response code was $rcode)";
throw new Error\Api($msg, $rcode, $rbody, $resp, $rheaders);
}

$error = $resp['error'];
$msg = isset($error['message']) ? $error['message'] : null;
$param = isset($error['param']) ? $error['param'] : null;
$code = isset($error['code']) ? $error['code'] : null;

switch ($rcode) {
case 400:
// 'rate_limit' code is deprecated, but left here for backwards compatibility
// for API versions earlier than 2015-09-08
if ($code == 'rate_limit') {
throw new Error\RateLimit($msg, $param, $rcode, $rbody, $resp, $rheaders);
}

// intentional fall-through
case 404:
throw new Error\InvalidRequest($msg, $param, $rcode, $rbody, $resp, $rheaders);
case 401:
throw new Error\Authentication($msg, $rcode, $rbody, $resp, $rheaders);
case 402:
throw new Error\Card($msg, $param, $code, $rcode, $rbody, $resp, $rheaders);
case 429:
throw new Error\RateLimit($msg, $param, $rcode, $rbody, $resp, $rheaders);
default:
throw new Error\Api($msg, $rcode, $rbody, $resp, $rheaders);
}
}

private function _requestRaw($method, $url, $params, $headers)
{
$myApiKey = $this->_apiKey;
if (!$myApiKey) {
$myApiKey = Stripe::$apiKey;
}

if (!$myApiKey) {
$msg = 'No API key provided. (HINT: set your API key using '
. '"Stripe::setApiKey(<API-KEY>)". You can generate API keys from '
. 'the Stripe web interface. See https://stripe.com/api for '
. 'details, or email [email protected] if you have any questions.';
throw new Error\Authentication($msg);
}

$absUrl = $this->_apiBase.$url;
$params = self::_encodeObjects($params);
$langVersion = phpversion();
$uname = php_uname();
$ua = array(
'bindings_version' => Stripe::VERSION,
'lang' => 'php',
'lang_version' => $langVersion,
'publisher' => 'stripe',
'uname' => $uname,
);
$defaultHeaders = array(
'X-Stripe-Client-User-Agent' => json_encode($ua),
'User-Agent' => 'Stripe/v1 PhpBindings/' . Stripe::VERSION,
'Authorization' => 'Bearer ' . $myApiKey,
);
if (Stripe::$apiVersion) {
$defaultHeaders['Stripe-Version'] = Stripe::$apiVersion;
}

if (Stripe::$accountId) {
$defaultHeaders['Stripe-Account'] = Stripe::$accountId;
}

$hasFile = false;
$hasCurlFile = class_exists('\CURLFile', false);
foreach ($params as $k => $v) {
if (is_resource($v)) {
$hasFile = true;
$params[$k] = self::_processResourceParam($v, $hasCurlFile);
} elseif ($hasCurlFile && $v instanceof \CURLFile) {
$hasFile = true;
}
}

if ($hasFile) {
$defaultHeaders['Content-Type'] = 'multipart/form-data';
} else {
$defaultHeaders['Content-Type'] = 'application/x-www-form-urlencoded';
}

$combinedHeaders = array_merge($defaultHeaders, $headers);
$rawHeaders = array();

foreach ($combinedHeaders as $header => $value) {
$rawHeaders[] = $header . ': ' . $value;
}

list($rbody, $rcode, $rheaders) = $this->httpClient()->request(
$method,
$absUrl,
$rawHeaders,
$params,
$hasFile
);
return array($rbody, $rcode, $rheaders, $myApiKey);
}

private function _processResourceParam($resource, $hasCurlFile)
{
if (get_resource_type($resource) !== 'stream') {
throw new Error\Api(
'Attempted to upload a resource that is not a stream'
);
}

$metaData = stream_get_meta_data($resource);
if ($metaData['wrapper_type'] !== 'plainfile') {
throw new Error\Api(
'Only plainfile resource streams are supported'
);
}

if ($hasCurlFile) {
// We don't have the filename or mimetype, but the API doesn't care
return new \CURLFile($metaData['uri']);
} else {
return '@'.$metaData['uri'];
}
}

private function _interpretResponse($rbody, $rcode, $rheaders)
{
try {
$resp = json_decode($rbody, true);
} catch (Exception $e) {
$msg = "Invalid response body from API: $rbody "
. "(HTTP response code was $rcode)";
throw new Error\Api($msg, $rcode, $rbody);
}

if ($rcode < 200 || $rcode >= 300) {
$this->handleApiError($rbody, $rcode, $rheaders, $resp);
}
return $resp;
}

public static function setHttpClient($client)
{
self::$_httpClient = $client;
}

private function httpClient()
{
if (!self::$_httpClient) {
self::$_httpClient = HttpClient\CurlClient::instance();
}
return self::$_httpClient;
}
}
Loading