v2board/app/Services/PaymentService.php

68 lines
2.1 KiB
PHP
Raw Normal View History

2021-04-28 16:56:08 +08:00
<?php
namespace App\Services;
use App\Models\Payment;
class PaymentService
{
2021-05-26 00:38:57 +08:00
public $method;
protected $class;
protected $config;
protected $payment;
2021-05-07 00:17:55 +08:00
public function __construct($method, $id = NULL, $uuid = NULL)
2021-04-28 16:56:08 +08:00
{
$this->method = $method;
$this->class = '\\App\\Payments\\' . $this->method;
if (!class_exists($this->class)) abort(500, 'gate is not found');
if ($id) $payment = Payment::find($id)->toArray();
2021-05-07 00:17:55 +08:00
if ($uuid) $payment = Payment::where('uuid', $uuid)->first()->toArray();
2021-04-28 16:56:08 +08:00
$this->config = [];
2021-04-29 00:25:11 +08:00
if (isset($payment)) {
2021-08-06 00:43:01 +08:00
$this->config = $payment['config'];
2021-04-29 00:25:11 +08:00
$this->config['enable'] = $payment['enable'];
$this->config['id'] = $payment['id'];
2021-05-07 00:17:55 +08:00
$this->config['uuid'] = $payment['uuid'];
$this->config['notify_domain'] = $payment['notify_domain'];
2021-04-29 00:25:11 +08:00
};
2021-04-28 16:56:08 +08:00
$this->payment = new $this->class($this->config);
}
public function notify($params)
{
if (!$this->config['enable']) abort(500, 'gate is not enable');
return $this->payment->notify($params);
}
public function pay($order)
{
// custom notify domain name
2022-05-09 23:26:59 +08:00
$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'];
}
2021-04-28 16:56:08 +08:00
return $this->payment->pay([
'notify_url' => $notifyUrl,
2022-05-09 23:26:59 +08:00
'return_url' => config('v2board.app_url') . '/#/order/' . $order['trade_no'],
2021-04-28 16:56:08 +08:00
'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 ($keys as $key) {
if (isset($this->config[$key])) $form[$key]['value'] = $this->config[$key];
}
return $form;
}
}