PaymentService.php 1.4 KB

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