Payment.php 2.4 KB

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