PHP code example of ifthenpay / laravel

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

    

ifthenpay / laravel example snippets


use Ifthenpay\Laravel\Facades\Ifthenpay;

$paymentResponse = Ifthenpay::multibanco()->generatePayment(
    orderId: 'order-1887',
    amount: '10.99',
    description: 'Order #1887', // optional
    expiryDays: 3,               // optional
);

use Ifthenpay\Laravel\Gateways\MultibancoGateway;

$multibancoGateway = new MultibancoGateway(['multibanco_key' => 'ITP-000000']);

$multibancoGateway->generatePayment(
    orderId: 'order-1887',
    amount: '10.99',
    description: 'Order #1887', // optional
    expiryDays: 3,               // optional
);

// ifthenpay/laravel-ifthenpay/src/Gateways/MultibancoGateway.php
public function generatePayment(string $orderId, string $amount, ?string $description = null, ?int $expiryDays = null): MultibancoResponse

// ifthenpay/laravel-ifthenpay/src/Gateways/MbwayGateway.php
public function generatePayment(string $orderId, string $amount, string $mobileNumber, ?string $description = null, ?string $email = null, ?int $expiryMinutes = null): MbwayResponse

// ifthenpay/laravel-ifthenpay/src/Gateways/PayshopGateway.php
public function generatePayment(string $orderId, string $amount, ?int $expiryDays = null): PayshopResponse

// ifthenpay/laravel-ifthenpay/src/Gateways/CreditcardGateway.php
public function generatePayment(string $orderId, string $amount, string $successUrl, string $errorUrl, string $cancelUrl, string $language = 'en', ?int $expiryMinutes = null): CreditcardResponse

// ifthenpay/laravel-ifthenpay/src/Gateways/PixGateway.php
public function generatePayment(string $orderId, string $amount, string $customerCpf, string $customerName, string $customerEmail, string $customerPhone, string $redirectUrl, ?string $description = null, ?string $customerAddress = null, ?string $customerStreetNumber = null, ?string $customerCity = null, ?string $customerZipCode = null, ?string $customerState = null, ?int $expiryMinutes = null): PixResponse

// ifthenpay/laravel-ifthenpay/src/Gateways/PaybylinkGateway.php
public function generatePayment(string $orderId, string $amount, string $successUrl, string $errorUrl, string $cancelUrl, ?string $description = null, ?int $expiryDays = null, bool $otp = false, string $lang = 'pt', ?string $btnCloseUrl = null, ?string $btnCloseLabel = null): PaybylinkResponse

// ifthenpay/laravel-ifthenpay/src/Contracts/IfthenpayResponse.php
public function isSuccessful(): bool;   // result of payment generation
public function getMessage(): ?string;  // error message, in case of failure
public function toArray(): array;       // response payload with keys prepared for mass-assignment to ifthenpay_payments model

use Ifthenpay\Laravel\Facades\Ifthenpay;
use Ifthenpay\Laravel\Models\Payment;

$multibancoResponse = Ifthenpay::multibanco()->generatePayment(
    orderId: '1000',
    amount: '10.99',
    description: 'Order #1887', // optional
    expiryDays: 3,               // optional
);

if(!$multibancoResponse->isSuccessful()) {
	throw new Exception("Error Generating Payment", 1);
}

// store payment or another action
Payment::create($response->toArray());

use Ifthenpay\Laravel\Facades\Ifthenpay;
use Ifthenpay\Laravel\Models\Payment;

$mbwayResponse = Ifthenpay::mbway()->generatePayment(
    orderId: '1000',
    amount: '10.99',
    mobileNumber: '351#912345678',
    description: 'Order #1887', // optional
    email: '[email protected]',  // optional
    expiryMinutes: 10,          // optional
);

if(!$mbwayResponse->isSuccessful()) {
	throw new Exception("Error Generating Payment", 1);
}

// store payment or another action
Payment::create($mbwayResponse->toArray());

use Ifthenpay\Laravel\Facades\Ifthenpay;
use Ifthenpay\Laravel\Models\Payment;

$payshopResponse = Ifthenpay::payshop()->generatePayment(
    orderId: '1000',
    amount: '10.99',
    expiryDays: 5, // optional
);

if(!$payshopResponse->isSuccessful()) {
	throw new Exception("Error Generating Payment", 1);
}

// store payment or another action
Payment::create($payshopResponse->toArray());

use Ifthenpay\Laravel\Facades\Ifthenpay;
use Ifthenpay\Laravel\Models\Payment;

$creditcardResponse = Ifthenpay::creditcard()->generatePayment(
    orderId: '1000',
    amount: '10.99',
    successUrl: 'https://example.com/success',
    errorUrl: 'https://example.com/error',
    cancelUrl: 'https://example.com/cancel',
    language: 'en',    // optional
    expiryMinutes: 15, // optional
);

if(!$creditcardResponse->isSuccessful()) {
	throw new Exception("Error Generating Payment", 1);
}

// store payment or another action
Payment::create($creditcardResponse->toArray());

use Ifthenpay\Laravel\Facades\Ifthenpay;
use Ifthenpay\Laravel\Models\Payment;

$pixResponse = Ifthenpay::pix()->generatePayment(
    orderId: '1000',
    amount: '10.99',
    customerCpf: '123.456.789-00',
    customerName: 'John Doe',
    customerEmail: '[email protected]',
    customerPhone: '351912345678',
    redirectUrl: 'https://example.com/redirect',
    description: 'Order #1887',     // optional
    customerAddress: 'Rua Exemplo', // optional
    customerStreetNumber: '10',     // optional
    customerCity: 'Lisboa',         // optional
    customerZipCode: '1000-001',    // optional
    customerState: 'Lisboa',        // optional
    expiryMinutes: 15,              // optional
);

if(!$pixResponse->isSuccessful()) {
	throw new Exception("Error Generating Payment", 1);
}

// store payment or another action
Payment::create($pixResponse->toArray());

use Ifthenpay\Laravel\Facades\Ifthenpay;
use Ifthenpay\Laravel\Models\Payment;

$paybylinkResponse = Ifthenpay::paybylink()->generatePayment(
    orderId: '1000',
    amount: '10.99',
    successUrl: 'https://example.com/success',
    errorUrl: 'https://example.com/error',
    cancelUrl: 'https://example.com/cancel',
    description: 'Order #1887',               // optional
    expiryDays: 5,                             // optional
    otp: false,                                // optional
    lang: 'pt',                                // optional
    btnCloseUrl: 'https://example.com/close',  // optional
    btnCloseLabel: 'Close',                    // optional
);

if(!$paybylinkResponse->isSuccessful()) {
	throw new Exception("Error Generating Payment", 1);
}

// store payment or another action
Payment::create($paybylinkResponse->toArray());

use Ifthenpay\Laravel\Facades\Ifthenpay;
use Ifthenpay\Laravel\Models\Payment;

$multibancoResponse = Ifthenpay::multibanco()->generatePayment(orderId: 'order-1887', amount: '10.99', expiryDays: 3);

Payment::create($multibancoResponse->toArray());

// app/Models/Order.php
use Ifthenpay\Laravel\Traits\HasIfthenpayPayments;

class Order extends Model
{
    use HasIfthenpayPayments;
}

// app/Http/Controllers/YourController.php
use App\Models\Order;

$order = Order::find(1);
$payment = $order->createMbwayPayment(orderId: 'order-1887', amount: '10.99', mobileNumber: '351#912345678');

$orderPayments = $order->payments;          // all payments for this order (MorphMany)
$orderMbwayPayments = $order->mbwayPayments;     // only its MB WAY payments

// /routes/console.php
use Illuminate\Support\Facades\Schedule;

Schedule::command('ifthenpay:payments:expire')->hourly();

use Ifthenpay\Laravel\Facades\Ifthenpay;
use Ifthenpay\Laravel\Models\Payment;

$payment = Payment::find(1);

$statusResponse = Ifthenpay::mbway()->checkStatus($payment);

if ($statusResponse->isSuccessful()) {
    $payment->markAsPaid();
}

// ifthenpay/laravel-ifthenpay/src/Responses/MbwayStatusResponse.php
public function __construct(
    public ?string $message,
    public MbwayStatus $status,
) {}

use Ifthenpay\Laravel\Exceptions\IfthenpayException;
use Ifthenpay\Laravel\Facades\Ifthenpay;
use Ifthenpay\Laravel\Models\Payment;

$payment = Payment::where('order_id', request('orderId'))->firstOrFail();

try {
    Ifthenpay::creditcard()->verifyPayment(secretKey: request('sk'), payment: $payment);
} catch (IfthenpayException $e) {
    abort(403, 'Invalid payment return.');
}

// verification passed, safe to treat as returning from a genuine ifthenpay redirect

use Ifthenpay\Laravel\Events\PaymentExpired;
use Illuminate\Support\Facades\Event;

Event::listen(function (PaymentExpired $event) {
    // $event->payment — the Payment model instance that was just marked as expired
});

use Ifthenpay\Laravel\Events\PaymentWebhookConfirmed;
use Illuminate\Support\Facades\Event;

Event::listen(function (PaymentWebhookConfirmed $event) {
    // $event->payload — Ifthenpay\Laravel\DTO\WebhookPayload: method, orderId, reference, requestId, amount
});

use Ifthenpay\Laravel\Events\PaymentWebhookRefunded;
use Illuminate\Support\Facades\Event;

Event::listen(function (PaymentWebhookRefunded $event) {
    // $event->payload — Ifthenpay\Laravel\DTO\WebhookPayload: method, orderId, reference, requestId, amount
});

use Ifthenpay\Laravel\Events\PaymentWebhookRejected;
use Illuminate\Support\Facades\Event;

Event::listen(function (PaymentWebhookRejected $event) {
    // $event->payload — Ifthenpay\Laravel\DTO\WebhookPayload, built defensively from the raw (possibly invalid) query
    // $event->reason  — internal string explaining why the webhook was rejected
});

// app/Providers/AppServiceProvider.php
use Ifthenpay\Laravel\Contracts\LoggableEvent;
use Ifthenpay\Laravel\Listeners\LogIfthenpayEvent;
use Illuminate\Support\Facades\Event;

Event::listen(LoggableEvent::class, LogIfthenpayEvent::class);
bash
php artisan vendor:publish --tag=ifthenpay-config
bash
php artisan migrate
bash
php artisan ifthenpay:register-webhook
bash
php artisan ifthenpay:register-webhook --method=mbway --key=ITP-000000
bash
php artisan ifthenpay:register-webhook --method=mbway --key=ITP-000000 --url="https://yoururl.com"
bash
php artisan ifthenpay:register-webhook --method=mbway --key=ITP-000000 --url="https://yoururl.com" --params="scope=01"
bash
php artisan vendor:publish --tag=ifthenpay-routes
bash
php artisan vendor:publish --tag=ifthenpay-views