PHP code example of tarfin-labs / moka

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

    

tarfin-labs / moka example snippets


use Tarfin\Moka\Facades\Moka;

// With minimal parameters
public function checkoutMinimal()
{
    $result = Moka::threeDPayment()->create(
        amount: 100.00,
        cardHolderName: 'John Doe',
        cardNumber: '5555555555555555',
        expMonth: '12',
        expYear: '2025',
        cvc: '123'
    );

    return $result; // Returns RedirectResponse
}

// With all parameters
public function checkout()
{
    $result = Moka::threeDPayment()->create(
        amount: 100.00,
        cardHolderName: 'John Doe',
        cardNumber: '5555555555555555',
        expMonth: '12',
        expYear: '2025',
        cvc: '123',
        software: 'Tarfin',
        // Optional parameters
        returnUrl: 'https://your-site.com/moka-callback', // Defaults to route('callback.handle3D')
        installment: 1,
        otherTrxCode: 'your-unique-id', // If not provided, a UUID will be generated
        isPoolPayment: 0,
        isTokenized: 0,
        currency: 'TL',
        redirectType: 1,
        language: 'TR',
        description: 'Payment description'
    );

    // The user will be redirected to Moka's 3D secure page
    return $result; // Returns RedirectResponse
}

// With buyer information using method chaining
public function checkoutWithBuyerInfo()
{
    $result = Moka::threeDPayment()
        ->buyerInformation(
            fullName: 'John Doe',
            gsmNumber: '5551234567',
            email: '[email protected]',
            address: '123 Main St, City'
        )
        ->create(
            amount: 100.00,
            cardHolderName: 'John Doe',
            cardNumber: '5555555555555555',
            expMonth: '12',
            expYear: '2025',
            cvc: '123'
        );

    return $result; // Returns RedirectResponse
}

// Without redirect away if you want to handle the payment in your own view
public function checkoutMinimal()
{
    $result = Moka::threeDPayment()->create(
        amount: 100.00,
        cardHolderName: 'John Doe',
        cardNumber: '5555555555555555',
        expMonth: '12',
        expYear: '2025',
        cvc: '123',
        redirectAway: false // Set false to not redirect away
    );

    return $result; // Returns Array with parameters Url and CodeForHash 
}

[
    'other_trx_code' => 'other_transaction_id',
    'status' => 'success|failed',
    'message' => 'Payment result message'
]

// For successful payments
Tarfin\Moka\Events\MokaPaymentSucceededEvent::dispatch($payment);

// For failed payments
Tarfin\Moka\Events\MokaPaymentFailedEvent::dispatch($payment);

// app/Listeners/HandleSuccessfulMokaPayment.php

namespace App\Listeners;

use Tarfin\Moka\Events\MokaPaymentSucceededEvent;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class HandleSuccessfulMokaPayment implements ShouldQueue
{
    use InteractsWithQueue;

    /**
     * Handle the event.
     */
    public function handle(MokaPaymentSucceededEvent $event): void
    {
        $payment = $event->mokaPayment;

        // Access payment details
        $transactionId = $payment->other_trx_code;
        $amount = $payment->amount;

        // Implement your business logic
        // - Complete the order
        // - Generate invoice
        // - Send confirmation email
        // - Update inventory
    }
}

// app/Listeners/HandleFailedMokaPayment.php

namespace App\Listeners;

use Tarfin\Moka\Events\MokaPaymentFailedEvent;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class HandleFailedMokaPayment implements ShouldQueue
{
    use InteractsWithQueue;

    /**
     * Handle the event.
     */
    public function handle(MokaPaymentFailedEvent $event): void
    {
        $payment = $event->mokaPayment;

        // Access payment details
        $transactionId = $payment->other_trx_code;
        $failureCode = $payment->result_code;
        $failureMessage = $payment->result_message;

        // Implement your business logic
        // - Update order status
        // - Notify customer
        // - Log payment failure
    }
}

$result = Moka::threeDPayment()->create(
    amount: 100.00,
    cardHolderName: 'John Doe',
    cardNumber: '5555555555555555',
    expMonth: '12',
    expYear: '2025',
    cvc: '123',
    returnUrl: route('moka-callback.handle3D', [
        'success_url' => 'https://your-site.com/orders/123/payment-success',
        'failure_url' => 'https://your-site.com/orders/123/payment-failed',
    ]),
    installment: 1,
);

use TarfinMokaServicesInformationMokaPaymentAmount;

// With minimal parameters
$paymentAmount = app(MokaPaymentAmount::class);
$result = $paymentAmount->calculate(
    binNumber: '526911',
    amount: 100.00
);

// With all parameters
$result = $paymentAmount->calculate(
    binNumber: '526911',
    amount: 100.00,
    installment: 3,
    isThreeD: 0,
    currency: 'USD'
);

[
    'PaymentAmount' => 101.56,
    'DealerDepositAmount' => 95.0,
    'DealerCommissionRate' => 6.46,
    'DealerCommissionAmount' => 6.56,
    'DealerCommissionFixedAmount' => 0.0,
    'DealerGroupCommissionRate' => 1.54,
    'DealerGroupCommissionAmount' => 1.56,
    'DealerGroupCommissionFixedAmount' => 0.0,
    'GroupRevenueRate' => 5.0,
    'GroupRevenueAmount' => 5.0,
    'BankCard' => [
        'BankName' => 'FINANSBANK',
        'BankCode' => '111',
        'BinNumber' => '526911',
        'CardType' => 'MASTER',
        'CreditType' => 'CreditCard',
        'ProductCategory' => 'Bireysel'
    ]
]

use Tarfin\Moka\Facades\Moka;

$binInfo = Moka::binInquiry()->get('526911');

// Response structure
[
    'BankName' => 'FİNANSBANK',
    'BankCode' => '111',
    'BinNumber' => '526911',
    'CardName' => '',
    'CardType' => 'MASTER',
    'CreditType' => 'CreditCard',
    'CardLogo' => 'https://cdn.moka.com/Content/BankLogo/CARDFINANS.png',
    'CardTemplate' => 'https://cdn.moka.com/Content/BankCardTemplate/FINANS-MASTER-CREDIT.png',
    'ProductCategory' => 'Bireysel',
    'GroupName' => 'CARDFINANS'
]

use Tarfin\Moka\Facades\Moka;

// With minimal parameters
$result = Moka::paymentTable()->calculate(
    amount: 100.00
);

// With BIN number
$result = Moka::paymentTable()->calculate(
    amount: 100.00,
    binNumber: '526911'
);

// With all parameters
$result = Moka::paymentTable()->calculate(
    amount: 100.00,
    binNumber: '526911',
    isThreeD: 0,
    isIncludedCommissionAmount: 0,
    currency: 'TL',
);

'BankPaymentInstallmentInfoList' => [
    [
        'BankInfoName' => 'GENEL',
        'PaymentInstallmentInfoList' => [
            [
                'CommissionType' => 'CreditCard',
                'InstallmentNumber' => 1,
                'DealerCommissionRate' => 2.2,
                'DealerCommissionFixedAmount' => 0,
                'DealerCommissionAmount' => 2.2,
                'PerInstallmentAmount' => 100,
                'Amount' => 100,
            ],
            // ... more installment options
        ],
    ],
    [
        'BankInfoName' => 'AXESS',
        'PaymentInstallmentInfoList' => [
            [
                'CommissionType' => 'CreditCard',
                'InstallmentNumber' => 1,
                'DealerCommissionRate' => 3,
                'DealerCommissionFixedAmount' => 0,
                'DealerCommissionAmount' => 3,
                'PerInstallmentAmount' => 100,
                'Amount' => 100,
            ],
            // ... more installment options
        ],
    ],
],

use Tarfin\Moka\Facades\Moka;

// Get payment details using paymentId
$paymentDetails = Moka::paymentDetailList()->get('1170');

// Or get payment details using your transaction code
$paymentDetails = Moka::paymentDetailList()->get(null, 'YOUR_ORDER_CODE_123');

[
    'IsSuccessful' => true,
    'ResultCode' => '00',
    'ResultMessage' => '',
    'PaymentDetail' => [
        'DealerPaymentId' => 27405,
        'OtherTrxCode' => 'YOUR_ORDER_CODE_123',
        'CardHolderFullName' => 'John Doe',
        'CardNumberFirstSix' => '554960',
        'CardNumberLastFour' => '5523',
        'PaymentDate' => '2023-06-15T14:42:17.26',
        'Amount' => 100.00,
        'RefAmount' => 0.00,
        'CurrencyCode' => 'TL',
        'InstallmentNumber' => 0,
        'DealerCommissionAmount' => 2.50,
        'IsThreeD' => true,
        'Description' => 'Payment description',
        'PaymentStatus' => 2,
        'TrxStatus' => 1
    ],
    'ListItemCount' => 1,
    'PaymentTrxDetailList' => [
        [
            'DealerPaymentTrxId' => 2971,
            'DealerPaymentId' => 27405,
            'TrxCode' => '26ba712e-6381-4291-8c59-702c13b30d4d',
            'TrxDate' => '2023-06-15T14:42:17.837',
            'Amount' => 100.00,
            'TrxType' => 2,
            'TrxStatus' => 1,
            'PaymentReason' => 1,
            'VoidRefundReason' => 0,
            'VirtualPosOrderId' => 'ORDER-23060RYOG07011948',
            'ResultMessage' => ''
        ]
    ]
]
bash
php artisan vendor:publish --tag="moka-config"