微信小程序支付(2) tp5

1.将类放到tp5的extend文件夹下
2.在控制器里加以下代码

namespace app\api\controller;
use think\Controller;
use think\Session;
use think\Cache;
use think\Request;
use think\Db;

class Pay extends Controller
{
    public function pay_api()
    {
        import('wxpay', EXTEND_PATH);
        $PaynArr = new \WeixinPay('你的小程序appid','你的openid','商户号','商户平台操作Api密钥','下单时生成的订单号','支付标题(例如:拼团)',金额);
         $pay =  $PaynArr->pay();
          return json_encode($pay);
    }
}

支付回调方法
(注意,注意,注意)这个方法不用自己在小程序调用,支付成功后微信会自动异步去返回你这个接口参数,你只要在这个接口里写好接参和后续对数据库的操作就好

<?php
/**
 * 支付回调(注意回调信息是打印不出来的,只能写入文件来判断是否回调信息返回成功)就是微信小程序支付参数通知地址就写这个方法的地址
 */
namespace app\api\controller;
use think\Controller;
use think\Session;
use think\Cache;
use think\Request;
use think\Db;
class Notify extends Controller
{
     public function notify_api()
     {
        $postXml = file_get_contents("php://input"); //接收微信返回的xml数据
        $attr = xml_to_array($postXml);//获取到返回的信息(将微信返回的xml数据转成数组)
       foreach((array)$attr as $k=>$v){
        file_put_contents('minaNotify.txt',"$k:$v----",FILE_APPEND);
            }
       后续是操作数据库的动作
       操作数据库成功返回
       echo exit('<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>');
       防止微信一直回调这个方法

     }
}

写入common.php 文件的方法

/**
     * 将xml转为array
     *
     * return array
     */
    function xml_to_array($xml){
        //将XML转为array
        //禁止引用外部xml实体
       libxml_disable_entity_loader(true);
       $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);   
       $val = json_decode(json_encode($xmlstring), true);
       return $val;
    }

3.小程序代码

group:function()
  {
    wx.request({
      url: '后台地址',
      data: '',
      header: { 'content-type': 'application/json'},
      method: 'GET',
      dataType: 'json',

      success: function(res) {
        console.log(res.data);
        wx.requestPayment({
          timeStamp: res.data.timeStamp,
          nonceStr:res.data.nonceStr,
          package: res.data.package,
          signType: res.data.signType,
          paySign: res.data.paySign,
        })
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  }

后续自己处理。。。
虽然我的资源写的没有实际测试过,但请各位放心下载,我已经测试过了,CSDN资源不能删除所以改不了 请谅解。
支付类地址
完成!
支付宝支付和微信支付异步回调一直调用的解决方案

猜你喜欢

转载自blog.csdn.net/php12345679/article/details/81533499
今日推荐