| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\common\controllers;
- use app\models\Option;
- use app\modules\common\models\BytedanceNotifyForm;
- use yii\base\BaseObject;
- use yii\helpers\Json;
- use yii\web\Controller;
- class ByteDanceNotifyController extends Controller
- {
- // public $token = null;
- public $token = 'Tianxin1002021';
- public function actionIndex() {
- $notify = post_params();
- // $notify = [
- // '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"}',
- // 'msg_signature' => '51decafe6f54565446cd1e5bfb791200157f335f',
- // 'type' => 'payment',
- // 'timestamp' => '1634972897',
- // 'nonce' => '4387',
- // ];
- // \Yii::warning($notify);
- if (empty($notify['msg'])) {
- \Yii::warning('抖音支付回调消息体为空');
- return;
- }
- $msg = Json::decode($notify['msg']);
- $store_id = Json::decode(stripslashes($msg['cp_extra']))['store_id'];
- if ($store_id > 0) {
- $bytedance_config = Option::get('douyin', $store_id, 'store');
- if (!empty($bytedance_config['value'])) {
- $bytedance_config = Json::decode($bytedance_config['value']);
- $this->token = $bytedance_config['token'];
- }
- } else {
- $bytedance_config = Option::getSaasPlatformBytedance();
- $this->token = $bytedance_config['token'];
- }
- \Yii::warning([$store_id, $this->token]);
- if ($notify['msg_signature'] !== $this->getNotifySign($notify, $this->token)) {
- \Yii::warning('回调验签错误');
- } else {
- \Yii::warning('验签成功');
- if ($msg['status'] != 'SUCCESS') {
- \Yii::error(['订单回调失败,结果为:', $msg]);
- return;
- }
- // way: 1:微信 2:支付宝
- // 处理订单
- $orderNoHead = substr($msg['cp_orderno'], 0, 2);
- if ($msg['way'] == 1) {
- $pay_type = 1; // 微信
- }
- if ($msg['way'] == 2) {
- $pay_type = 4; // 支付宝
- }
- $notify = new BytedanceNotifyForm();
- switch ($orderNoHead) {
- case 'UN':
- // 合并支付的订单
- $notify->UnionOrderNotify($msg, $pay_type);
- break;
- case 'RG':
- // 充值订单
- $notify->RechargeOrderNotify($msg, $pay_type);
- break;
- case 'LV':
- // 会员购买
- $notify->LevelOrderNotify($msg, $pay_type);
- break;
- case 'ML':
- // 商城订单
- $notify->MallOrderNotify($msg, $pay_type);
- break;
- case 'MC':
- // 入住商提现订单
- $notify->batchTransNotify($msg, $pay_type);
- break;
- case 'SC':
- // 当面付订单
- $notify->ScanOrderNotify($msg, $pay_type);
- break;
- case 'FO':
- // 点餐订单
- $notify->FoodNotify($msg, $pay_type);
- break;
- default:
- break;
- }
- }
- $data = ['err_no' => '0', 'err_tips' => 'success'];
- return Json::encode($data);
- }
- /**
- * 生成签名
- * @param array $body
- * @param string $secret
- * @return string
- */
- private function getNotifySign(array $body, string $secret) {
- $filtered = [];
- foreach ($body as $key => $value) {
- if (in_array($key, ['msg_signature', 'type'])) {
- continue;
- }
- $filtered[] =
- is_string($value)
- ? trim($value)
- : $value;
- }
- $filtered[] = trim($secret);
- sort($filtered, SORT_STRING);
- $filtered = trim(implode('', $filtered));
- return sha1($filtered);
- }
- }
|