PHP code example of omnitrade / stripe

1. Go to this page and download the library: Download omnitrade/stripe library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

omnitrade / stripe example snippets




mnitrade\Model\Money;
use Omnitrade\Model\Payment;
use Omnitrade\Stripe\StripeGatewayFactory;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;

// Stripe's answer (the Checkout session), recorded: no account, and nothing leaves the process.
$http = new MockHttpClient(new MockResponse('{"id": "cs_test_1", "object": "checkout.session", "status": "open", "payment_status": "unpaid", "amount_total": 1250, "currency": "eur", "url": "https://checkout.stripe.com/c/pay/cs_test_1"}'));
$gateway = (new StripeGatewayFactory($http))->create(['api_key' => 'sk_test_not_a_key']);

$transaction = $gateway->purchase(new Payment(amount: Money::of(1250, 'EUR'), reference: 'ORDER-1042', returnUrl: 'https://shop.example/back'));
echo $transaction->status->value, ' ', $transaction->redirectUrl, "\n";   // pending https://checkout.stripe.com/c/pay/cs_test_1

$gateway = (new StripeGatewayFactory($http))->create(['api_key' => '...', 'webhook_secret' => '...']);   // $http: the application's HTTP client; none given, the factory makes its own

use Omnitrade\Model\Customer;
use Omnitrade\Model\Money;
use Omnitrade\Model\Payment;
use Omnitrade\Request\CompleteMethodSetup;
use Omnitrade\Request\FetchSavedMethods;
use Omnitrade\Request\SetupMethod;

$setup = $stripe->execute(new SetupMethod(new Customer('[email protected]'), 'https://shop.example/saved'))->getSetup();
// send the buyer to $setup->redirectUrl; keep $setup->customer (cus_...) and $setup->reference (cs_...)
// back on https://shop.example/saved:
$saved = $stripe->execute(new CompleteMethodSetup($setup->reference, $setup->customer))->getMethod();   // "Visa •••• 4242"
$card = $stripe->execute(new FetchSavedMethods($setup->customer))->getMethods()[0];   // "Visa •••• 4242"
$paid = $stripe->purchase(new Payment(Money::of(1990, 'EUR'), 'ORDER-7', customer: new Customer(reference: $setup->customer),
    returnUrl: 'https://shop.example/orders/7', idempotencyKey: 'order-7-attempt-1', savedMethod: $card->reference));
// PAID, REFUSED, or PENDING with the bank's page in $paid->redirectUrl