PaymentService.php 2.2 KB

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