| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <?php
- namespace app\api\controller\v1;
- use app\common\logic\Pay as PayLogic;
- use app\common\logic\PayCallback;
- use app\common\model\ComplexMembers;
- use app\common\model\ComplexPay;
- use app\common\model\Members;
- use app\common\model\PayCpinfo;
- use think\Controller;
- use think\Db;
- use think\Exception;
- use think\Request;
- use app\service\QuickService;
- class QuickCallback extends Controller
- {
- protected $dir_name = "quick";
- const MD5KEY = "d0ekhdx5klc2zoelfa3jfaftkdzxgvdk"; // 自己的Quick md5key
- const CALLBACK_KEY = "95051247812538717836001325967096";//自己的callback_key
- public function index(Request $request)
- {
- $param = $request->param();
- // $param = [
- // "nt_data" => '@108@119@174@165@158@82@167@152@171@166@161@162@159@115@90@106@94@103@83@85@154@166@156@161@155@160@166@155@118@90@139@141@118@101@110@90@82@165@165@148@167@151@153@159@160@164@157@118@82@165@160@87@116@118@117@165@162@176@165@163@168@166@169@152@157@157@169@171@147@153@150@113@117@160@157@166@164@151@159@158@110@115@166@158@153@118@109@110@102@172@161@152@119@116@162@168@151@161@164@151@160@147@158@152@119@164@169@164@162@167@169@117@95@163@160@156@158@166@152@160@152@164@157@114@117@167@171@173@143@167@168@156@151@164@144@161@168@113@116@98@160@171@172@152@159@169@149@154@167@151@167@161@117@115@167@166@157@157@168@152@158@167@116@104@99@98@97@101@105@100@108@100@98@102@109@106@101@106@100@101@102@105@106@100@111@103@107@103@117@103@165@171@148@157@168@151@160@161@111@111@169@148@177@146@165@159@165@158@110@105@97@102@105@101@106@99@100@103@109@84@106@109@112@108@99@114@103@108@110@97@161@148@178@146@172@156@158@155@118@117@145@164@160@170@163@172@119@98@101@103@105@112@104@153@163@168@165@166@170@118@110@165@165@148@173@168@171@113@97@114@103@172@164@152@165@170@168@118@117@151@175@171@170@149@172@151@166@154@162@153@163@171@112@170@169@171@117@98@157@171@165@168@153@172@143@167@146@167@150@165@172@112@115@102@165@153@172@171@151@160@149@118@114@103@165@157@170@160@168@162@166@166@144@163@157@172@163@152@152@154@115',
- // "sign" => '@104@155@109@158@105@104@104@106@111@103@112@103@148@152@104@158@149@111@101@151@102@111@156@149@155@112@113@105@154@110@108@106',
- // "md5Sign" => '07dae8d71374651c1f0ee4cc16a37f6e',
- // ];
- $this->log_write_message("quick回调:", $param);
- if (empty($param)) {
- throw new Exception("参数错误");
- }
- if (!array_key_exists("nt_data", $param)) {
- $this->log_write_message("quick异常抛出:", "nt_data参数不存在");
- throw new Exception("nt_data参数不存在");
- }
- if (!array_key_exists("sign", $param)) {
- $this->log_write_message("quick异常抛出:", "sign参数不存在");
- throw new Exception("sign参数不存在");
- }
- if (!array_key_exists("md5Sign", $param)) {
- $this->log_write_message("quick异常抛出:", "md5Sign参数不存在");
- throw new Exception("md5Sign参数不存在");
- }
- $md5Sign = $param['md5Sign'];
- unset($param['md5Sign']);
- // // 验证签名
- if(QuickService::getSign($param,self::MD5KEY) != $md5Sign){
- $this->log_write_message("quick异常抛出:","签名错误" );
- throw new Exception("参数错误");
- }
- // 解密数据
- try {
- $nt_data_xml = QuickService::decode($param['nt_data'], self::CALLBACK_KEY);
- }catch (\Exception $e){
- $this->log_write_message("quick异常抛出:", "nt_data解密出错");
- throw new Exception("nt_data解密出错");
- }
- // 解析xml
- $result = QuickService::xmlToArray($nt_data_xml);
- $result = $result['message'];
- if (!$result) {
- $this->log_write_message("quick异常抛出:", "xml解密出错");
- throw new Exception("xml解密出错");
- }
- if ($result['status'] == 1) { //quick 支付状态判断
- $this->log_write_message("quick异常抛出:", $result['status'] . "在quick方支付失败");
- throw new Exception($result['status'] . "在quick方支付失败");
- }
- $this->updateOrder($result['game_order'], $result['amount'], $result['order_no']);
- echo "SUCCESS";exit;
- }
- public function log_write_message($tip, $param)
- {
- if (!is_dir(RUNTIME_PATH . 'quick')) {
- mkdir(RUNTIME_PATH . 'quick', 0777);
- }
- $logPath = RUNTIME_PATH . 'quick/log.txt';
- if (!file_exists($logPath)) {
- touch($logPath);
- chmod($logPath, 0777);
- }
- file_put_contents($logPath, $tip . ":\n" . var_export($param, true), FILE_APPEND);
- file_put_contents($logPath, $tip . ":\n" . json_encode($param, JSON_UNESCAPED_UNICODE), FILE_APPEND);
- }
- /**
- * 更新订单信息
- *
- * @param string $orderid :订单编号
- * @param string $real_amount :第三方支付的金额(现金部分)
- *
- * @return boolean
- */
- public function updateOrder($orderid, $real_amount = '', $sub_orderid = null)
- {
- $complexPayModel = new ComplexPay();
- $payCpinfoModel = new PayCpinfo;
- $complexMembersModel = 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;
- //写入日志
- $this->log_write_message("quick金额对比", '订单编号:' . $orderid . '支付金额不一致,订单状态更新失败');
- } //待支付状态时
- elseif ($payInfo['status'] == 0) {
- // 启动事务
- Db::startTrans();
- try {
- $complexPayModel->where(['id' => $payInfo['id']])->update(
- [
- 'status' => 1,
- 'sub_orderid' => $sub_orderid,
- '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;
- //写入日志
- $this->log_write_message("quick异常抛出", '订单编号:' . $orderid . '订单状态更新失败' . $e->getMessage());
- }
- }
- //订单状态更新成功
- if ($result) {
- //更新member表的total_pay_amount
- $complexMembersModel->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']);
- }
- }
- }
|