Version 4.5.0
This commit is contained in:
1 parent
1a0c7fd3c2
commit
f79dcf067a
3791 files changed
+248032
-76372
No files matched your search
@@ -12,7 +12,7 @@
|
||||
namespace IPS\nexus\Customer;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
@@ -48,7 +48,7 @@ class _CreditCard extends \IPS\Patterns\ActiveRecord
|
||||
public static function constructFromData( $data, $updateMultitonStoreIfExists = TRUE )
|
||||
{
|
||||
$gateway = \IPS\nexus\Gateway::load( $data['card_method'] );
|
||||
$classname = 'IPS\nexus\Gateway\\' . $gateway->gateway . '\\CreditCard';
|
||||
$classname = \IPS\nexus\Gateway::gateways()[ $gateway->gateway ] . '\\CreditCard';
|
||||
|
||||
/* Initiate an object */
|
||||
$obj = new $classname;
|
||||
@@ -84,19 +84,58 @@ class _CreditCard extends \IPS\Patterns\ActiveRecord
|
||||
* Add Form
|
||||
*
|
||||
* @param \IPS\nexus\Customer $customer The customer
|
||||
* @param bool $admin Set to TRUE if the *admin* (opposed to the customer themselves) wants to create a new payment method
|
||||
* @return \IPS\Helpers\Form|\IPS\nexus\CreditCard
|
||||
*/
|
||||
public static function create( \IPS\nexus\Customer $customer )
|
||||
public static function create( \IPS\nexus\Customer $customer, $admin )
|
||||
{
|
||||
$form = new \IPS\Helpers\Form;
|
||||
$gateways = \IPS\nexus\Gateway::cardStorageGateways();
|
||||
$showSubmitButton = FALSE;
|
||||
$hiddenValues = array();
|
||||
foreach ( static::createFormElements( $customer, $admin, $showSubmitButton, $hiddenValues ) as $element )
|
||||
{
|
||||
$form->add( $element );
|
||||
}
|
||||
foreach ( $hiddenValues as $k => $v )
|
||||
{
|
||||
$form->hiddenValues[ $k ] = $v;
|
||||
}
|
||||
|
||||
if ( $values = $form->values() )
|
||||
{
|
||||
try
|
||||
{
|
||||
return static::createFormSubmit( $values, $customer, $admin );
|
||||
}
|
||||
catch ( \DomainException $e )
|
||||
{
|
||||
$form->error = $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
return $form->customTemplate( array( \IPS\Theme::i()->getTemplate( 'forms', 'nexus', 'global' ), 'addPaymentMethodForm' ), $showSubmitButton );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get form elements for creating a new card
|
||||
*
|
||||
* @param \IPS\nexus\Customer $customer The customer
|
||||
* @param bool $admin Set to TRUE if the *admin* (opposed to the customer themselves) wants to create a new payment method
|
||||
* @param bool $showSubmitButton Will be set to a bool indicating if the submit button should be shown
|
||||
* @param array $hiddenValues Hidden values to add to the form
|
||||
* @return array
|
||||
*/
|
||||
public static function createFormElements( \IPS\nexus\Customer $customer, $admin, &$showSubmitButton, &$hiddenValues )
|
||||
{
|
||||
$gateways = \IPS\nexus\Gateway::cardStorageGateways( $admin );
|
||||
|
||||
$elements = array();
|
||||
$paymentMethodsToggles = array();
|
||||
foreach ( $gateways as $gateway )
|
||||
{
|
||||
$invoice = new \IPS\nexus\Invoice;
|
||||
foreach ( $gateway->paymentScreen( $invoice, $invoice->total, $customer ) as $element )
|
||||
$invoice->currency = $customer->defaultCurrency();
|
||||
foreach ( $gateway->paymentScreen( $invoice, $invoice->total, $customer, array(), 'card' ) as $element )
|
||||
{
|
||||
if ( !$element->htmlId )
|
||||
{
|
||||
@@ -111,61 +150,90 @@ class _CreditCard extends \IPS\Patterns\ActiveRecord
|
||||
}
|
||||
}
|
||||
|
||||
if ( count( $gateways ) > 1 )
|
||||
if ( \count( $gateways ) > 1 )
|
||||
{
|
||||
$showSubmitButton = FALSE;
|
||||
$options = array();
|
||||
$toggles = array();
|
||||
foreach ( $gateways as $gateway )
|
||||
{
|
||||
$options[ $gateway->id ] = $gateway->_title;
|
||||
}
|
||||
if ( $gateway->showSubmitButton() )
|
||||
{
|
||||
$showSubmitButton = TRUE;
|
||||
$paymentMethodsToggles[ $gateway->id ][] = 'paymentMethodSubmit';
|
||||
}
|
||||
}
|
||||
|
||||
$element = new \IPS\Helpers\Form\Radio( 'payment_method', NULL, TRUE, array( 'options' => $options, 'toggles' => $paymentMethodsToggles ) );
|
||||
$element->label = \IPS\Member::loggedIn()->language()->addToStack('card_gateway');
|
||||
$form->add( $element );
|
||||
$element->label = \IPS\Member::loggedIn()->language()->addToStack('card_gateway');
|
||||
|
||||
array_unshift( $elements, $element );
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach ( $gateways as $gateway )
|
||||
{
|
||||
$form->hiddenValues['payment_method'] = $gateway->id;
|
||||
$hiddenValues['payment_method'] = $gateway->id;
|
||||
$showSubmitButton = $gateway->showSubmitButton();
|
||||
}
|
||||
}
|
||||
|
||||
foreach ( $elements as $element )
|
||||
return $elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle submission of the form for creating a new card
|
||||
*
|
||||
* @param array $values Values from the form
|
||||
* @param \IPS\nexus\Customer $customer The customer
|
||||
* @param bool $admin Set to TRUE if the *admin* (opposed to the customer themselves) wants to create a new payment method
|
||||
* @param ?\IPS\nexus\Invoice $invoice If customer is a guest, will sa`ve the guest data onto the provided invoice
|
||||
* @return \IPS\nexus\CreditCard
|
||||
* @throws \DomainException
|
||||
*/
|
||||
public static function createFormSubmit( $values, $customer, $admin, \IPS\nexus\Invoice $invoice = NULL )
|
||||
{
|
||||
if ( isset( $values['payment_method'] ) )
|
||||
{
|
||||
$form->add( $element );
|
||||
if ( $values['payment_method'] != 0 )
|
||||
{
|
||||
$gateway = \IPS\nexus\Gateway::load( $values['payment_method'] );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$gateways = \IPS\nexus\Gateway::cardStorageGateways( $admin );
|
||||
$gateway = array_pop( $gateways );
|
||||
}
|
||||
|
||||
if ( $values = $form->values() )
|
||||
{
|
||||
if ( !$values[ \IPS\Request::i()->payment_method . '_card' ] )
|
||||
if ( !$values[ $gateway->id . '_card' ] )
|
||||
{
|
||||
throw new \DomainException( \IPS\Member::loggedIn()->language()->addToStack('card_number_invalid') );
|
||||
}
|
||||
else
|
||||
{
|
||||
$classname = \IPS\nexus\Gateway::gateways()[ $gateway->gateway ] . '\\CreditCard';
|
||||
$card = new $classname;
|
||||
$card->member = $customer;
|
||||
$card->method = $gateway;
|
||||
|
||||
if ( \is_array( $values[ $gateway->id . '_card' ] ) )
|
||||
{
|
||||
$form->error = \IPS\Member::loggedIn()->language()->addToStack('card_number_invalid');
|
||||
$_card = new \IPS\nexus\CreditCard;
|
||||
$_card->token = $values[ $gateway->id . '_card' ]['token'];
|
||||
$card->set_card( $_card, $invoice );
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
$gateway = \IPS\nexus\Gateway::load( \IPS\Request::i()->payment_method );
|
||||
$classname = 'IPS\nexus\Gateway\\' . $gateway->gateway . '\CreditCard';
|
||||
$card = new $classname;
|
||||
$card->member = $customer;
|
||||
$card->method = $gateway;
|
||||
$card->card = $values[ $gateway->id . '_card' ];
|
||||
$card->save();
|
||||
|
||||
$customer->log( 'card', array( 'type' => 'add', 'number' => $card->card->lastFour ) );
|
||||
|
||||
return $card;
|
||||
}
|
||||
catch ( \DomainException $e )
|
||||
{
|
||||
$form->error = $e->getMessage();
|
||||
}
|
||||
$card->set_card( $values[ $gateway->id . '_card' ], $invoice );
|
||||
}
|
||||
$card->save();
|
||||
|
||||
$customer->log( 'card', array( 'type' => 'add', 'number' => $card->card->lastFour ) );
|
||||
|
||||
return $card;
|
||||
}
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -181,12 +249,12 @@ class _CreditCard extends \IPS\Patterns\ActiveRecord
|
||||
/**
|
||||
* Set member
|
||||
*
|
||||
* @param \IPS\Member
|
||||
* @param \IPS\Member $member Member
|
||||
* @return void
|
||||
*/
|
||||
public function set_member( \IPS\Member $member )
|
||||
{
|
||||
$this->_data['member'] = $member->member_id;
|
||||
$this->_data['member'] = $member->member_id ?: 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -202,7 +270,7 @@ class _CreditCard extends \IPS\Patterns\ActiveRecord
|
||||
/**
|
||||
* Set payment gateway
|
||||
*
|
||||
* @param \IPS\nexus\Gateway
|
||||
* @param \IPS\nexus\Gateway $gateway Payment gateway
|
||||
* @return void
|
||||
*/
|
||||
public function set_method( \IPS\nexus\Gateway $gateway )
|
||||
|
||||
Reference in new issue
Block a user