PHP code example of progalaxyelabs / stonescriptphp-payments

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

    

progalaxyelabs / stonescriptphp-payments example snippets


use StoneScriptPay\Drivers\RazorpayDriver;
use StoneScriptPay\DTO\CreateOrderRequest;
use StoneScriptPay\DTO\VerifyRequest;
use StoneScriptPay\DTO\WebhookRequest;

// Instantiate — credentials are runtime-injected from env, never hardcoded
$provider = new RazorpayDriver(
    keyId:         $_ENV['RAZORPAY_KEY_ID'],
    keySecret:     $_ENV['RAZORPAY_KEY_SECRET'],
    webhookSecret: $_ENV['RAZORPAY_WEBHOOK_SECRET'],
);

// 1. Create an order (server-set amount — never trust client)
$order = $provider->createOrder(new CreateOrderRequest(
    amountMinorUnits: 49900,   // paise for INR
    currency:         'INR',
    receipt:          'order_ref_001',
    notes:            ['tenant_id' => $tenantId],
));
// Pass $order->orderId and $order->publishableKeyId to the frontend

// 2. Verify the checkout response from the frontend
$result = $provider->verifySignature(new VerifyRequest(
    paymentId: $request->razorpay_payment_id,
    orderId:   $request->razorpay_order_id,
    signature: $request->razorpay_signature,
));
// $result->verified === true → safe to fulfil the order

// 3. Handle webhooks
$event = $provider->handleWebhook(new WebhookRequest(
    rawBody:   file_get_contents('php://input'),
    signature: $_SERVER['HTTP_X_RAZORPAY_SIGNATURE'] ?? '',
));
if ($event->isPaymentCaptured()) {
    // Record the capture (see "Capture primitive" below), then fulfil
}

use StoneScriptPay\Drivers\PaypalDriver;
use StoneScriptPay\DTO\WebhookRequest;

$provider = new PaypalDriver(
    baseUrl:      $_ENV['PAYPAL_BASE_URL'],      // e.g. https://api-m.sandbox.paypal.com
    clientId:     $_ENV['PAYPAL_CLIENT_ID'],
    clientSecret: $_ENV['PAYPAL_CLIENT_SECRET'],
    webhookId:    $_ENV['PAYPAL_WEBHOOK_ID'],
);

// verifySignature() differs from Razorpay's model: PayPal has no
// client-returned HMAC distinct from capture, so this driver performs the
// CAPTURE call itself inside verifySignature() and reports success only if
// it completed. Pass '' for VerifyRequest::$paymentId/$signature (unused);
// only $orderId is read.

// handleWebhook() needs PayPal's multiple PAYPAL-TRANSMISSION-* headers
// (not one HMAC header like Razorpay) — pass them via WebhookRequest::$headers:
$event = $provider->handleWebhook(new WebhookRequest(
    rawBody:   file_get_contents('php://input'),
    signature: '',
    headers:   getallheaders() ?: [],
));

use StoneScriptPHP\Billing\CollectionOrchestrator;
use StoneScriptPHP\Billing\Contracts\GatewayCode;
use StoneScriptPHP\Invoice\Php\InvoiceSourceAdapter;
use StoneScriptPay\Drivers\RazorpayDriver;

$orchestrator = new CollectionOrchestrator(
    payment: new RazorpayDriver($keyId, $keySecret, $webhookSecret),
    invoices: new InvoiceSourceAdapter(createdBy: 'webhook:razorpay'),
    gatewayCode: GatewayCode::RAZORPAY,
);

$ingress = new WebhookIngress(
    ['razorpay' => new WebhookGateway('razorpay', new RazorpayDriver($keyId, $keySecret, $webhookSecret), 'x-razorpay-signature')],
    new SqlWebhookInboxStore(new DatabaseFnCaller()),            // pay_webhook_* SQL functions via the gateway
    new PlatformRoutingProcessor('myplatform', $myProcessor),    // foreign / unattributed routing + your idempotent handler
    $alertSink,                                                  // optional; defaults to error_log
);

$orders = new OrderService($db, ['razorpay' => $razorpayDriver]);
$view = $orders->create(new OrderRequest(
    tenantId: $tenant, platformCode: 'myplatform', idempotencyKey: $clientKey,   // the retry key
    gatewayCode: 'razorpay', purpose: OrderRequest::SUBSCRIPTION_PERIOD,
    amountMinorUnits: 58882, currency: 'INR',
    catalogRef: (string) $planId, seriesCode: 'acme-inv', invoiceSpec: [...],    // what to invoice
));
// $view->orderRef, ->gatewayOrderId, ->publishableKeyId for the hosted checkout; poll $orders->status(...)

$processor = new PlatformRoutingProcessor('myplatform', new CaptureWebhookProcessor(new CaptureApplier($db), ['razorpay' => $razorpayDriver]));
sql
-- once per seller of record, and once per numbering series
SELECT _inv_issuer_upsert('acme', 'myplatform', NULL, 'Acme Pvt Ltd', 'regular',
                         '29AAACX1234C1ZP', '29', 'address', false, 'actor');
SELECT _inv_series_create('acme-inv',  'acme', 'invoice', 'live', 'AB',    '/', 5, true, 'actor');  -- AB/26-27/00001
SELECT _inv_series_create('acme-test', 'acme', 'invoice', 'test', 'TESTA', '/', 4, true, 'actor');  -- TESTA/26-27/0001

-- every invoice: pass the series and the mode, leave the number NULL
SELECT inv_issue_invoice(NULL, ..., p_series_code => 'acme-inv', p_mode => 'live');