PHP code example of samandeggs / silverstripe-stripewebhook

1. Go to this page and download the library: Download samandeggs/silverstripe-stripewebhook 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/ */

    

samandeggs / silverstripe-stripewebhook example snippets



use Stripe\Event;
use Vulcan\StripeWebhook\Handlers\StripeEventHandler;
use SilverStripe\Security\Member;

class CustomerEventsHandler extends StripeEventHandler
{
    private static $events = [
        'customer.created',
        'customer.deleted'
    ];

    public static function handle($event, Event $data)
    {
        // $event is the string identifier of the event
        if ($event == 'customer.created') {
            // create member
            return "Member created";
        }

        $member = Member::get()->filter('Email', $event->data->object->email)->first();

        if (!$member) {
            return "Member did not exist";
        }

        $member->delete();
        return "Member deleted";
    }
}

public function MakeStripeSession($data, $standard = false)
{
    $stripe_secret_key = '';

    if ($standard) {
        $stripe_secret_key = Environment::getEnv('STRIPE_STANDARD_SECRET');
    } else {
        $stripe_secret_key = Environment::getEnv('STRIPE_SECONDARY_SECRET');
    }

    $stripe = new \Stripe\StripeClient($stripe_secret_key);

    $sessionInformation = [
        'payment_method_types' => ['card'],
        'line_items' => [
            [
            'price_data' => [
                'currency' => 'nzd',
                'product_data' => [
                'name' => $data->Name,
                ],
                // price in cents
                'unit_amount' => $data->TotalCost,
            ],
            'quantity' => 1,
            ],
        ],
        'mode' => 'payment',
        'payment_intent_data' => [
            'capture_method' => 'manual',
            'metadata' => [
                'stripe_account' => $standard ? 'STANDARD' : 'SECONDARY', // supporting multi .env / multi stripe account webhook return
            ]
        ],
        'success_url' => 'https://www.example.com/success',
        'cancel_url' => 'https://www.example.com/cancel',
    ];

    return $stripe->checkout->sessions->create($sessionInformation);
}