PHP code example of xpiston / worldline-sips

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

    

xpiston / worldline-sips example snippets


use Piston\Sips\Field\Amount;
use Piston\Sips\Paypage\PaymentRequest;
use Piston\Sips\Sips;

$sips = Sips::simulation();          // merchantId 002001000000002, published key

$form = $sips->form(new PaymentRequest(
    amount: Amount::fromMinor(2500, 'EUR'),   // 25,00 €, in cents
    normalReturnUrl: 'https://shop.test/retour',
    automaticResponseUrl: 'https://shop.test/notification',
    orderId: 'ORD101',
));

echo $form->toHtml('Payer 25,00 €');

$sips = Sips::production('011223344550000', $secretKey, keyVersion: 1);

use Piston\Sips\Response\ResponseVerifier;

$response = $sips->attemptResponse($_POST);   // null if the seal does not match

if ($response?->isAccepted() === true
    && $response->matches($order->reference, $order->total, $sips->credentials->merchantId)) {
    $order->markPaid($response->authorisationId());
}

use Piston\Sips\Office\OfficeRequest;

// Taken in VALIDATION mode and now shipping: capture it.
$result = $sips->validate(OfficeRequest::validate($reference, $order->total));

// Out of stock before settlement: cancel, which costs nothing.
$result = $sips->cancel(OfficeRequest::cancel($reference, $order->total));

// Settled and coming back: refund, which is a new movement with its own fees.
$result = $sips->refund(OfficeRequest::refund($reference, Amount::fromMinor(1000, 'EUR')));

$result->isDone();                 // carried out, and the answer was sealed by our key
$result->isTransactionUnknown();   // 25: no such reference
$result->isStateInvalid();         // 24: it exists, but not in a state that allows this
$result->isWorthRetrying();        // 90, 97, 99: the platform, not the request
$result->newAmount();              // what the platform now says is left

use Illuminate\Http\Request;
use Piston\Sips\Bridge\Laravel\ResponseRequest;
use Piston\Sips\Sips;

final class SipsNotificationController
{
    public function __construct(private readonly Sips $sips)
    {
    }

    public function __invoke(Request $request)
    {
        $response = $this->sips->attemptResponse(ResponseRequest::fields($request));

        if ($response?->isAccepted() === true) {
            ProcessPayment::dispatch($response->fields());
        }

        return response('', 200);
    }
}

// routes/web.php
Route::post('/sips/notification', SipsNotificationController::class);

// bootstrap/app.php
->withMiddleware(function (Middleware $middleware) {
    $middleware->validateCsrfTokens(except: ['sips/notification', 'sips/retour']);
})

// config/bundles.php
Piston\Sips\Bridge\Symfony\SipsBundle::class => ['all' => true],