首页 > 开发 > ThinkPHP > 正文

Thinkphp和onethink实现微信支付插件

2020-10-12 21:01:36
字体:
来源:转载
供稿:网友

thinkPHP和微支付实现的微信支付插件,在微信中调用微信jssdk实现支付,这里有详细的代码和教程,具体看这里:

  1. //实现的Wxpay钩子方法 
  2.  
  3.     public function Wxpay($param){ 
  4.  
  5.       require './Addons/Wxpay/WxPayPubHelper/WxPayPubHelper.php'
  6.  
  7.       $jsApi = new /JsApi_pub();  
  8.  
  9.          
  10.  
  11.       //=========步骤1:网页授权获取用户openid============ 
  12.  
  13.       if (!isset($_GET['code'])) 
  14.  
  15.       { 
  16.  
  17.         //触发微信返回code码 
  18.  
  19.         $url = $jsApi->createOauthUrlForCode(/WxPayConf_pub::JS_API_CALL_URL.'/order_id/'.$param['order_id']); 
  20.  
  21.         Header("Location: $url"); 
  22.  
  23.       }else 
  24.  
  25.       { 
  26.  
  27.         $order_id = $param['order_id']; 
  28.  
  29.         $info = M('Order')->where('id='.$order_id)->find(); 
  30.  
  31.         if(emptyempty($info) || $info['is_pay'] == 1){ 
  32.  
  33.           dump('该订单不存在或已支付'.$order_id); 
  34.  
  35.           exit(); 
  36.  
  37.         } 
  38.  
  39.         $this->assign('info'$info); 
  40.  
  41.         $a = $info['money']; 
  42.  
  43.         $b = 100; 
  44.  
  45.         $c = $a * $b
  46.  
  47.            
  48.  
  49.         //获取code码,以获取openid 
  50.  
  51.         $code = $_GET['code']; 
  52.  
  53.         $jsApi->setCode($code); 
  54.  
  55.         $openid = $jsApi->getOpenId(); 
  56.  
  57.       } 
  58.  
  59.          
  60.  
  61.       //=========步骤2:使用统一支付接口,获取prepay_id============ 
  62.  
  63.       //使用统一支付接口 
  64.  
  65.       $unifiedOrder = new /UnifiedOrder_pub(); 
  66.  
  67.          
  68.  
  69.       $unifiedOrder->setParameter("openid","$openid");//商品描述 
  70.  
  71.       $unifiedOrder->setParameter("body","在线预订");//商品描述 
  72.  
  73.       //自定义订单号,此处仅作举例 
  74.  
  75.       $timeStamp = time(); 
  76.  
  77. //       $out_trade_no = /WxPayConf_pub::APPID."$timeStamp"; 
  78.  
  79.       $out_trade_no = $info['ordersn']; 
  80.  
  81.       $unifiedOrder->setParameter("out_trade_no""$out_trade_no");//商户订单号 
  82.  
  83.       $unifiedOrder->setParameter("total_fee""$c");//总金额 
  84.  
  85.       $unifiedOrder->setParameter("notify_url",/WxPayConf_pub::NOTIFY_URL.'/order_id/'.$param['order_id']);//通知地址 
  86.  
  87.       $unifiedOrder->setParameter("trade_type","JSAPI");//交易类型 
  88.  
  89.          
  90.  
  91.       $prepay_id = $unifiedOrder->getPrepayId(); 
  92.  
  93.       //=========步骤3:使用jsapi调起支付============ 
  94.  
  95.       $jsApi->setPrepayId($prepay_id); 
  96.  
  97.       $jsApiParameters = $jsApi->getParameters(); 
  98.  
  99.       $this->assign('jsApiParameters'$jsApiParameters); 
  100.  
  101.   //Cuoxin.com 
  102.  
  103.       $this->display('pay'); 
  104.  
  105.     } 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选