Miscellaneous
Payment Method
Add a new payment Method
Add Payment Method
Create a new payment method in /home/panel/backend/app/Payments. If no errors should display in admin panel payment methods Gateway.
<?php
namespace App\Payments;
use Illuminate\Http\Request;
use App\Services\CurrencyService;
use Illuminate\Support\Facades\Log;
final class MethodName
{
/**
* Create a new Paypal instance.
*
* @param array<string, mixed> $config
*
* Expected must have config keys:
* - currency: Currency code (e.g., 'USD')
* - currency_decimals: Number of decimal places for currency
*/
public function __construct(protected array $config)
{
$this->currencyService = new CurrencyService();
}
/**
* Return the payment form configuration.
*
* @return array<string, array<string, mixed>> The form configuration.
*/
public function paymentForm(): array
{
return [
'name' => [
'label' => 'Payment Method',
'type' => 'text',
'default' => 'MethodName',
'disabled' => true,
],
// Your other form options
'currency' => [
'label' => 'Currency',
'type' => 'text',
'default' => 'USD',
'disabled' => false,
],
'currency_decimals' => [
'label' => 'Decimal Places',
'type' => 'integer',
'default' => 2,
'disabled' => false,
],
];
}
/**
* Create a payment action for an order.
*
* @param array<string, mixed> $order Order details.
* @return array<string, mixed>
*
* - notify_url can be retrieve from $this->config['notify_url']
* Expected order keys:
* - total_amount: The order amount
* - pay_id: Unique payment identifier
* - description : order description
* - email : order email of the user
* - user_id: user id
* - cancel_url : payment cancel return url
* - return_url: order payment successful return url
*/
public function pay(array $order): array
{
$requiredKeys = ['total_amount', 'pay_id'];
foreach ($requiredKeys as $key) {
if (!isset($order[$key])) {
throw new \Exception(__("order.missing_key").$key);
}
}
$rates = $this->currencyService->fetchRates();
$defaultCurrency = config('xmplus.default_currency_code', 'USD'); // Fallback default currency
if (!isset($rates[$this->config['currency']], $rates[$defaultCurrency])) {
throw new \InvalidArgumentException(__('order.invalid_rate'));
}
$value = $rates[$this->config['currency']] / $rates[$defaultCurrency];
$total = number_format((float) $order['total_amount'] * $value, $this->config['currency_decimals'], '.', '');
try {
// Process you payment method api here
return [
'data' => '',
'link' => '',
'total_amount' => $total,
'external' => true,
];
} catch (\Throwable $e) {
Log::error('Paypal: ',[
'message' => $e->getMessage(),
'line' => $e->getLine(),
'file' => $e->getFile(),
]);
abort(422, $e->getMessage());
}
}
/**
* Handle callback notifications.
*
* @param Request $request
* @return array<string, mixed> Notification result with trade_no, callback_no, and code.
*/
public function notify(Request $request)
{
$data = $request->input();
try{
return [
'trade_no' => 'your order id set in payment order',
'callback_no' => 'api callback transaction id',
'code' => 200
];
} catch (\Throwable $e) {
Log::error('Notification exception', [
'message' => $e->getMessage(),
'line' => $e->getLine(),
]);
return [
'code' => 400,
'message' => $e->getMessage(),
];
}
return ['code' => 200, 'status' => 'ok'];
}
}
