PHP code example of ebrahimsalahi / digipay-php

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

    

ebrahimsalahi / digipay-php example snippets


use EbrahimSalahi\DigiPay\DigiPay;
use EbrahimSalahi\DigiPay\DigiPayConfig;

$config = new DigiPayConfig(
    username: 'merchant-username',
    password: 'merchant-password',
    clientId: 'client-id',
    clientSecret: 'client-secret',
);

$sdk = new DigiPay($config);

use EbrahimSalahi\DigiPay\DigiPayConfig;

$config = DigiPayConfig::fromEnv(
    clientId: 'your-client-id',
    clientSecret: 'your-client-secret'
);

$token = $sdk->login();

use EbrahimSalahi\DigiPay\Enums\PreferredGateway;

$response = $sdk->createSimpleTicket(
    amount: 150000,
    cellNumber: '09120000000',
    providerId: 'ORDER-1001',
    callbackUrl: 'https://example.com/digipay/callback',
    preferredGateway: PreferredGateway::WALLET
);
header('Location: ' . $response->redirectUrl());
exit;

$callback = $sdk->parseCallback($_POST);

$callback->assertMatchesOrder(
    expectedAmount: 150000,
    expectedProviderId: 'ORDER-1001'
);

use EbrahimSalahi\DigiPay\Enums\TicketType;

$verify = $sdk->verify(
    trackingCode: $callback->trackingCode,
    providerId: $callback->providerId,
    type: TicketType::WALLET
);

use EbrahimSalahi\DigiPay\Enums\TicketType;

$reverse = $sdk->reverse(
    purchaseTrackingCode: $callback->trackingCode,
    providerId: $callback->providerId,
    type: TicketType::IPG
);

use EbrahimSalahi\DigiPay\Enums\TicketType;

$deliver = $sdk->deliver(
    deliveryDate: 1592502763000,
    invoiceNumber: '7471288365484',
    trackingCode: '5239470511667728782510',
    products: ['product-4', 'product-5'],
    type: TicketType::CREDIT
);

use EbrahimSalahi\DigiPay\Enums\TicketType;

$refund = $sdk->refund(
    providerId: 'REFUND-1',
    amount: 150000,
    saleTrackingCode: '5239470511667728782510',
    type: TicketType::WALLET
);

use EbrahimSalahi\DigiPay\Enums\TicketType;

$inquiry = $sdk->refundInquiry(
    inquiryId: 'REFUND-1',
    type: TicketType::WALLET
);

use EbrahimSalahi\DigiPay\Exceptions\AuthenticationException;
use EbrahimSalahi\DigiPay\Exceptions\ValidationException;
use EbrahimSalahi\DigiPay\Exceptions\BusinessException;
use EbrahimSalahi\DigiPay\Exceptions\ApiException;

try {
    $sdk->login();
} catch (AuthenticationException $e) {
    // احراز هویت
} catch (ValidationException $e) {
    // ورودی نامعتبر
} catch (BusinessException $e) {
    echo $e->getBusinessCode();
    echo $e->getBusinessMessageFa();
    echo $e->isRetryable() ? 'retry' : 'stop';
} catch (ApiException $e) {
    // خطای سرویس
}
bash
composer