Version 4.0.13.1
This commit is contained in:
1 parent
f9e857d255
commit
c6a86f0d9b
7517 files changed
+597400
-668041
No files matched your search
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
/**
|
||||
* @brief PayPal Stored Card
|
||||
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
|
||||
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
|
||||
* @license http://www.invisionpower.com/legal/standards/
|
||||
* @package IPS Social Suite
|
||||
* @subpackage Nexus
|
||||
* @since 10 Feb 2014
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
|
||||
namespace IPS\nexus\Gateway\Stripe;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stripe Stored Card
|
||||
*/
|
||||
class _CreditCard extends \IPS\nexus\Customer\CreditCard
|
||||
{
|
||||
/**
|
||||
* @brief Card
|
||||
*/
|
||||
protected $_card;
|
||||
|
||||
/**
|
||||
* Get card
|
||||
*
|
||||
* @return \IPS\nexus\CreditCard
|
||||
*/
|
||||
public function get_card()
|
||||
{
|
||||
if ( !$this->_card )
|
||||
{
|
||||
$profiles = $this->member->cm_profiles;
|
||||
if ( !isset( $profiles[ $this->method->id ] ) )
|
||||
{
|
||||
throw new \UnexpectedValueException;
|
||||
}
|
||||
|
||||
$response = $this->method->api( "customers/{$profiles[ $this->method->id ]}/cards/{$this->data}", NULL, 'get' );
|
||||
|
||||
$this->_card = new \IPS\nexus\CreditCard;
|
||||
$this->_card->lastFour = $response['last4'];
|
||||
switch ( $response['type'] )
|
||||
{
|
||||
case 'Visa':
|
||||
$this->_card->type = \IPS\nexus\CreditCard::TYPE_VISA;
|
||||
break;
|
||||
case 'American Express':
|
||||
$this->_card->type = \IPS\nexus\CreditCard::TYPE_AMERICAN_EXPRESS;
|
||||
break;
|
||||
case 'MasterCard':
|
||||
$this->_card->type = \IPS\nexus\CreditCard::TYPE_MASTERCARD;
|
||||
break;
|
||||
case 'Discover':
|
||||
$this->_card->type = \IPS\nexus\CreditCard::TYPE_DISCOVER;
|
||||
break;
|
||||
case 'JCB':
|
||||
$this->_card->type = \IPS\nexus\CreditCard::TYPE_JCB;
|
||||
break;
|
||||
case 'Diners Club':
|
||||
$this->_card->type = \IPS\nexus\CreditCard::TYPE_DINERS_CLUB;
|
||||
break;
|
||||
}
|
||||
$this->_card->expMonth = $response['exp_month'];
|
||||
$this->_card->expYear = $response['exp_year'];
|
||||
}
|
||||
return $this->_card;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set card
|
||||
*
|
||||
* @param \IPS\nexus\CreditCard $card The card
|
||||
* @return void
|
||||
*/
|
||||
public function set_card( \IPS\nexus\CreditCard $card )
|
||||
{
|
||||
$profiles = $this->member->cm_profiles;
|
||||
|
||||
if ( !isset( $profiles[ $this->method->id ] ) )
|
||||
{
|
||||
$response = $this->method->api( 'customers', array( 'metadata' => array( 'id' => $this->member->member_id ) ) );
|
||||
$profiles[ $this->method->id ] = $response['id'];
|
||||
$this->member->cm_profiles = $profiles;
|
||||
$this->member->save();
|
||||
}
|
||||
|
||||
$response = $this->method->api( "customers/{$profiles[ $this->method->id ]}/cards", array(
|
||||
'card' => $card->token
|
||||
) );
|
||||
$this->data = $response['id'];
|
||||
|
||||
$this->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function delete()
|
||||
{
|
||||
$profiles = $this->member->cm_profiles;
|
||||
try
|
||||
{
|
||||
$this->method->api( "customers/{$profiles[ $this->method->id ]}/cards/{$this->data}", NULL, 'delete' );
|
||||
}
|
||||
catch ( \IPS\nexus\Gateway\Stripe\Exception $e ) { }
|
||||
return parent::delete();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
/**
|
||||
* @brief Stripe Exception
|
||||
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
|
||||
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
|
||||
* @license http://www.invisionpower.com/legal/standards/
|
||||
* @package IPS Social Suite
|
||||
* @subpackage Nexus
|
||||
* @since 10 Mar 2014
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
|
||||
namespace IPS\nexus\Gateway\Stripe;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stripe Exception
|
||||
*/
|
||||
class _Exception extends \DomainException
|
||||
{
|
||||
/**
|
||||
* @brief Details
|
||||
*/
|
||||
public $details;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $error Error details
|
||||
* @return void
|
||||
*/
|
||||
public function __construct( array $response )
|
||||
{
|
||||
$this->details = $response;
|
||||
if ( $response['type'] == 'card_error' )
|
||||
{
|
||||
parent::__construct( $response['message'] );
|
||||
}
|
||||
else
|
||||
{
|
||||
parent::__construct( \IPS\Member::loggedIn()->language()->get( 'gateway_err' ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
<?php
|
||||
/**
|
||||
* @brief Stripe Pay Out Gateway
|
||||
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
|
||||
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
|
||||
* @license http://www.invisionpower.com/legal/standards/
|
||||
* @package IPS Social Suite
|
||||
* @subpackage Nexus
|
||||
* @since 7 Apr 2014
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
|
||||
namespace IPS\nexus\Gateway\Stripe;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stripe Pay Out Gateway
|
||||
*/
|
||||
class _Payout extends \IPS\nexus\Payout
|
||||
{
|
||||
/**
|
||||
* ACP Settings
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function settings()
|
||||
{
|
||||
$settings = json_decode( \IPS\Settings::i()->nexus_payout, TRUE );
|
||||
if ( !isset( $settings['Stripe'] ) or !count( $settings['Stripe'] ) )
|
||||
{
|
||||
try
|
||||
{
|
||||
$method = \IPS\Db::i()->select( '*', 'nexus_paymethods', array( 'm_gateway=? AND m_active=1', 'Stripe' ) )->first();
|
||||
$_settings = json_decode( $method['m_settings'], TRUE );
|
||||
$settings['Stripe']['secret_key'] = $_settings['secret_key'];
|
||||
}
|
||||
catch ( \UnderflowException $e ) { }
|
||||
}
|
||||
|
||||
return array( new \IPS\Helpers\Form\Text( 'stripe_secret_key', ( isset( $settings['Stripe'] ) and isset( $settings['Stripe']['secret_key'] ) ) ? $settings['Stripe']['secret_key'] : NULL ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Payout Form
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function form()
|
||||
{
|
||||
$return = array();
|
||||
|
||||
/* Get any details we've used before */
|
||||
$previous = \IPS\Db::i()->select( 'po_data', 'nexus_payouts', array( 'po_member=? AND po_gateway=?', \IPS\Member::loggedIn()->member_id, 'Stripe' ) );
|
||||
if ( count( $previous ) )
|
||||
{
|
||||
$options = array();
|
||||
foreach ( $previous as $id )
|
||||
{
|
||||
$recipient = static::api( "recipients/{$id}", NULL, 'get' );
|
||||
$options[ $id ] = "{$recipient['active_account']['bank_name']} - XXXX{$recipient['active_account']['last4']}";
|
||||
}
|
||||
$options[0] = 'other';
|
||||
$return[] = new \IPS\Helpers\Form\Radio( 'stripe_recipient', NULL, NULL, array( 'options' => $options, 'toggles' => array( 0 => array( 'stripe_country', 'stripe_routing_number', 'stripe_account_number', 'stripe_account_type' ) ) ) );
|
||||
}
|
||||
|
||||
/* Required Validation */
|
||||
$requiredValidation = function( $val )
|
||||
{
|
||||
if ( !$val and \IPS\Request::i()->withdraw_method === 'Stripe' and isset( \IPS\Request::i()->stripe_recipient ) and !\IPS\Request::i()->stripe_recipient )
|
||||
{
|
||||
throw new \DomainException('form_required');
|
||||
}
|
||||
};
|
||||
|
||||
/* Build form for new */
|
||||
return array_merge( $return, array(
|
||||
'country' => new \IPS\Helpers\Form\Select( 'stripe_country', 'US', NULL, array( 'options' => array_map( function( $val )
|
||||
{
|
||||
return 'country-' . $val;
|
||||
}, array_combine( \IPS\GeoLocation::$countries, \IPS\GeoLocation::$countries ) ) ), NULL, NULL, NULL, 'stripe_country' ),
|
||||
'routing_number' => new \IPS\Helpers\Form\Text( 'stripe_routing_number', NULL, NULL, array(), $requiredValidation, NULL, NULL, 'stripe_routing_number' ),
|
||||
'account_number' => new \IPS\Helpers\Form\Text( 'stripe_account_number', NULL, NULL, array(), $requiredValidation, NULL, NULL, 'stripe_account_number' ),
|
||||
'account_type' => new \IPS\Helpers\Form\Radio( 'stripe_account_type', 'individual', NULL, array(
|
||||
'options' => array(
|
||||
'individual' => 'stripe_account_type_individual',
|
||||
'corporation' => 'stripe_account_type_corporation'
|
||||
),
|
||||
'toggles' => array(
|
||||
'individual' => array( 'stripe_individual_name' ),
|
||||
'corporation' => array( 'stripe_corporation_name' )
|
||||
)
|
||||
), NULL, NULL, NULL, 'stripe_account_type' ),
|
||||
'individual_name' => new \IPS\Helpers\Form\Text( 'stripe_individual_name', \IPS\nexus\Customer::loggedIn()->cm_name, NULL, array(), function( $val )
|
||||
{
|
||||
if ( !$val and \IPS\Request::i()->withdraw_method === 'Stripe' and isset( \IPS\Request::i()->stripe_recipient ) and !\IPS\Request::i()->stripe_recipient and \IPS\Request::i()->stripe_account_type === 'individual' )
|
||||
{
|
||||
throw new \DomainException('form_required');
|
||||
}
|
||||
}, NULL, NULL, 'stripe_individual_name' ),
|
||||
'corporation_name' => new \IPS\Helpers\Form\Text( 'stripe_corporation_name', NULL, NULL, array(), function( $val )
|
||||
{
|
||||
if ( !$val and \IPS\Request::i()->withdraw_method === 'Stripe' and isset( \IPS\Request::i()->stripe_recipient ) and !\IPS\Request::i()->stripe_recipient and \IPS\Request::i()->stripe_account_type === 'corporation' )
|
||||
{
|
||||
throw new \DomainException('form_required');
|
||||
}
|
||||
}, NULL, NULL, 'stripe_corporation_name' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get data and validate
|
||||
*
|
||||
* @param array $values Values from form
|
||||
* @return mixed
|
||||
* @throws \DomainException
|
||||
*/
|
||||
public function getData( array $values )
|
||||
{
|
||||
if ( isset( $values['stripe_recipient'] ) and $values['stripe_recipient'] )
|
||||
{
|
||||
return $values['stripe_recipient'];
|
||||
}
|
||||
|
||||
$recipient = static::api( 'recipients', array(
|
||||
'name' => $values['stripe_account_type'] === 'individual' ? $values['stripe_individual_name'] : $values['stripe_corporation_name'],
|
||||
'type' => $values['stripe_account_type'],
|
||||
'bank_account' => array(
|
||||
'country' => $values['stripe_country'],
|
||||
'routing_number' => $values['stripe_routing_number'],
|
||||
'account_number' => $values['stripe_account_number']
|
||||
),
|
||||
'metadata' => array(
|
||||
'id' => \IPS\Member::loggedIn()->member_id
|
||||
),
|
||||
) );
|
||||
|
||||
return $recipient['id'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Process
|
||||
*
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function process()
|
||||
{
|
||||
$response = static::api( 'transfers', array(
|
||||
'amount' => $this->amount->amount,
|
||||
'currency' => $this->amount->currency,
|
||||
'recipient' => $this->data
|
||||
) );
|
||||
|
||||
$this->status = static::STATUS_COMPLETE;
|
||||
$this->completed = new \IPS\DateTime;
|
||||
$this->gw_id = $response['id'];
|
||||
$this->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get recipient
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function recipient()
|
||||
{
|
||||
return static::api( "recipients/{$this->data}", NULL, 'get' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Send API Request
|
||||
*
|
||||
* @param string $uri The API to request (e.g. "charges")
|
||||
* @param array $data The data to send
|
||||
* @param string $method Method (get/post)
|
||||
* @return array
|
||||
* @throws \IPS\Http|Exception
|
||||
* @throws \IPS\nexus\Gateway\PayPal\Exception
|
||||
*/
|
||||
protected static function api( $uri, $data=NULL, $method='post' )
|
||||
{
|
||||
$settings = json_decode( \IPS\Settings::i()->nexus_payout, TRUE );
|
||||
|
||||
$response = \IPS\Http\Url::external( 'https://api.stripe.com/v1/' . $uri )
|
||||
->request()
|
||||
->setHeaders( array( 'Stripe-Version' => '2014-01-31' ) )
|
||||
->login( $settings['Stripe']['secret_key'], '' )
|
||||
->$method( $data )
|
||||
->decodeJson();
|
||||
|
||||
if ( isset( $response['error'] ) )
|
||||
{
|
||||
throw new \DomainException( $response['error']['message'] );
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,336 @@
|
||||
<?php
|
||||
/**
|
||||
* @brief Stripe Gateway
|
||||
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
|
||||
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
|
||||
* @license http://www.invisionpower.com/legal/standards/
|
||||
* @package IPS Social Suite
|
||||
* @subpackage Nexus
|
||||
* @since 13 Mar 2014
|
||||
* @version SVN_VERSION_NUMBER
|
||||
*/
|
||||
|
||||
namespace IPS\nexus\Gateway;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stripe Gateway
|
||||
*/
|
||||
class _Stripe extends \IPS\nexus\Gateway
|
||||
{
|
||||
/* !Features */
|
||||
|
||||
const SUPPORTS_REFUNDS = TRUE;
|
||||
const SUPPORTS_PARTIAL_REFUNDS = TRUE;
|
||||
|
||||
/**
|
||||
* Check the gateway can process this...
|
||||
*
|
||||
* @param $amount \IPS\nexus\Money The amount
|
||||
* @param $billingAddress \IPS\GeoLocation The billing address
|
||||
* @return bool
|
||||
* @see <a href="https://support.stripe.com/questions/which-currencies-does-stripe-support/">Which currencies does Stripe support?</a>
|
||||
*/
|
||||
public function checkValidity( \IPS\nexus\Money $amount, \IPS\GeoLocation $billingAddress )
|
||||
{
|
||||
if ( static::_amountAsCents( $amount ) < 50 )
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
$settings = json_decode( $this->settings, TRUE );
|
||||
switch ( $settings['country'] )
|
||||
{
|
||||
case 'CA':
|
||||
if ( !in_array( $amount->currency, array( 'CAD', 'USD' ) ) )
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'AU':
|
||||
if( $amount->currency != 'AUD' )
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
if ( !in_array( $amount->currency, array( 'AED', 'AFN', 'ALL', 'AMD', 'ANG', 'AOA', 'ARS', 'AUD', 'AWG', 'AZN', 'BAM', 'BBD', 'BDT', 'BGN', 'BIF', 'BMD', 'BND', 'BOB', 'BRL', 'BSD', 'BWP', 'BZD', 'CAD', 'CDF', 'CHF', 'CLP', 'CNY', 'COP', 'CRC', 'CVE', 'CZK', 'DJF', 'DKK', 'DOP', 'DZD', 'EEK', 'EGP', 'ETB', 'EUR', 'FJD', 'FKP', 'GBP', 'GEL', 'GIP', 'GMD', 'GNF', 'GTQ', 'GYD', 'HKD', 'HNL', 'HRK', 'HTG', 'HUF', 'IDR', 'ILS', 'INR', 'ISK', 'JMD', 'JPY', 'KES', 'KGS', 'KHR', 'KMF', 'KRW', 'KYD', 'KZT', 'LAK', 'LBP', 'LKR', 'LRD', 'LSL', 'LTL', 'LVL', 'MAD', 'MDL', 'MGA', 'MKD', 'MNT', 'MOP', 'MRO', 'MUR', 'MVR', 'MWK', 'MXN', 'MYR', 'MZN', 'NAD', 'NGN', 'NIO', 'NOK', 'NPR', 'NZD', 'PAB', 'PEN', 'PGK', 'PHP', 'PKR', 'PLN', 'PYG', 'QAR', 'RON', 'RSD', 'RUB', 'RWF', 'SAR', 'SBD', 'SCR', 'SEK', 'SGD', 'SHP', 'SLL', 'SOS', 'SRD', 'STD', 'SVC', 'SZL', 'THB', 'TJS', 'TOP', 'TRY', 'TTD', 'TWD', 'TZS', 'UAH', 'UGX', 'USD', 'UYI', 'UZS', 'VEF', 'VND', 'VUV', 'WST', 'XAF', 'XCD', 'XOF', 'XPF', 'YER', 'ZAR', 'ZMW' ) ) )
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return parent::checkValidity( $amount, $billingAddress );
|
||||
}
|
||||
|
||||
/**
|
||||
* Can store cards?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function canStoreCards()
|
||||
{
|
||||
$settings = json_decode( $this->settings, TRUE );
|
||||
return ( $settings['cards'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Admin can manually charge using this gateway?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function canAdminCharge()
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/* !Payment Gateway */
|
||||
|
||||
/**
|
||||
* Payment Screen Fields
|
||||
*
|
||||
* @param \IPS\nexus\Invoice $invoice Invoice
|
||||
* @param \IPS\nexus\Money $amount The amount to pay now
|
||||
* @return array
|
||||
*/
|
||||
public function paymentScreen( \IPS\nexus\Invoice $invoice, \IPS\nexus\Money $amount )
|
||||
{
|
||||
\IPS\Output::i()->jsFiles = array_merge( \IPS\Output::i()->jsFiles, \IPS\Output::i()->js( 'global_gateways.js', 'nexus', 'global' ) );
|
||||
|
||||
$settings = json_decode( $this->settings, TRUE );
|
||||
|
||||
$supportedCards = array( \IPS\nexus\CreditCard::TYPE_VISA, \IPS\nexus\CreditCard::TYPE_MASTERCARD );
|
||||
if ( !in_array( $invoice->currency, array( 'AFN', 'AOA', 'ARS', 'AUD', 'BOB', 'BRL', 'CAD', 'CLP', 'COP', 'CRC', 'CVE', 'CZK', 'DJF', 'EEK', 'FKP', 'GNF', 'GTQ', 'HNL', 'INR', 'LAK', 'MUR', 'MXN', 'NIO', 'PAB', 'PEN', 'PYG', 'SHP', 'SRD', 'SVC', 'UYI', 'VEF', 'XOF', 'XPF' ) ) )
|
||||
{
|
||||
$supportedCards[] = \IPS\nexus\CreditCard::TYPE_AMERICAN_EXPRESS;
|
||||
}
|
||||
if ( $settings['country'] == 'US' )
|
||||
{
|
||||
$supportedCards[] = \IPS\nexus\CreditCard::TYPE_DISCOVER;
|
||||
$supportedCards[] = \IPS\nexus\CreditCard::TYPE_DINERS_CLUB;
|
||||
$supportedCards[] = \IPS\nexus\CreditCard::TYPE_JCB;
|
||||
}
|
||||
|
||||
return array( 'card' => new \IPS\nexus\Form\CreditCard( $this->id . '_card', NULL, FALSE, array(
|
||||
'types' => $supportedCards,
|
||||
'attr' => array(
|
||||
'data-controller' => 'nexus.global.gateways.stripe',
|
||||
'data-id' => $this->id,
|
||||
'class' => 'ipsHide',
|
||||
'data-key' => $settings['publishable_key']
|
||||
),
|
||||
'jsRequired' => TRUE,
|
||||
'names' => FALSE,
|
||||
'save' => ( $settings['cards'] and \IPS\Member::loggedIn()->member_id ) ? $this : NULL,
|
||||
'member' => $invoice->member,
|
||||
) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Authorize
|
||||
*
|
||||
* @param \IPS\nexus\Transaction $transaction Transaction
|
||||
* @param array|\IPS\nexus\Customer\CreditCard $values Values from form OR a stored card object if this gateway supports them
|
||||
* @param \IPS\nexus\Fraud\MaxMind\Request|NULL $maxMind *If* MaxMind is enabled, the request object will be passed here so gateway can additional data before request is made
|
||||
* @return \IPS\DateTime|NULL Auth is valid until or NULL to indicate auth is good forever
|
||||
* @throws \LogicException Message will be displayed to user
|
||||
*/
|
||||
public function auth( \IPS\nexus\Transaction $transaction, $values, \IPS\nexus\Fraud\MaxMind\Request $maxMind = NULL )
|
||||
{
|
||||
/* Set MaxMind type */
|
||||
if ( $maxMind )
|
||||
{
|
||||
$maxMind->setTransactionType('creditcard');
|
||||
}
|
||||
|
||||
/* Build data */
|
||||
$data = array(
|
||||
'amount' => static::_amountAsCents( $transaction->amount ),
|
||||
'currency' => $transaction->amount->currency,
|
||||
'capture' => 'false'
|
||||
);
|
||||
$card = $values[ $this->id . '_card' ];
|
||||
|
||||
/* Stored Card */
|
||||
if ( $card instanceof \IPS\nexus\Gateway\Stripe\CreditCard )
|
||||
{
|
||||
$profiles = $card->member->cm_profiles;
|
||||
$data['customer'] = $profiles[ $this->id ];
|
||||
$data['card'] = $values[ $this->id . '_card' ]->data;
|
||||
}
|
||||
|
||||
/* New Card */
|
||||
else
|
||||
{
|
||||
/* Are we saving it? */
|
||||
$settings = json_decode( $this->settings, TRUE );
|
||||
if ( $settings['cards'] and $card->save )
|
||||
{
|
||||
$storedCard = new \IPS\nexus\Gateway\Stripe\CreditCard;
|
||||
$storedCard->member = $transaction->member;
|
||||
$storedCard->method = $this;
|
||||
$storedCard->card = $card;
|
||||
$storedCard->save();
|
||||
|
||||
$profiles = $transaction->member->cm_profiles;
|
||||
$data['customer'] = $profiles[ $this->id ];
|
||||
$data['card'] = $storedCard->data;
|
||||
}
|
||||
/* Nope, just use the token */
|
||||
else
|
||||
{
|
||||
$data['card'] = $card->token;
|
||||
}
|
||||
}
|
||||
|
||||
/* Authorize */
|
||||
$response = $this->api( 'charges', $data );
|
||||
$transaction->gw_id = $response['id'];
|
||||
|
||||
/* Return */
|
||||
return \IPS\DateTime::ts( $response['created'] )->add( new \DateInterval( 'P7D' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Void
|
||||
*
|
||||
* @param \IPS\nexus\Transaction $transaction Transaction
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function void( \IPS\nexus\Transaction $transaction )
|
||||
{
|
||||
try
|
||||
{
|
||||
$response = $this->refund( $transaction );
|
||||
}
|
||||
catch ( \Exception $e ) { }
|
||||
}
|
||||
|
||||
/**
|
||||
* Capture
|
||||
*
|
||||
* @param \IPS\nexus\Transaction $transaction Transaction
|
||||
* @return void
|
||||
* @throws \LogicException
|
||||
*/
|
||||
public function capture( \IPS\nexus\Transaction $transaction )
|
||||
{
|
||||
$this->api( "charges/{$transaction->gw_id}/capture" );
|
||||
}
|
||||
|
||||
/**
|
||||
* Refund
|
||||
*
|
||||
* @param \IPS\nexus\Transaction $transaction Transaction to be refunded
|
||||
* @param float|NULL $amount Amount to refund (NULL for full amount - always in same currency as transaction)
|
||||
* @return mixed Gateway reference ID for refund, if applicable
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function refund( \IPS\nexus\Transaction $transaction, $amount = NULL )
|
||||
{
|
||||
$data = NULL;
|
||||
if ( $amount )
|
||||
{
|
||||
$data['amount'] = static::_amountAscents( new \IPS\nexus\Money( $amount, $transaction->currency ) );
|
||||
}
|
||||
|
||||
$this->api( "charges/{$transaction->gw_id}/refund", $data );
|
||||
}
|
||||
|
||||
/* !ACP Configuration */
|
||||
|
||||
/**
|
||||
* Settings
|
||||
*
|
||||
* @param \IPS\Helpers\Form $form The form
|
||||
* @return void
|
||||
*/
|
||||
public function settings( &$form )
|
||||
{
|
||||
$settings = json_decode( $this->settings, TRUE );
|
||||
$form->add( new \IPS\Helpers\Form\Text( 'stripe_secret_key', $settings ? $settings['secret_key'] : NULL, TRUE ) );
|
||||
$form->add( new \IPS\Helpers\Form\Text( 'stripe_publishable_key', $settings ? $settings['publishable_key'] : NULL, TRUE ) );
|
||||
$form->add( new \IPS\Helpers\Form\YesNo( 'stripe_cards', $settings ? $settings['cards'] : TRUE, TRUE ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Settings
|
||||
*
|
||||
* @param array $settings Settings
|
||||
* @return array
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function testSettings( $settings )
|
||||
{
|
||||
try
|
||||
{
|
||||
$response = $this->api( 'account', NULL, 'get', $settings );
|
||||
$settings['country'] = $response['country'];
|
||||
return $settings;
|
||||
}
|
||||
catch ( \IPS\nexus\Gateway\Stripe\Exception $e )
|
||||
{
|
||||
throw new \InvalidArgumentException( $e->details['message'] );
|
||||
}
|
||||
}
|
||||
|
||||
/* !Utility Methods */
|
||||
|
||||
/**
|
||||
* Send API Request
|
||||
*
|
||||
* @param string $uri The API to request (e.g. "charges")
|
||||
* @param array $data The data to send
|
||||
* @param string $method Method (get/post)
|
||||
* @param array|NULL $settings Settings (NULL for saved setting)
|
||||
* @return array
|
||||
* @throws \IPS\Http|Exception
|
||||
* @throws \IPS\nexus\Gateway\PayPal\Exception
|
||||
*/
|
||||
public function api( $uri, $data=NULL, $method='post', $settings = NULL )
|
||||
{
|
||||
$settings = $settings ?: json_decode( $this->settings, TRUE );
|
||||
|
||||
$response = \IPS\Http\Url::external( 'https://api.stripe.com/v1/' . $uri )
|
||||
->request()
|
||||
->setHeaders( array( 'Stripe-Version' => '2014-01-31' ) )
|
||||
->login( $settings['secret_key'], '' )
|
||||
->$method( $data )
|
||||
->decodeJson();
|
||||
|
||||
if ( isset( $response['error'] ) )
|
||||
{
|
||||
throw new \IPS\nexus\Gateway\Stripe\Exception( $response['error'] );
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert amount into cents
|
||||
*
|
||||
* @param \IPS\nexus\Money $amount The amount
|
||||
* @return int
|
||||
*/
|
||||
protected static function _amountAsCents( \IPS\nexus\Money $amount )
|
||||
{
|
||||
if ( in_array( $amount->currency, array( 'BIF', 'CLP', 'DJF', 'GNF', 'JPY', 'KMF', 'KRW', 'MGA', 'PYG', 'RWF', 'VUV', 'XAF', 'XOF', 'XPF' ) ) )
|
||||
{
|
||||
return intval( $amount->amount );
|
||||
}
|
||||
else
|
||||
{
|
||||
return intval( $amount->amount * 100 );
|
||||
}
|
||||
}
|
||||
}
|
||||
Whitespace-only changes.
Reference in new issue
Block a user