PHP code example of xpiston / monetico

1. Go to this page and download the library: Download xpiston/monetico 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 / monetico example snippets


use Piston\Monetico\Monetico;
use Piston\Monetico\Field\{Amount, Reference};
use Piston\Monetico\Payment\PaymentRequest;
use Piston\Monetico\Payment\Context\{BillingAddress, OrderContext};

$monetico = Monetico::sandbox('1234567', $securityKey, 'monSite1');

$form = $monetico->form(new PaymentRequest(
    reference: Reference::of('REF7896543'),
    amount: Amount::fromMinor(6273, 'EUR'),      // 62.73 €, in cents
    orderedAt: new DateTimeImmutable(),
    context: OrderContext::for(
        BillingAddress::create('1 rue de Test', 'Paris', '75001', 'FR'),
    ),
    customerEmail: '[email protected]',
    successUrl: 'https://shop.test/merci',
    errorUrl: 'https://shop.test/paiement-refuse',
));

echo $form->toHtml('Payer 62,73 €');

use Piston\Monetico\Notification\Acknowledgement;

$notification = $monetico->attemptNotification($_POST);   // null if the seal is wrong

if ($notification !== null && $notification->isAccepted()
    && $notification->matches($order->reference, $order->total)) {
    $order->markPaid($notification->authorisationNumber());
}

header('Content-Type: text/plain');
echo Acknowledgement::for($notification !== null)->body();   // version=2\ncdr=0\n

use Piston\Monetico\Service\{CaptureRequest, RefundRequest};

$capture = $monetico->capture(CaptureRequest::of(
    reference: $order->reference,
    orderAmount: $order->total,
    toCapture: $order->total,
    orderedOn: $order->placedAt,
));

$capture->isAccepted();            // the money is on its way
$capture->isRetryable();           // the service was busy; this one is worth repeating

$refund = $monetico->refund(RefundRequest::of(
    reference: $order->reference,
    orderAmount: $order->total,
    toRefund: Amount::fromMinor(3200, 'EUR'),
    orderedOn: $order->placedAt,
    remittedOn: $order->remittedOn,
    authorisationNumber: $order->authorisationNumber,
));

$refund->isCompleted();
$refund->isAlreadyDone();          // -46: nothing to do, and not a failure

use Illuminate\Http\Request;
use Piston\Monetico\Bridge\Laravel\{AcknowledgementResponse, NotificationRequest};
use Piston\Monetico\Monetico;
use Piston\Monetico\Notification\Acknowledgement;

final class MoneticoNotificationController
{
    public function __construct(private readonly Monetico $monetico)
    {
    }

    public function __invoke(Request $request)
    {
        $notification = $this->monetico->attemptNotification(
            NotificationRequest::fields($request),
        );

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

        return AcknowledgementResponse::for(Acknowledgement::for($notification !== null));
    }
}

// routes/web.php
Route::post('/monetico/notify', MoneticoNotificationController::class);

// bootstrap/app.php
->withMiddleware(function (Middleware $middleware) {
    $middleware->validateCsrfTokens(except: ['monetico/notify']);
})

#[Route('/monetico/notify', methods: ['POST'])]
public function notify(Request $request, Monetico $monetico): Response
{
    $notification = $monetico->attemptNotification(NotificationRequest::fields($request));

    return AcknowledgementResponse::for(Acknowledgement::for($notification !== null));
}
yaml
# config/bundles.php
Piston\Monetico\Bridge\Symfony\MoneticoBundle::class => ['all' => true],