PaymentService.php 1.6 KB

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