| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- namespace app\api\controller\v1;
- use app\api\controller\Api;
- use app\common\logic\Pay as PayLogic;
- use app\common\logic\PayCallback;
- use app\common\model\Members;
- use app\common\model\ComplexPay;
- use app\common\model\PayCpinfo;
- use think\Controller;
- use think\Db;
- use think\Log;
- use app\common\model\ComplexMembers;
- class WanCmsNotify extends Controller
- {
- public function index()
- {
- $param = request()->param();
- if (!isset($param['type'])) {
- throw new \Exception("路由有误");
- }
- if (!is_dir(RUNTIME_PATH . 'wancms')) {
- mkdir(RUNTIME_PATH . 'wancms', 0777);
- }
- $logPath = RUNTIME_PATH . 'wancms/log.txt';
- if (!file_exists($logPath)) {
- touch($logPath);
- chmod($logPath, 0777);
- }
- file_put_contents($logPath, var_export($param, true), FILE_APPEND);
- file_put_contents($logPath, json_encode($param, JSON_UNESCAPED_UNICODE), FILE_APPEND);
- if (empty($param)) {
- file_put_contents($logPath, "参数为空" . "\n", FILE_APPEND);
- echo "error";
- die;
- }
- // $param = '{"orderid":"16370446509707","username":"gb713555299","gameid":"3237","roleid":"10086","serverid":"888","paytype":"ptb","amount":"1","paytime":"1637044650","attach":"CMWL16370446471004179310","sign":"04a0b6ad8e1b1ff3f3f52d4d5b871049","type":"2"}';
- // $param =json_decode($param,true);
- // if ($param['type'] == 1) { // 1. 安卓 2.ios
- // $appKey = "01c28224724dac701e06b90238ec7880";
- // } else {
- // $appKey = "558f7d1bfa36f9b3fb5fa81cd18262a8";
- // }
- $appKey = 'b61f7d6da1c424d74c0b9f385dc3216c';
- // 验签
- $signString = "orderid=" . $param['orderid'] . "&";
- $signString .= "username=" . $param['username'] . "&";
- $signString .= "gameid=" . $param['gameid'] . "&";
- $signString .= "roleid=" . $param['roleid'] . "&";
- $signString .= "serverid=" . $param['serverid'] . "&";
- $signString .= "paytype=" . $param['paytype'] . "&";
- $signString .= "amount=" . $param['amount'] . "&";
- $signString .= "paytime=" . $param['paytime'] . "&";
- $signString .= "attach=" . $param['attach'] . "&";
- $signString .= "appkey=" . $appKey;
- $signGen = strtolower(md5(urldecode($signString)));
- if ($signGen != $param['sign']) {
- // file_put_contents($logPath, "签名错误", FILE_APPEND);
- echo "errorSign";
- die;
- }
- $this->updateOrder($param['attach'], $param['amount']);
- echo "success";
- }
- /**
- * 更新订单信息
- *
- * @param string $orderid :订单编号
- * @param string $real_amount :第三方支付的金额(现金部分)
- *
- * @return boolean
- */
- public function updateOrder($orderid, $real_amount = '')
- {
- $complexPayModel = new ComplexPay();
- $payCpinfoModel = new PayCpinfo;
- $membersModel = new ComplexMembers();
- $result = false;
- //消费订单信息
- $payInfo = $complexPayModel->field('id,status,amount,userid,gameid,channel_id')->where(['orderid' => $orderid])->find();
- if ($real_amount != '' && $payInfo['amount'] != $real_amount) {
- $result = false;
- //写入日志
- log_message('订单编号:' . $orderid . '支付金额不一致,订单状态更新失败', 'error', LOG_PATH . '../paylog/');
- } //待支付状态时
- elseif ($payInfo['status'] == 0) {
- // 启动事务
- Db::startTrans();
- try {
- $complexPayModel->where(['id' => $payInfo['id']])->update(['status' => 1, 'pay_time' => NOW_TIMESTAMP]);
- //cy_paycpinfo表记录状态更新
- $payCpinfoModel->where(['orderid' => $orderid])->update(['payflag' => 1,'update_time'=>NOW_TIMESTAMP]);
- $result = true;
- // 提交事务
- Db::commit();
- } catch (\Exception $e) {
- // 回滚事务
- Db::rollback();
- $result = false;
- //写入日志
- log_message('订单编号:' . $orderid . '订单状态更新失败' . $e->getMessage(), 'error', LOG_PATH . '../paylog/');
- }
- }
- //订单状态更新成功
- if ($result) {
- //更新member表的total_pay_amount
- $membersModel->where(['id' => $payInfo['userid']])->update(['total_pay_amount' => Db::raw('total_pay_amount+' . $payInfo['amount'])]);
- // 通知CP,支付成功
- (new PayCallback())->callBackToCp($orderid);
- //预警邮件
- (new PayLogic())->payWarning($payInfo['userid'], $payInfo['channel_id'], $payInfo['gameid'], $payInfo['amount']);
- }
- }
- }
|