PHP code example of ma-lara / payments

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

    

ma-lara / payments example snippets


return [
    'stripe' => \Ma\Payment\Gateways\Stripe\StripeGateway::class,
    'paymob' => \Ma\Payment\Gateways\Paymob\PaymobGateway::class,
];

use Ma\Payment\Facades\MaPayment;

$gateway = MaPayment::driver('stripe');

$gateway->pay(...);

$gateway->verify(...);

$gateway->retryPayment(...);

$gateway->refund(...);

$gateway = MaPayment::driver('paymob');


$result = $gateway->pay([
    'amount' => 150.50,
    'currency' => 'USD',

    'customer' => [
        'id' => $user->id,
        'first_name' => 'John',
        'last_name' => 'Doe',
        'email' => '[email protected]',
        'phone' => '+201000000000',
    ],

    'source' => 'card',

    'payment_method' => [
        'id' => 'pm_...',
    ],
]);

150.50

Ma\Payment\Enums\PaymentStatus

$result = $gateway->pay([
    'amount' => 150.50,
    'currency' => 'USD',

    'customer' => [
        'id' => $user->id,
        'first_name' => 'John',
        'last_name' => 'Doe',
        'email' => '[email protected]',
        'phone' => '+201000000000',
    ],

    'source' => 'card',

    'payment_method' => [
        'id' => 'pm_...',
    ],
]);

$gateway->pay([
    // ...
    'payment_method' => [
        'id' => $paymentMethodId,
    ],
]);

$gateway->retryPayment(
    $transactionId,
    $paymentMethodId
);

$paylink = $gateway->pay([
    'amount' => 150.50,
    'currency' => 'EGP',

    'customer' => [
        'id' => $user->id,
        'first_name' => 'John',
        'last_name' => 'Doe',
        'email' => '[email protected]',
        'phone' => '01010101010',
    ],

    'source' => 'card',
]);

'source' => 'wallet'

$paylink = $gateway->pay([
    'amount' => 200,
    'currency' => 'EGP',

    'customer' => [
        'id' => $user->id,
        'first_name' => 'John',
        'last_name' => 'Doe',
        'email' => '[email protected]',
        'phone' => '01010101010',
    ],

    'source' => 'wallet',
]);

$transaction = $gateway->verify(
    $request->all()
);

use Illuminate\Http\Request;
use Ma\Payment\Facades\MaPayment;

Route::post('/stripe/webhook', function (Request $request,) {
    $gateway = MaPayment::driver('stripe');

    return response()->json(
        $gateway->verify(
            $request->getContent(),
            $request->header('Stripe-Signature')
        )
    );
});

UpdateRefundTransactionJob::dispatch($event['refund_id'], $event['status']->value);

->withoutMiddleware([VerifyCsrfToken::class])

use Ma\Payment\Facades\MaPayment;

Route::post('/paymob/callback', function (Request $request) {

    $gateway = MaPayment::driver('paymob');

    return response()->json(
        $gateway->verify($request->all())
    );
});

public function getTransactions(?string $status = null): Illuminate\Database\Eloquent\Collection;

public function getCustomerTransactions(int $userId, ?string $status = null): Illuminate\Database\Eloquent\Collection;

use Ma\Payment\Facades\MaPayment;

$gateway = MaPayment::driver('stripe'); // or 'paymob'

// All transactions (no filter)
$transactions = $gateway->getTransactions();

// Only succeeded transactions
$transactions = $gateway->getTransactions('succeeded');

use Ma\Payment\Facades\MaPayment;

$gateway = MaPayment::driver('paymob');

// All transactions of the user
$transactions = $gateway->getCustomerTransactions($userId);

// Only refunded transactions of the user
$transactions = $gateway->getCustomerTransactions($userId, 'fully_refunded');

$transactions = $gateway
    ->getCustomerTransactions($userId, 'succeeded')
    ->filter(fn ($t) => $t->gateway === 'stripe');

use Ma\Payment\Facades\MaPayment;

$gateway = MaPayment::driver('stripe');

foreach ($gateway->getCustomerTransactions(auth()->id(), 'succeeded') as $transaction) {
    $transaction->gateway_reference; // Stripe PaymentIntent ID
    $transaction->minor_amount;      // e.g. 15050
    $transaction->pounds();          // 150.5
    $transaction->status;            // 'succeeded'
    $transaction->refundedPayments;  // related refund records
}

$gateway->refund($transactionId, $amount);

$gateway->refund($transactionId, 50);

$gateway->retryPayment($transactionId);

use Ma\Payment\Exceptions\RefundAmountGreaterThanTransactionAmountException;

try {
    $gateway->refund($transactionId, 999999);
} catch (RefundAmountGreaterThanTransactionAmountException $e) {
    // Handle invalid refund amount.
}

use Stripe\Exception\CardException;

$manager->driver('stripe');
$manager->driver('paymob');

executePayment()

class TapGateway extends BaseGateway
    implements PaymentGatewayInterface
{
    // ...
}

public function __construct(
    CustomerSerivce $customerService,
    TransactionRepositoryInterface $transactionRepository,
    private TapApiService $apiService,
) {
    parent::__construct(
        $customerService,
        $transactionRepository
    );

    $this->gateway_name = 'tap';
}

protected function sendPaymentRequest(
    PaymentRequestDTO $dto
): array {
    return $this->apiService->charge($dto);
}

PaymentTransactionDTO

return [
    'stripe' => \Ma\Payment\Gateways\Stripe\StripeGateway::class,
    'paymob' => \Ma\Payment\Gateways\Paymob\PaymobGateway::class,
    'tap'    => \Ma\Payment\Gateways\Tap\TapGateway::class,
];
bash
php artisan vendor:publish --tag=ma-payment-config
bash
php artisan vendor:publish --tag=ma-payment-views
bash
php artisan vendor:publish --tag=ma-payment-migrations
bash
php artisan migrate
text
config/ma_payment_conf.php
text
config/ma_payment_drivers.php
text
config/ma_payment_drivers.php
bash
php artisan vendor:publish --tag=ma-payment-views
bash
php artisan queue:work
text
payment_transactions
text
refunded_payment_transactions
text
partially_refunded
text
fully_refunded
text
config/ma-drivers.php
text
src/
└── Gateways/
    └── Tap/
        ├── TapGateway.php
        └── Services/
            ├── TapApiService.php
            └── TapWebhookHandler.php
text
config/ma-drivers.php
text
config/ma-payment.php