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;
     }
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

whaxkl

该打赏续费了

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值