| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- /**
- * 支付的服务器异步通知控制器
- *
- */
- namespace app\api\controller\v1;
- use app\api\controller\Api;
- use app\api\service\PayNotifyService;
- use app\common\library\AirwallexPay;
- use app\common\model\Pay;
- use app\common\model\PayCpinfo;
- use app\common\model\Members;
- use app\common\logic\PayCallback;
- use app\common\logic\Pay as PayLogic;
- use app\common\library\WeixinPay;
- use app\guildapi\controller\ChannelRechargeCoin;
- use app\service\TiktokService;
- use think\Config;
- use think\Db;
- use think\Exception;
- class CpsNotify extends Api
- {
- /**
- * 不进行父类的登录验证,所以增加构造方法重写了父类的初始化方法
- */
- public function _initialize()
- {
- $log_postdata = '';
- try {
- $postData = input();
- if (!$postData) {
- $postData = request()->getInput();
- }
- foreach ($postData as $k => $v) {
- $log_postdata .= $k . ':' . $v . ' ';
- }
- } catch (\Exception $e) {
- $log_postdata = json_encode($postData);
- }
- log_message('支付订单回调:' . request()->url() . ' ' . $log_postdata, 'log', LOG_PATH . 'CpsNotify/');
- }
- /**
- * 支付宝Aop支付服务器异步通知页面方法
- */
- public function alipayAop()
- {
- //Alipay Aop Sdk支付回调
- vendor('alipayAop.AopSdk');
- $aop = new \AopClient();
- $public_key = Config::get('ali_pay_config')['configs'][Config::get('ali_pay_config')['default']['zfb']]['public_key'];
- $aop->alipayrsaPublicKey = $public_key;
- if ($result = $aop->rsaCheckV1($_POST, NULL, Config::get('ali_pay_config')['sign_type'])) {
- // 处理支付成功后的逻辑业务
- if (input('post.trade_status') == 'TRADE_SUCCESS') {
- //写入日志
- log_message($result, 'log', LOG_PATH . 'paylog/');
- $out_trade_no = input('out_trade_no'); //订单号
- if (!empty($out_trade_no)) {
- //更新订单状态处理
- $this->updateOrder($out_trade_no, input('total_fee'));
- }
- echo 'success';
- exit; //请不要修改或删除
- }
- echo 'fail';
- exit; //验证失败
- }
- echo 'fail';
- exit;
- }
- /**
- * 酷点h5支付
- */
- public function kdh5zhifu()
- {
- $request_data = file_get_contents('php://input');
- log_message('酷点h5支付回调参数:' . $request_data, 'log', LOG_PATH . '../paylog/');
- vendor('kdpaySdk.des');
- $sumpay = new \kdpay();
- $data = json_decode($request_data, true);
- $res_result = json_decode($data['result'], true);
- if ($res_result['status'] == '100') {
- $retjson = $sumpay->notifyVerify($data);
- if ($retjson == $data['sign']) {
- $orderid = trim($res_result['out_trade_no']);
- $amount = floatval($res_result['fee'] / 100);
- //订单编号不为空时
- if (!empty($orderid)) {
- //更新订单状态处理
- $this->updateOrder($orderid, $amount, $res_result['transaction_id']);
- }
- echo "success";
- die();
- } else {
- echo 'fail';
- exit();
- }
- } else {
- echo '支付失败';
- exit();
- }
- }
- private function updateOrder($orderid, $real_amount = 0, $callback_order = '')
- {
- $channelRechargeCoin = model('ChannelRechargeCoin')->field('id,orderid,channel_id,channel_name,send_channel_id,send_channel_name,amount,real_amount,status')
- ->where(['recharge_type'=>2,'orderid'=>$orderid])->find();
- if ($real_amount != '' && $channelRechargeCoin['real_amount'] != $real_amount) {
- $result = false;
- //写入日志
- log_message('订单编号:' . $orderid . '支付金额不一致,订单状态更新失败', 'error', LOG_PATH . 'cpsnotify/');
- } //待支付状态时
- elseif ($channelRechargeCoin['status'] == 0) {
- // 启动事务
- Db::startTrans();
- try {
- model('ChannelRechargeCoin')->where(['id'=>$channelRechargeCoin['id']])->update(['status'=>1,'callback_order'=>$callback_order]);
- $detData = array();
- $detData['channel_id'] = $channelRechargeCoin['channel_id'];
- $detData['channel_name'] = $channelRechargeCoin['channel_name'];
- $detData['change_amount'] = +$channelRechargeCoin['amount'];
- $detData['account_type'] = 3; //通用账户
- $detData['type'] = 4; //充值收入
- $detData['out_orderid'] = $channelRechargeCoin['orderid'];
- $detData['create_time'] = NOW_TIMESTAMP;
- $insertDetId = model('ChannelAccountDet')->insertGetId($detData);
- if ( !$insertDetId) {
- throw new Exception("添加账户变动明细失败");
- }
- $updData = array();
- $updData['amount_coin'] = Db::raw("amount_coin+".$channelRechargeCoin['amount']);
- $updData['update_time'] = time();
- $updFromResult = model('Channel')->where(['id'=>$channelRechargeCoin['channel_id']])->update($updData);
- if (!$updFromResult) {
- throw new Exception("账户金额变动失败");
- }
- $data['finish_status'] = 1;
- $data['finish_time']= time();
- $result = model('ChannelRechargeCoin')->where(['id'=>$channelRechargeCoin['id'],'status'=>1])->update($data);
- if (!$result) {
- throw new Exception("充值失败");
- }
- // 提交事务
- Db::commit();
- } catch (\Exception $e) {
- // 回滚事务
- Db::rollback(); var_dump( $e->getMessage());
- //写入日志
- log_message('订单编号:' . $orderid . '订单状态更新失败' . $e->getMessage(), 'error', LOG_PATH . 'cpsnotify/');
- }
- }
- }
- }
|