PaymentService.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. };
  25. $this->payment = new $this->class($this->config);
  26. if (isset($this->payment->customResult)) $this->customResult = $this->payment->customResult;
  27. }
  28. public function notify($params)
  29. {
  30. if (!$this->config['enable']) abort(500, 'gate is not enable');
  31. return $this->payment->notify($params);
  32. }
  33. public function pay($order)
  34. {
  35. return $this->payment->pay([
  36. 'notify_url' => url("/api/v1/guest/payment/notify/{$this->method}/{$this->config['uuid']}"),
  37. 'return_url' => config('v2board.app_url', env('APP_URL')) . '/#/order/' . $order['trade_no'],
  38. 'trade_no' => $order['trade_no'],
  39. 'total_amount' => $order['total_amount'],
  40. 'user_id' => $order['user_id'],
  41. 'stripe_token' => $order['stripe_token']
  42. ]);
  43. }
  44. public function form()
  45. {
  46. $form = $this->payment->form();
  47. $keys = array_keys($form);
  48. foreach ($keys as $key) {
  49. if (isset($this->config[$key])) $form[$key]['value'] = $this->config[$key];
  50. }
  51. return $form;
  52. }
  53. }