Payment.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. \Log::info('快手支付回调==>'.json_encode($notify,JSON_UNESCAPED_SLASHES));
  51. if($kwaisign !== $this->app->getNotifySign($notify)){
  52. //throw new \Exception('签名验证错误');
  53. }
  54. return $notify['data'];
  55. }
  56. /**
  57. * 返回数据
  58. * @return \Illuminate\Http\JsonResponse
  59. */
  60. private function toResponse()
  61. {
  62. $data = [
  63. 'result' => is_null($this->fail) ? 1 : 0,
  64. 'message_id' => $this->messageId,
  65. 'fail' => $this->fail
  66. ];
  67. return response()->json($data);
  68. }
  69. }