PHP code example of neuron-php / payments

1. Go to this page and download the library: Download neuron-php/payments 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/ */

    

neuron-php / payments example snippets


use Neuron\Payments\GatewayFactory;
use Neuron\Payments\Dto\CheckoutSessionRequest;
use Neuron\Payments\Dto\Money;
use Neuron\Payments\Dto\Frequency;

$gateway = GatewayFactory::create( [
    'provider'       => 'stripe',
    'secret_key'     => getenv( 'STRIPE_SECRET_KEY' ),
    'webhook_secret' => getenv( 'STRIPE_WEBHOOK_SECRET' ),
] );

$session = $gateway->createCheckoutSession( new CheckoutSessionRequest(
    amount:      Money::fromMajorUnits( 50.00 ),
    frequency:   Frequency::Monthly,
    successUrl:  'https://example.org/donations/success?session_id={CHECKOUT_SESSION_ID}',
    cancelUrl:   'https://example.org/donations/cancel',
    productName: 'Donation',
    metadata:    [ 'donation_id' => 123 ]
) );

header( 'Location: ' . $session->url );

use Neuron\Payments\Dto\LineItem;

$session = $gateway->createCheckoutSession( new CheckoutSessionRequest(
    amount:     Money::fromMajorUnits( 45.00 ), // convenience total only
    frequency:  Frequency::OneTime,
    successUrl: 'https://example.org/store/success?session_id={CHECKOUT_SESSION_ID}',
    cancelUrl:  'https://example.org/store/cancel',
    metadata:   [ 'payment_id' => 123 ],
    lineItems:  [
        new LineItem( 'T-Shirt', Money::fromMajorUnits( 20.00 ), 2 ),
        new LineItem( 'Sticker', Money::fromMajorUnits( 5.00 ), 1 ),
    ]
) );

$event = $gateway->verifyWebhook(
    file_get_contents( 'php://input' ),
    $_SERVER['HTTP_STRIPE_SIGNATURE'] ?? ''
);

match( true )
{
    // Initial checkout (one-time payment or the first subscription charge)
    $event->isCheckoutCompleted()    => /* mark paid; $event->subscriptionId() != null => recurring */,

    // A subscription invoice was paid; $event->isRenewal() distinguishes a
    // renewal cycle from the initial charge.
    $event->isInvoicePaid()          => /* record renewal via $event->invoiceId() / amountPaid() */,

    // Subscription status / period changed, or it was canceled.
    $event->isSubscriptionUpdated()  => /* $event->subscriptionStatus(), currentPeriodEnd() */,
    $event->isSubscriptionDeleted()  => /* mark canceled */,

    // A renewal payment failed (dunning).
    $event->isInvoicePaymentFailed() => /* flag past_due */,

    default                          => null
};

$subscription = $gateway->getSubscription( 'sub_123' );
$subscription->isActive();            // bool
$subscription->currentPeriodEnd;      // unix timestamp

// Cancel immediately, or at the end of the paid period.
$gateway->cancelSubscription( 'sub_123' );
$gateway->cancelSubscription( 'sub_123', atPeriodEnd: true );
bash
composer 
bash
composer