Skip to content

Commit

Permalink
[stripe] try to create a customer on post obtain token requestest.
Browse files Browse the repository at this point in the history
  • Loading branch information
makasim committed Mar 24, 2016
1 parent 30f9812 commit bcab34f
Show file tree
Hide file tree
Showing 2 changed files with 356 additions and 26 deletions.
60 changes: 41 additions & 19 deletions Extension/CreateCustomerExtension.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
<?php
namespace Payum\Stripe\Extension;

use Payum\Core\Action\GatewayAwareAction;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\Extension\Context;
use Payum\Core\Extension\ExtensionInterface;
use Payum\Core\GatewayInterface;
use Payum\Core\Request\Capture;
use Payum\Stripe\Constants;
use Payum\Stripe\Request\Api\CreateCharge;
use Payum\Stripe\Request\Api\CreateCustomer;
use Payum\Stripe\Request\Api\ObtainToken;

Expand All @@ -29,8 +27,45 @@ public function onPreExecute(Context $context)
if (false == $model instanceof \ArrayAccess) {
return;
}
$model = ArrayObject::ensureArrayObject($model);

$this->createCustomer($context->getGateway(), ArrayObject::ensureArrayObject($model));
}

/**
* @var Context $context
*/
public function onExecute(Context $context)
{
}

/**
* @var Context $context
*/
public function onPostExecute(Context $context)
{
/** @var Capture $request */
$request = $context->getRequest();
if (false == $request instanceof ObtainToken) {
return;
}

$model = $request->getModel();
if (false == $model instanceof \ArrayAccess) {
return;
}

$this->createCustomer($context->getGateway(), ArrayObject::ensureArrayObject($model));
}

/**
* @param GatewayInterface $gateway
* @param ArrayObject $model
*/
protected function createCustomer(GatewayInterface $gateway, ArrayObject $model)
{
if ($model['customer']) {
return;
}
if (false == ($model['card'] && is_string($model['card']))) {
return;
}
Expand All @@ -42,7 +77,8 @@ public function onPreExecute(Context $context)

$customer = $local->getArray('customer');
$customer['card'] = $model['card'];
$context->getGateway()->execute(new CreateCustomer($customer));

$gateway->execute(new CreateCustomer($customer));

$local['customer'] = $customer->toUnsafeArray();
$model['local'] = $local->toUnsafeArray();
Expand All @@ -54,18 +90,4 @@ public function onPreExecute(Context $context)
$model['status'] = Constants::STATUS_FAILED;
}
}

/**
* @var Context $context
*/
public function onExecute(Context $context)
{
}

/**
* @var Context $context
*/
public function onPostExecute(Context $context)
{
}
}
Loading

0 comments on commit bcab34f

Please sign in to comment.