12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- <?php
- namespace App\Services;
- use App\Models\Payment;
- class PaymentService
- {
- public $method;
- public $customResult;
- 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)) abort(500, '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 = json_decode($payment['config'], true);
- $this->config['enable'] = $payment['enable'];
- $this->config['id'] = $payment['id'];
- $this->config['uuid'] = $payment['uuid'];
- };
- $this->payment = new $this->class($this->config);
- if (isset($this->payment->customResult)) $this->customResult = $this->payment->customResult;
- }
- public function notify($params)
- {
- if (!$this->config['enable']) abort(500, 'gate is not enable');
- return $this->payment->notify($params);
- }
- public function pay($order)
- {
- return $this->payment->pay([
- 'notify_url' => url("/api/v1/guest/payment/notify/{$this->method}/{$this->config['uuid']}"),
- 'return_url' => config('v2board.app_url', env('APP_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 ($keys as $key) {
- if (isset($this->config[$key])) $form[$key]['value'] = $this->config[$key];
- }
- return $form;
- }
- }
|