Files
Xboard/app/Services/PaymentService.php
xboard 508caebdcd refactor: refactor subscription delivery logic, change payment return_url to origin_url concatenation
- Unify protocol filter configuration to client.type.field (dot-path, three-segment) format, support strict whitelist mode
- Refactor AbstractProtocol and all protocol classes for more flexible and maintainable subscription delivery
- Change payment callback logic: use origin_url concatenation instead of return_url for more accurate redirects
2025-07-18 15:42:58 +08:00

82 lines
2.5 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' => origin_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;
}
}