ByteDanceNotifyController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\common\controllers;
  8. use app\models\Option;
  9. use app\modules\common\models\BytedanceNotifyForm;
  10. use yii\base\BaseObject;
  11. use yii\helpers\Json;
  12. use yii\web\Controller;
  13. class ByteDanceNotifyController extends Controller
  14. {
  15. // public $token = null;
  16. public $token = 'Tianxin1002021';
  17. public function actionIndex() {
  18. $notify = post_params();
  19. // $notify = [
  20. // 'msg' => '{"appid":"tt6236148bfd1c52fb","cp_orderno":"2834823949234923904","cp_extra":"{\\"store_id\\":\\"1\\"}","way":"1","channel_no":"4334501242202110232721400286","channel_gateway_no":"","payment_order_no":"PC2021102315074003985945718245","out_channel_order_no":"","total_amount":1,"status":"SUCCESS","seller_uid":"69513213815313553990","extra":"null","item_id":"","paid_at":1634972898,"message":"","order_id":"7022153160022100254"}',
  21. // 'msg_signature' => '51decafe6f54565446cd1e5bfb791200157f335f',
  22. // 'type' => 'payment',
  23. // 'timestamp' => '1634972897',
  24. // 'nonce' => '4387',
  25. // ];
  26. // \Yii::warning($notify);
  27. if (empty($notify['msg'])) {
  28. \Yii::warning('抖音支付回调消息体为空');
  29. return;
  30. }
  31. $msg = Json::decode($notify['msg']);
  32. $store_id = Json::decode(stripslashes($msg['cp_extra']))['store_id'];
  33. if ($store_id > 0) {
  34. $bytedance_config = Option::get('douyin', $store_id, 'store');
  35. if (!empty($bytedance_config['value'])) {
  36. $bytedance_config = Json::decode($bytedance_config['value']);
  37. $this->token = $bytedance_config['token'];
  38. }
  39. } else {
  40. $bytedance_config = Option::getSaasPlatformBytedance();
  41. $this->token = $bytedance_config['token'];
  42. }
  43. \Yii::warning([$store_id, $this->token]);
  44. if ($notify['msg_signature'] !== $this->getNotifySign($notify, $this->token)) {
  45. \Yii::warning('回调验签错误');
  46. } else {
  47. \Yii::warning('验签成功');
  48. if ($msg['status'] != 'SUCCESS') {
  49. \Yii::error(['订单回调失败,结果为:', $msg]);
  50. return;
  51. }
  52. // way: 1:微信 2:支付宝
  53. // 处理订单
  54. $orderNoHead = substr($msg['cp_orderno'], 0, 2);
  55. if ($msg['way'] == 1) {
  56. $pay_type = 1; // 微信
  57. }
  58. if ($msg['way'] == 2) {
  59. $pay_type = 4; // 支付宝
  60. }
  61. $notify = new BytedanceNotifyForm();
  62. switch ($orderNoHead) {
  63. case 'UN':
  64. // 合并支付的订单
  65. $notify->UnionOrderNotify($msg, $pay_type);
  66. break;
  67. case 'RG':
  68. // 充值订单
  69. $notify->RechargeOrderNotify($msg, $pay_type);
  70. break;
  71. case 'LV':
  72. // 会员购买
  73. $notify->LevelOrderNotify($msg, $pay_type);
  74. break;
  75. case 'ML':
  76. // 商城订单
  77. $notify->MallOrderNotify($msg, $pay_type);
  78. break;
  79. case 'MC':
  80. // 入住商提现订单
  81. $notify->batchTransNotify($msg, $pay_type);
  82. break;
  83. case 'SC':
  84. // 当面付订单
  85. $notify->ScanOrderNotify($msg, $pay_type);
  86. break;
  87. case 'FO':
  88. // 点餐订单
  89. $notify->FoodNotify($msg, $pay_type);
  90. break;
  91. default:
  92. break;
  93. }
  94. }
  95. $data = ['err_no' => '0', 'err_tips' => 'success'];
  96. return Json::encode($data);
  97. }
  98. /**
  99. * 生成签名
  100. * @param array $body
  101. * @param string $secret
  102. * @return string
  103. */
  104. private function getNotifySign(array $body, string $secret) {
  105. $filtered = [];
  106. foreach ($body as $key => $value) {
  107. if (in_array($key, ['msg_signature', 'type'])) {
  108. continue;
  109. }
  110. $filtered[] =
  111. is_string($value)
  112. ? trim($value)
  113. : $value;
  114. }
  115. $filtered[] = trim($secret);
  116. sort($filtered, SORT_STRING);
  117. $filtered = trim(implode('', $filtered));
  118. return sha1($filtered);
  119. }
  120. }