PaymentService.php 1.6 KB

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