ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

fastadmin支付插件

2021-09-27 16:34:04  阅读:319  来源: 互联网

标签:插件 return trade result fastadmin 支付 type id recharge


public function rechargePurchasing(){
     $user_id = $this->auth->id;
     $userOpenid = Db::name('***')->where('user_id','=',$user_id)->value('openid');
     $recharge_id = $this->request->request('recharge_id',1);
     $recharge_find = Db::name('*')->where('id','=',$recharge_id)->find();
     if(empty($recharge_find)){
         return $this->error('充值套餐不存在');
     }
     $amount = $recharge_find['money'];
     /*//MWEB
     $platfrom = $this->request->header('platform', 'MP-WEIXIN');

     switch ($platfrom) {
         case 'MP-WEIXIN':
             $trade_type = 'JSAPI';
             break;
         case 'H5':
             $trade_type = 'MWEB';
             break;
         case 'APP-PLUS':
             $trade_type = 'APP';
             break;
     }
     // 如果是微信内访问 公众号等
     if (Wechat::h5InWechat()) {
         $trade_type = 'JSAPI';
     }*/
     $trade_type = 'JSAPI';
     $body = $recharge_find['name'];
     
     // 如果 JSAPI 必须传openid、
     $appid = Config::getByName('app_id')['value'];
     if (empty($userOpenid)) {
         $this->success('', array(
             'weixinOauth2' =>
                 "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=".urlencode("https://$_SERVER[HTTP_HOST]/addons/unishop/pay/weixinOauth2")."&response_type=code&scope=snsapi_base&state=".$this->request->request('order_id', $user_id)."#wechat_redirect"
         ,'trade_type' => 'JSAPI'));
     }
     
     // 生成功支付参数
     $orderOutNo = 'v'.time().mt_rand(111111,999999);
     $orderid = Db::name('recharge_records')->insertGetId([
         'ordersn' => $orderOutNo,
         'createtime' => time()
     ]);
     if(!$orderid) return $this->error('出错了,请稍后试试');
     
     // $app = Wechat::initEasyWechat('payment');
     // $result = $app->order->unify([
     //     'body' => $body,
     //     'out_trade_no' => $orderOutNo,
     //     'total_fee' => bcmul($amount,100),
     //     'spbill_create_ip' => $_SERVER['REMOTE_ADDR'], // 可选,如不传该参数,SDK 将会自动获取相应 IP 地址
     //     'trade_type' => $trade_type, // 请对应换成你的支付方式对应的值类型
     //     'openid' => $userOpenid
     // ]);
     // if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
     //     if ($trade_type == 'JSAPI') {
     //         // 二次签名
     //         $result['timeStamp'] = (string)time();
     //         $result['paySign'] = Wechat::paySign([
     //             'appId' => $appid,
     //             'nonceStr' => $result['nonce_str'],
     //             'package' => 'prepay_id='.$result['prepay_id'],
     //             'timeStamp' => $result['timeStamp'],
     //             'signType' => 'MD5'
     //         ], Config::getByName('key')['value']);
     //     }
     // } else {
     //     $this->error($result['return_msg']);
     // }
     // $result['nonceStr'] = $result['nonce_str'];
     $params = [
         //'amount'    => $amount,
         'amount'    => $amount,
         'orderid'   => $orderOutNo,
         'type'      => "wechat",
         'title'     => "充值",
         'notifyurl' => "https:/***",
         'method'    => "mp",
         'openid'    => $userOpenid,
     ];
     $payRes = payUtils::submitOrder($params);
     $payRes['prepay_id'] = str_replace('prepay_id=','',$payRes['package']);
     return $this->success('请支付',['payRes'=>$payRes]);
     $this->success('', ['payRes'=>$result]);
 }
 
 public function serviceWeichatNotify(){
     //获取返回的xml
     $testxml = file_get_contents("php://input");
     //将xml转化为json格式
     $jsonxml = json_encode(simplexml_load_string($testxml, 'SimpleXMLElement', LIBXML_NOCDATA));
     //转成数组
     $result = json_decode($jsonxml, true);
     writeLog('支付回调成功,订单:'.$result['out_trade_no'].',参数:'.json_encode($result),'serviceWeichatNotify');
     if($result){
         //如果成功返回了
         if($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'){
             $order = Db::name('recharge_records')->where(['ordersn'=>$result['out_trade_no']])->find();
             if(!$order){
                 writeLog('支付回调处理失败,订单:'.$result['out_trade_no'].',原因,数据库未找到订单','serviceWeichatNotify');
                 return false;
             }
             if($order['is_pay']==0){
                 Db::startTrans();
                 try {
                     $recharge_find = Db::name('recharge')->where('id','=',$order['recharge_id'])->find();
                     if(empty($recharge_find)){
                         writeLog('支付回调处理失败,订单:'.$result['out_trade_no'].',原因,套餐不存在','serviceWeichatNotify');
                     }
                     
                     
                     // 更新订单
                     $upOrder = Db::name('recharge_records')->where(['ordersn'=>$result['out_trade_no']])->update([
                         'is_pay' => 1,
                         'pay_time' => date('Y-m-d H:i:s',strtotime($result['time_end']))
                     ]);
                     if(!$upOrder) throw new \Exception("订单更新失败");
                     Db::commit();  
                 } catch (\Exception $e) {
                     Db::rollback();
                     writeLog('用户'.$order['user_id'].'充值采购金微信回调失败,原因:'.$e->getMessage(),'balanceWeichatNotify');
                     return false;
                 }
             }
             return true;
         }
     }else{
         return false;
     }
 }

标签:插件,return,trade,result,fastadmin,支付,type,id,recharge
来源: https://blog.csdn.net/whaxkl/article/details/120511176

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有