Xboard/app/Services/PaymentService.php
xboard 0f43fff242
Some checks are pending
Docker Build and Publish / build (push) Waiting to run
feat: new xboard
2025-01-21 14:57:54 +08:00

77 lines
2.4 KiB
PHP

<?php
namespace App\Services;
use App\Exceptions\ApiException;
use App\Models\Payment;
class PaymentService
{
public $method;
protected $class;
protected $config;
protected $payment;
public function __construct($method, $id = NULL, $uuid = NULL)
{
$this->method = $method;
$this->class = '\\App\\Payments\\' . $this->method;
if (!class_exists($this->class)) throw new ApiException('gate is not found');
if ($id) $payment = Payment::find($id)->toArray();
if ($uuid) $payment = Payment::where('uuid', $uuid)->first()->toArray();
$this->config = [];
if (isset($payment)) {
$this->config = $payment['config'];
$this->config['enable'] = $payment['enable'];
$this->config['id'] = $payment['id'];
$this->config['uuid'] = $payment['uuid'];
$this->config['notify_domain'] = $payment['notify_domain'];
};
$this->payment = new $this->class($this->config);
}
public function notify($params)
{
if (!$this->config['enable']) throw new ApiException('gate is not enable');
return $this->payment->notify($params);
}
public function pay($order)
{
// custom notify domain name
$notifyUrl = url("/api/v1/guest/payment/notify/{$this->method}/{$this->config['uuid']}");
if ($this->config['notify_domain']) {
$parseUrl = parse_url($notifyUrl);
$notifyUrl = $this->config['notify_domain'] . $parseUrl['path'];
}
return $this->payment->pay([
'notify_url' => $notifyUrl,
'return_url' => url('/#/order/' . $order['trade_no']),
'trade_no' => $order['trade_no'],
'total_amount' => $order['total_amount'],
'user_id' => $order['user_id'],
'stripe_token' => $order['stripe_token']
]);
}
public function form()
{
$form = $this->payment->form();
$keys = array_keys($form);
foreach ($form as $key => $field) {
$form[$key] = [
'label' => $field['label'],
'field_name' => $key,
'field_type' => $field['type'],
'type' => $field['type'],
'placeholder' => $field['placeholder'] ?? '',
'value' => $this->config[$key] ?? '',
'select_options' => $field['select_options'] ?? [],
];
}
return $form;
}
}