Skip to content

Commit

Permalink
[stripe] add an action to get a credit card token from payment details.
Browse files Browse the repository at this point in the history
  • Loading branch information
makasim committed Sep 15, 2016
1 parent 3e4e6b7 commit 764d427
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
34 changes: 34 additions & 0 deletions Action/GetCreditCardTokenAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
namespace Payum\Stripe\Action;

use Payum\Core\Action\ActionInterface;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\RequestNotSupportedException;
use Payum\Core\Request\GetCreditCardToken;

class GetCreditCardTokenAction implements ActionInterface
{
/**
* {@inheritDoc}
*
* @param GetCreditCardToken $request
*/
public function execute($request)
{
RequestNotSupportedException::assertSupports($this, $request);

$model = ArrayObject::ensureArrayObject($request->getModel());

$request->token = $model['customer'];
}
/**
* {@inheritDoc}
*/
public function supports($request)
{
return
$request instanceof GetCreditCardToken &&
$request->getModel() instanceof \ArrayAccess
;
}
}
4 changes: 2 additions & 2 deletions Resources/docs/store-card-and-use-later.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ once the first payment is done you can get the customer id and store it somewher
use Payum\Core\Request\GetCreditCardToken;

/** @var \Payum\Core\Model\PaymentInterface $payment */

/** @var \Payum\Core\GatewayInterface $gateway */

$gateway->execute($getToken = new GetCreditCardToken($payment));

$token = $getToken->getToken(); // if not null you are done. store it somewhere
$token = $getToken->token; // if not null you are done. store it somewhere
```

## Use stored card
Expand Down
2 changes: 2 additions & 0 deletions StripeCheckoutGatewayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Payum\Stripe\Action\Api\ObtainTokenAction;
use Payum\Stripe\Action\CaptureAction;
use Payum\Stripe\Action\ConvertPaymentAction;
use Payum\Stripe\Action\GetCreditCardTokenAction;
use Payum\Stripe\Extension\CreateCustomerExtension;
use Payum\Stripe\Action\StatusAction;
use Stripe\Stripe;
Expand All @@ -35,6 +36,7 @@ protected function populateConfig(ArrayObject $config)
'payum.action.capture' => new CaptureAction(),
'payum.action.convert_payment' => new ConvertPaymentAction(),
'payum.action.status' => new StatusAction(),
'payum.action.get_credit_card_token' => new GetCreditCardTokenAction(),
'payum.action.obtain_token' => function (ArrayObject $config) {
return new ObtainTokenAction($config['payum.template.obtain_token']);
},
Expand Down
55 changes: 55 additions & 0 deletions Tests/Action/GetCreditCardTokenActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
namespace Payum\Stripe\Tests\Action\Api;

use Payum\Core\Action\ActionInterface;
use Payum\Core\Request\GetCreditCardToken;
use Payum\Core\Tests\GenericActionTest;
use Payum\Stripe\Action\GetCreditCardTokenAction;

class GetCreditCardTokenActionTest extends GenericActionTest
{
protected $requestClass = GetCreditCardToken::class;

protected $actionClass = GetCreditCardTokenAction::class;

/**
* @test
*/
public function shouldImplementActionInterface()
{
$rc = new \ReflectionClass(GetCreditCardTokenAction::class);

$this->assertTrue($rc->implementsInterface(ActionInterface::class));
}

/**
* @test
*/
public function shouldDoNothingIfPaymentHasNoCustomerSet()
{
$model = [
];

$action = new GetCreditCardTokenAction();

$action->execute($getCreditCardToken = new GetCreditCardToken($model));

self::assertEmpty($getCreditCardToken->token);
}

/**
* @test
*/
public function shouldSetCustomerIdAsCardToken()
{
$model = [
'customer' => 'theToken',
];

$action = new GetCreditCardTokenAction();

$action->execute($getCreditCardToken = new GetCreditCardToken($model));

self::assertEquals('theToken', $getCreditCardToken->token);
}
}

0 comments on commit 764d427

Please sign in to comment.