PHP code example of ifthenpay / payment-gateway-sdk

1. Go to this page and download the library: Download ifthenpay/payment-gateway-sdk 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 / payment-gateway-sdk example snippets




use Ifthenpay\PaymentGateway\IfthenpayGateway;

$config = [
    'mbway' => [
        'key' => 'ITP-000000',
    ],
];

$ifthenpayGateway = new IfthenpayGateway($config);

// you can then generate a payment (MB WAY in this case)
$payment = $ifthenpayGateway->mbway()->initPayment('0103', '10,99', '919999999');


// examples/configuration/configClosure.php

return [
    'backofficeKey'   => '1111-1111-1111-1111',
    'antiPhishingKey' => 'a0a0a0a0a0a0aa0a0a0a',
    'language'        => 'pt',
    'mbway'           => [
        'key'             => 'ITP-000000',
        'minutesToExpire' => 4,
    ],
    'multibancoDynamic' => [
        'key'          => 'ITP-000000',
        'daysToExpire' => 3,
    ],
    'multibancoOffline' => [
        'entity'       => '11111',
        'subEntity'    => '111',
        'daysToExpire' => 3,
    ],
    'payshop' => [
        'key'          => 'ITP-000000',
        'daysToExpire' => 3,
    ],
    'creditCard' => [
        'key'             => 'ITP-000000',
        'minutesToExpire' => 15,
        'successUrl'      => 'https://youraddress.com/sucess.php',
        'errorUrl'        => 'https://youraddress.com/error.php',
        'cancelUrl'       => 'https://youraddress.com/cancel.php',
    ],
    'pix' => [
        'key'             => 'ITP-000000',
        'minutesToExpire' => 15,
    ],
    'cofidis' => [
        'key'             => 'ITP-000000',
        'minutesToExpire' => 60,
        'returnUrl'      => 'https://youraddress.com/return.php',
    ],
    'payByLink' => [
        'key'            => 'ITPG-000000', // Gateway key, not the same as other account key
        'methodAccounts' => [
            '11111'   => '111',
            'MBWAY'   => 'ITP-000000',
            'PAYSHOP' => 'ITP-000000',
            'CCARD'   => 'ITP-000000',
            'COFIDIS' => 'ITP-000000',
            'GOOGLE'  => 'ITP-000000',
            'APPLE'   => 'ITP-000000',
            'PIX'     => 'ITP-000000',
        ],
        'defaultMethod' => 'CCARD', // MBWAY, MULTIBANCO_DYNAMIC, MULTIBANCO_OFFLINE, PAYSHOP, CREDIT_CARD, COFIDIS, GOOGLE, APPLE, PIX
        'daysToExpire'  => 3,
        'isOneTimePayment' => true,
        'successUrl'    => 'https://youraddress.com/sucess.php',
        'errorUrl'      => 'https://youraddress.com/error.php',
        'cancelUrl'     => 'https://youraddress.com/cancel.php',
        'btnCloseUrl'   => 'https://youraddress.com',
        'btnCloseLabel' => 'Close',
    ],
];

$ifthenpayGateway = new IfthenpayGateway($config, $guzzle);

$multibancoDynamic = $ifthenpayGateway->multibancoDynamic();
$multibancoOffline = $ifthenpayGateway->multibancoOffline();
$mbway = $ifthenpayGateway->mbway();
$payshop = $ifthenpayGateway->payshop();
$creditCard = $ifthenpayGateway->creditCard();
$cofidis = $ifthenpayGateway->cofidis();
$pix = $ifthenpayGateway->pix();
$payByLink = $ifthenpayGateway->payByLink();

$mbway = $ifthenpayGateway->mbway();
$mbway->initPayment('0103', '10,99', '919999999');
// or 
$ifthenpayGateway->mbway()->initPayment('0103', '10,99', '919999999');

// src/Interface/Service/MultibancoDynamicServiceInterface.php

public function initPayment(string $orderId, string $amount, ?string $description = null, ?int $daysToExpire = null): MultibancoDynamic;
public function isPaid(MultibancoDynamic $multibancoPayment): Bool;
public function registerWebhook(string $webhookUrl, ?array $extraParams = null): string;
public function validateWebhook(WebhookRequest $webhookRequest, MultibancoDynamic $multibancoPayment): void;
public function isExpired(MultibancoDynamic $multibancoPayment): bool;

// src/Interface/Service/MultibancoOfflineServiceInterface.php

public function initPayment(string $orderId, string $amount): MultibancoOffline;
public function isPaid(MultibancoOffline $multibancoPayment): Bool;
public function registerWebhook(string $webhookUrl, ?array $extraParams = null): string;
public function validateWebhook(WebhookRequest $webhookRequest, MultibancoOffline $multibancoPayment): void;
public function isExpired(MultibancoOffline $payment): bool;

// src/Interface/Service/MbwayServiceInterface.php

public function initPayment(string $orderId, string $amount, string $mobileNumber, ?string $description = null, ?string $email = null): Mbway;
public function isPaid(Mbway $mbwayPayment): Bool;
public function getPaymentStatus(string $transactionId): Status;
public function registerWebhook(string $webhookUrl);
public function validateWebhook(WebhookRequest $webhookRequest, Mbway $mbwayPayment): void;
public function isExpired(Mbway $payment): bool;

// src/Interface/Service/PayshopServiceInterface.php

public function initPayment(string $orderId, string $amount, ?int $daysToExpire = null): Payshop;
public function isPaid(Payshop $payshopPayment): Bool;
public function registerWebhook(string $webhookUrl, ?array $extraParams = null): string;
public function validateWebhook(WebhookRequest $webhookRequest, Payshop $payshopPayment): void;
public function isExpired(Payshop $payment): bool;

// src/Interface/Service/CreditCardServiceInterface.php

public function initPayment(string $orderId, string $amount, string $returnUrl, string $language = 'pt'): CreditCard;
public function isPaid(CreditCard $creditCardPayment): Bool;
public function registerWebhook(string $webhookUrl, ?array $extraParams = null): string;
public function validateWebhook(WebhookRequest $webhookRequest, CreditCard $creditCardPayment): void;
public function verifyPayment(string $secretKey, CreditCard $payment): void;
public function isExpired(CreditCard $payment): bool;

// src/Interface/Service/PixServiceInterface.php

public function initPayment(string $orderId, string $amount, string $cpf, string $name, string $email, string $mobileNumber, string $redirect, ?string $description = null): Pix;
public function isPaid(Pix $pixPayment): Bool;
public function registerWebhook(string $webhookUrl, ?array $extraParams = null): string;
public function validateWebhook(WebhookRequest $webhookRequest, Pix $pixPayment): void;
public function isExpired(Pix $payment): bool;

// src/Interface/Service/CofidisServiceInterface.php

public function initPayment(string $orderId, string $amount, CofidisCustomerData $customerData, ?string $description = null, ?string $returnUrl = null): Cofidis;
public function isPaid(Cofidis $cofidisPayment): Bool;
public function getPaymentStatus(string $transactionId, int $numberOfAttempts = 3): Status;
public function registerWebhook(string $webhookUrl, ?array $extraParams = null): string;
public function validateWebhook(WebhookRequest $webhookRequest, Cofidis $cofidisPayment): void;
public function isExpired(Cofidis $payment): bool;

// src/Interface/Service/PayByLinkServiceInterface.php

public function initPayment(string $orderId, string $amount, string $description, string $successUrl, string $errorUrl, string $cancelUrl, string $returnUrl, string $language = 'pt'): PayByLink;
public function registerWebhook(string $webhookUrl, ?array $extraParams = null): string;
public function validateWebhook(WebhookRequest $webhookRequest, PayByLink $payByLinkPayment): void;
public function isTransactionPaid(string $transactionId): bool|MethodCode;
public function isExpired(PayByLink $payment): bool;

/** @var \Ifthenpay\PaymentGateway\Payments\Mbway\MbwayPayment $payment */
$payment = $ifthenpayGateway->mbway()->initPayment(...$mbwayRequestPayload);

    // src/Interface/Model/PaymentInterface.php

    public function toArray(): array;
    public function getOrderId(): string;
    public function getAmount(): string;
    public function getTransactionId(): ?string;
    public function getReference(): ?string;
    public function getStatus(): Status;
    public function getExpireDate(): ?DateTimeImmutable;
    public function getCreatedAt(): ?DateTimeImmutable;
    public function getUpdatedAt(): ?DateTimeImmutable;


$ifthenpayGateway = new IfthenpayGateway($config);
$payment = $ifthenpayGateway->mbway()->initPayment('100021', '15.00', '919999999', 'MB WAY Payment Description');


$ifthenpayGateway = new IfthenpayGateway($config);
$ifthenpayGateway->mbway()->registerWebhook('https://your-payment-status-update-route.com');

// upon receiving the webhook you would get the amount (val), the transaction ID (tid),
// the order ID (oid), and the antiPhishingKey (apk) from the request
// and map them into a WebhookRequest object

$webhookRequest = new WebhookRequest($_GET['val'], $_GET['oid'], $_GET['tid'], $_GET['apk']);

// You would also get the payment record stored in your database (assume $dataArray)
// and map it to a payment object
$storedPayment = new Mbway($dataArray['amount'], $dataArray['orderId'], $dataArray['transactionId'], $dataArray['mobileNumber'], Status::tryFrom($dataArray['status']), $dataArray['expireDate']);

$ifthenpayGateway = new IfthenpayGateway($config);
try {
    $ifthenpayGateway->mbway()->validateWebhook($webhookRequest, $storedPayment);
}
catch {
    // your fail logic
}
// your success logic

// assuming getting a payment record stored in your database ($dataArray) maps it to a payment object
$storedPayment = new Mbway($dataArray['amount'], $dataArray['orderId'], $dataArray['transactionId'], $dataArray['mobileNumber'], Status::tryFrom($dataArray['status']), $dataArray['expireDate']);

$result = $ifthenpayGateway->mbway()->isExpired($payment);


// assuming getting a payment record stored in your database ($dataArray) maps it to a payment object
$storedPayment = new Mbway($dataArray['amount'], $dataArray['orderId'], $dataArray['transactionId'], $dataArray['mobileNumber'], Status::tryFrom($dataArray['status']),

$result = $ifthenpayGateway->mbway()->isPaid($storedPayment);

// assuming getting a payment record stored in your database ($dataArray)
$transactionId = $dataArray['transactionId'];

/** @var \Ifthenpay\PaymentGateway\Enums\Status $mbwayPaymentStatus */
$mbwayPaymentStatus = $ifthenpayGateway->mbway()->getPaymentStatus($transactionId);


$paymentMethod = $ifthenpayGateway->payByLink()->isTransactionPaid($transactionId);

if($paymentMethod) {
    // payment complete using $paymentMethod
}else{
    // payment is pending
}

// assumes you are redirected from the credit card gateway page
$isPaid = $ifthenpayGateway->creditCard()->verifyPayment($_GET['sk'], $payment);
if($isPaid) {
    // payment successful logic
}else{
    // payment error logic
}

// examples/configuration/factoryWithConfig.php

use Ifthenpay\PaymentGateway\IfthenpayGateway;

class IfthenpaySdkFactory
{
    public static function make(): IfthenpayGateway
    {
        $config = [
            'mbway' => [
                'key' => 'ITP-000000',
                'minutesToExpire' => 15,
            ],
        ];

        $ifthenpayGateway = new IfthenpayGateway($config);
        return $ifthenpayGateway;
    }
}

// Usage 
$ifthenpayGateway = IfthenpaySdkFactory::make();

// Now you can use the $ifthenpayGateway instance to access payment methods
// For example, to create a MB WAY payment:
$payment = $ifthenpayGateway->mbway()->initPayment('0103', '10,99', '919999999');


// examples/configuration/configClosure.php

return [
    'backofficeKey' => '1111-1111-1111-1111',
    'antiPhishingKey' => 'a0a0a0a0a0a0aa0a0a0a',
    'language' => 'pt',
    'mbway' => [
        'key' => 'ITP-000000',
        'minutesToExpire' => 15,
    ]
];


// examples/configuration/factoryWithConfigClosure.php

use Ifthenpay\PaymentGateway\IfthenpayGateway;

class SdkFactory
{
    public static function make(): IfthenpayGateway
    {
        $config =