PaymentService.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Payment;
  4. class PaymentService
  5. {
  6. public $method;
  7. protected $class;
  8. protected $config;
  9. protected $payment;
  10. public function __construct($method, $id = NULL, $uuid = NULL)
  11. {
  12. $this->method = $method;
  13. $this->class = '\\App\\Payments\\' . $this->method;
  14. if (!class_exists($this->class)) abort(500, 'gate is not found');
  15. if ($id) $payment = Payment::find($id)->toArray();
  16. if ($uuid) $payment = Payment::where('uuid', $uuid)->first()->toArray();
  17. $this->config = [];
  18. if (isset($payment)) {
  19. $this->config = $payment['config'];
  20. $this->config['enable'] = $payment['enable'];
  21. $this->config['id'] = $payment['id'];
  22. $this->config['uuid'] = $payment['uuid'];
  23. $this->config['notify_domain'] = $payment['notify_domain'];
  24. };
  25. $this->payment = new $this->class($this->config);
  26. }
  27. public function notify($params)
  28. {
  29. if (!$this->config['enable']) abort(500, 'gate is not enable');
  30. return $this->payment->notify($params);
  31. }
  32. public function pay($order)
  33. {
  34. // custom notify domain name
  35. $notifyUrl = config('v2board.app_url', env('APP_URL')) . "/api/v1/guest/payment/notify/{$this->method}/{$this->config['uuid']}";
  36. if ($this->config['notify_domain']) {
  37. $parseUrl = parse_url($notifyUrl);
  38. $notifyUrl = $this->config['notify_domain'] . $parseUrl['path'];
  39. }
  40. return $this->payment->pay([
  41. 'notify_url' => $notifyUrl,
  42. 'return_url' => config('v2board.app_url', env('APP_URL')) . '/#/order/' . $order['trade_no'],
  43. 'trade_no' => $order['trade_no'],
  44. 'total_amount' => $order['total_amount'],
  45. 'user_id' => $order['user_id'],
  46. 'stripe_token' => $order['stripe_token']
  47. ]);
  48. }
  49. public function form()
  50. {
  51. $form = $this->payment->form();
  52. $keys = array_keys($form);
  53. foreach ($keys as $key) {
  54. if (isset($this->config[$key])) $form[$key]['value'] = $this->config[$key];
  55. }
  56. return $form;
  57. }
  58. }