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

Better support for Dynamic 3D Secure workflow with Stripe Elements #156

Open
wants to merge 2 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
7 changes: 4 additions & 3 deletions src/Message/PaymentIntents/AuthorizeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@
*
* // 3DS 2.0 time!
* if ($response->isRedirect()) {
* $response->redirect();
* $response->redirect();
* } else if ($response->isStripeSDKAction()) {
* $client_secret = $response->getClientSecret();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be returned in response so client application (javascript etc) can use it.
Can you add comment about it here?

* } else if ($response->isSuccessful()) {
* echo "Authorize transaction was successful!\n";
* $sale_id = $response->getTransactionReference();
Expand Down Expand Up @@ -348,8 +350,7 @@ public function getData()

$data['confirm'] = $this->getConfirm() ? 'true' : 'false';

if ($this->getConfirm()) {
$this->validate('returnUrl');
if ($this->getConfirm() && $this->getReturnUrl()) {
$data['return_url'] = $this->getReturnUrl();
}
Comment on lines -351 to 355

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's better to write it like this?

if ($this->getReturnUrl()) {
    $data['confirm'] = 'true';
}

If you set return_url, then confirm should be true according to stripe docs https://stripe.com/docs/api/payment_intents/create#create_payment_intent-return_url . It's better to set it by auto, I think.


Expand Down
22 changes: 20 additions & 2 deletions src/Message/PaymentIntents/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,6 @@ public function isCancelled()
public function isRedirect()
{
if ($this->getStatus() === 'requires_action' || $this->getStatus() === 'requires_source_action') {
// Currently this gateway supports only manual confirmation, so any other
// next action types pretty much mean a failed transaction for us.
return (!empty($this->data['next_action']) && $this->data['next_action']['type'] === 'redirect_to_url');
}

Expand All @@ -155,6 +153,26 @@ public function getRedirectUrl()
return $this->isRedirect() ? $this->data['next_action']['redirect_to_url']['url'] : parent::getRedirectUrl();
}

/**
* @return bool
*/
public function isStripeSDKAction()
{
if ($this->getStatus() === 'requires_action' || $this->getStatus() === 'requires_source_action') {
return (!empty($this->data['next_action']) && $this->data['next_action']['type'] === 'use_stripe_sdk');
}

return false;
}

/**
* @return string|null
*/
public function getClientSecret()
{
return isset($this->data['client_secret']) ? $this->data['client_secret'] : null;
}

/**
* Get the payment intent reference.
*
Expand Down
12 changes: 4 additions & 8 deletions tests/Message/PaymentIntents/AuthorizeRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,13 @@ public function testDataWithDestination()
}

/**
* Confirming a payment intent without a return url would destroy the flow for 3DS 2.0,
* so let's make sure that setting confirm to true and skipping return url is
* not permitted.
*
* @expectedException \Omnipay\Common\Exception\InvalidRequestException
* @expectedExceptionMessage The returnUrl parameter is required
* Return url is only supported when confirming a payment intent.
* When a return url is specified, the response will provide a redirect url that supports 3DS 2.0.
*/
public function testReturnUrlMustBeSetWhenConfirming()
public function testReturnUrlWhenConfirming()
{
$this->request->setReturnUrl(null);
$data = $this->request->getData();
$this->assertArrayHasKey('return_url', $data);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/Message/PaymentIntents/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ public function testRequiresConfirmation()
$this->assertTrue($response->requiresConfirmation());
}

public function testClientSecret()
{
$httpResponse = $this->getMockHttpResponse('AuthorizeSuccess.txt');
$response = new Response($this->getMockRequest(), (string) $httpResponse->getBody());

$this->assertEquals('pi_1Euf5UFSbr6xR4YAp9PPTxza_secret_QjDdAp77yVbiJoyJ92mXx76F7', $response->getClientSecret());
}

public function testGetCardReference()
{
$httpResponse = $this->getMockHttpResponse('AuthorizeSuccess.txt');
Expand Down