其他分享
首页 > 其他分享> > fastadmin支付插件

fastadmin支付插件

作者:互联网

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