1. Go to this page and download the library: Download ownpay/ownpay-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/ */
namespace App\Listeners;
use OwnPay\Laravel\Laravel\Events\WebhookReceived;
class HandleOwnPayWebhook
{
public function handle(WebhookReceived $event): void
{
match ($event->event) {
'payment.completed' => $this->handlePaymentCompleted($event),
'payment.failed' => $this->handlePaymentFailed($event),
'refund.completed' => $this->handleRefundCompleted($event),
default => null,
};
}
private function handlePaymentCompleted(WebhookReceived $event): void
{
$transactionId = $event->getTransactionId();
$amount = $event->getAmount();
$currency = $event->getCurrency();
// Update your database
// Send confirmation email
// etc.
}
private function handlePaymentFailed(WebhookReceived $event): void
{
// Handle failed payment
}
private function handleRefundCompleted(WebhookReceived $event): void
{
// Handle completed refund
}
}
use OwnPay\Laravel\Facades\OwnPay;
// List all transactions
$transactions = OwnPay::listTransactions();
// List with filters
$transactions = OwnPay::listTransactions([
'status' => 'completed',
'gateway' => 'bkash',
'from' => '2024-01-01',
'to' => '2024-12-31',
'page' => 1,
'per_page' => 50,
]);
// Get specific transaction
$transaction = OwnPay::getTransaction('OP-XXXXX');
// Check if refundable
if ($transaction->isRefundable()) {
// Can create refund
}
use OwnPay\Laravel\Facades\OwnPay;
// Create customer
$customer = OwnPay::createCustomer([
'name' => 'John Doe',
'email' => '[email protected]',
'phone' => '+8801700000000',
]);
// List customers
$customers = OwnPay::listCustomers(['page' => 1]);
// Get customer by email or phone
$customer = OwnPay::getCustomer('[email protected]');
use OwnPay\Laravel\Facades\OwnPay;
// List API keys
$keys = OwnPay::listApiKeys();
// Generate new key
$result = OwnPay::generateApiKey([
'name' => 'Production Key',
'scopes' => ['read', 'write'],
]);
echo $result['key']; // Show this to the user ONCE
echo $result['prefix'];
// Revoke key
OwnPay::revokeApiKey($keyId);
use OwnPay\Laravel\Exception\OwnPayExceptionInterface;
use OwnPay\Laravel\Exception\AuthenticationException;
use OwnPay\Laravel\Exception\InvalidRequestException;
use OwnPay\Laravel\Exception\NotFoundException;
use OwnPay\Laravel\Exception\RateLimitException;
use OwnPay\Laravel\Exception\ConnectionException;
use OwnPay\Laravel\Exception\PaymentFailedException;
try {
$payment = OwnPay::createPayment([...]);
} catch (AuthenticationException $e) {
// Invalid API key or insufficient permissions
Log::error('OwnPay auth error: ' . $e->getMessage());
} catch (InvalidRequestException $e) {
// Validation error
$errors = $e->getErrorDetails();
// $errors is an array of {code, message, field}
} catch (NotFoundException $e) {
// Resource not found
} catch (RateLimitException $e) {
// Rate limit exceeded
$retryAfter = $e->getRetryAfter();
} catch (ConnectionException $e) {
// Network error
} catch (OwnPayExceptionInterface $e) {
// Catch all OwnPay exceptions
}