Skip to content

Latest commit

 

History

History
97 lines (66 loc) · 2.62 KB

authorize.md

File metadata and controls

97 lines (66 loc) · 2.62 KB

Stripe JS : PaymentIntent with capture_method = manual

Authorize flow is following this Stripe doc page : https://stripe.com/docs/payments/capture-later

Get it started

First get your credentials from Stripe dashboard.

The following example is the basic Payum implementation (see documentation of Payum for more information)

Starting with the configuration of a normal payment, we will have to make some modification on the prepare.php file to generate an AuthorizeToken. Then we will trigger the Authorize request in an authorize.php file.

prepare.php

<?php

declare(strict_types=1);

include __DIR__.'/config.php';

use Payum\Core\Model\Payment;

$gatewayName = 'stripe_js';

$storage = $payum->getStorage(Payment::class);

/** @var Payment $payment */
$payment = $storage->create();
$payment->setNumber(uniqid());
$payment->setCurrencyCode('EUR');
$payment->setTotalAmount(123); // 1.23 EUR
$payment->setDescription('A description');
$payment->setClientId('anId');
$payment->setClientEmail('[email protected]');
$payment->setDetails([]);

$storage->update($payment);

$tokenFactory = $payum->getTokenFactory();
$captureToken = $tokenFactory->createAuthorizeToken($gatewayName, $payment, 'done.php');

header("Location: ".$captureToken->getTargetUrl());

authorize.php

<?php

declare(strict_types=1);

use Payum\Core\Reply\HttpResponse;
use Payum\Core\Request\Authorize;
use Payum\Core\Reply\HttpRedirect;

include __DIR__.'/config.php';

$token = $payum->getHttpRequestVerifier()->verify($_REQUEST);
$gateway = $payum->getGateway($token->getGatewayName());

if ($reply = $gateway->execute(new Authorize($token), true)) {
    if ($reply instanceof HttpRedirect) {
        header("Location: ".$reply->getUrl());
        die();
    }
    if ($reply instanceof HttpResponse) {
        echo $reply->getContent();
        die();
    }

    throw new \LogicException('Unsupported reply', null, $reply);
}

$payum->getHttpRequestVerifier()->invalidate($token);

header("Location: ".$token->getAfterUrl());

Capture the authorized payment

Complete the content of done.php with those lines at the end of the file :

if ($status->getValue() === $status::STATUS_AUTHORIZED) {
    $tokenFactory = $payum->getTokenFactory();
    $captureToken = $tokenFactory->createCaptureToken($gatewayName, $payment, 'done.php');
    echo '<p><a href="'.$captureToken->getTargetUrl().'">Capture</a></p>';
}

Or Cancel the authorized payment

See the dedicated chapter to Cancel a PaymentIntent.