Payment.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Helper\UniPlatform\Kuaishou;
  3. use App\Helper\Kuaishou;
  4. /**
  5. * Class Payment.
  6. *
  7. * @property Kuaishou $app
  8. */
  9. class Payment
  10. {
  11. private $app;
  12. private $messageId = '';
  13. private $fail;
  14. public function __construct(Kuaishou $app)
  15. {
  16. $this->app = $app;
  17. }
  18. /**
  19. * 支付通知.
  20. *
  21. * @return \Illuminate\Http\JsonResponse
  22. */
  23. public function payNotify(\Closure $closure)
  24. {
  25. try {
  26. call_user_func($closure, $this->getNoticeData(), [$this, 'fail']);
  27. } catch (\Exception $e) {
  28. $this->fail($e->getMessage());
  29. }
  30. return $this->toResponse();
  31. }
  32. public function fail($msg)
  33. {
  34. $this->fail = $msg;
  35. }
  36. /**
  37. * 获取支付通知数据.
  38. *
  39. * @throws \Exception
  40. */
  41. private function getNoticeData()
  42. {
  43. $notify = \request()->all();
  44. $kwaisign = \request()->header('kwaisign');
  45. $this->messageId = $notify['message_id'];
  46. // 获取订单信息
  47. $notify = array_merge($notify, ['kwaisign' => $kwaisign]);
  48. \Log::info('快手支付回调==>' . json_encode($notify, JSON_UNESCAPED_SLASHES));
  49. if ($kwaisign !== $this->app->getNotifySign($notify)) {
  50. throw new \Exception('签名验证错误');
  51. }
  52. return $notify['data'];
  53. }
  54. /**
  55. * 返回数据.
  56. *
  57. * @return \Illuminate\Http\JsonResponse
  58. */
  59. private function toResponse()
  60. {
  61. $data = [
  62. 'result' => is_null($this->fail) ? 1 : 0,
  63. 'message_id' => $this->messageId,
  64. 'fail' => $this->fail,
  65. ];
  66. return response()->json($data);
  67. }
  68. }