PaymentService.php 1.7 KB

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