Payment.php 1.8 KB

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