Payment.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Helper\UniPlatform\Bytedance;
  3. use App\Helper\ByteDance;
  4. use App\Models\Pay;
  5. /**
  6. * Class Payment.
  7. *
  8. * @property ByteDance $app
  9. */
  10. class Payment
  11. {
  12. private $app;
  13. private $fail;
  14. public function __construct(ByteDance $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. // $notify = '{"msg":"{\"appid\":\"tt5b312d8cc40f46b701\",\"cp_orderno\":\"10022082800824490007\",\"cp_extra\":\"\",\"way\":\"2\",\"channel_no\":\"2022082822001477591433078541\",\"channel_gateway_no\":\"\",\"payment_order_no\":\"PCP2022082822540531221069106962\",\"out_channel_order_no\":\"2022082822001477591433078541\",\"total_amount\":1,\"status\":\"SUCCESS\",\"seller_uid\":\"71227862181355706950\",\"extra\":\"\",\"item_id\":\"\",\"paid_at\":1661698458,\"message\":\"\",\"order_id\":\"7136939355201063205\",\"trade_item_list\":null,\"ec_pay_trade_no\":\"NEP2022082822540409483061846962\"}","msg_signature":"804a60e48936b14a739230cef21fe6204427732e","nonce":"78","timestamp":"1661698459","type":"payment"}';
  45. // 获取订单信息
  46. // $notify = json_decode($notify,true);
  47. \Log::info('抖音支付回调==>' . json_encode($notify, JSON_UNESCAPED_SLASHES));
  48. if ($notify['msg_signature'] !== $this->app->getNotifySign($notify)) {
  49. throw new \Exception('签名验证错误');
  50. }
  51. return json_decode($notify['msg'], true);
  52. }
  53. /**
  54. * 返回数据.
  55. *
  56. * @return \Illuminate\Http\JsonResponse
  57. */
  58. private function toResponse()
  59. {
  60. $data = [
  61. 'err_no' => is_null($this->fail) ? 0 : 1,
  62. 'err_tips' => is_null($this->fail) ? 'success' : $this->fail,
  63. ];
  64. return response()->json($data);
  65. }
  66. }