tp5小程序支付 - 备忘

tp5小程序支付 - 备忘

  • 将官方的支付类放到vendor下面,vendor下面的weixinpay/lib/WxPay.Config.php 里面需要再配置下微信参数
  • 上代码,我这里的代码是放在 Order.php 类下面的
<?php
namespace app\index\controller;
use think\Db;
class Order extends Base
{
	protected $appId;
    protected $appSecret;
    protected $mchId;
    protected $apiKey;

    public function __construct()
    {
        $this->appId = '小程序的APPID';
        $this->appSecret = '小程序密钥';
        $this->mchId = '商户id';
        $this->apiKey = '商户KEY';
        $this->notifyUrl = 'https://'.$_SERVER['HTTP_HOST'].'/index/Order/notify';
    }
    
	// 微信支付处理  order_sn 订单号
    public function payWx($order_sn)
    {
        vendor('weixinpay.wxpay');	// 支付类库
        
        $order = Db::name('order')->where('order_sn', $order_sn)->find();
        $user  = Db::name('user')->where('id', $order['user_id'])->find();
        
        $openId = $user['openid'];	// openID存到user表中的
        $input = new \WxPayUnifiedOrder();
        $input->SetBody('购买课程');
        $input->SetOut_trade_no($order_sn);
        $input->SetTotal_fee(floatval($order['price']*100));	// 注意,微信的单位是分
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 3600));
        $input->SetNotify_url($this->notifyUrl);	// 回调地址,必须是绝对地址
        $input->SetTrade_type('JSAPI');
        $input->SetOpenid($openId);
        $order = \WxPayApi::unifiedOrder($input);
        $tools = new \JsApiPay();
        $jsApiParameters = $tools->GetJsApiParameters($order);
        $jsApiParameters = json_decode($jsApiParameters, true);
        $jsApiParameters['out_trade_no'] = $order_sn;
        return $jsApiParameters;
        /*
        jsApiParameters值为:
	        appId: "wx1234131314bb9e09"
			nonceStr: "n8ri8xet118skdcouieubpq8p2sm8j4j"
			out_trade_no: "NO2021012281119997"
			package: "prepay_id=wx221550266989162f3cd211a1199d590000"
			paySign: "78021D1FBBA92B6287BF86E8BE118A2C"
			signType: "MD5"
			timeStamp: "1611301826"
        */
    }
    
	// 微信支付回调处理,回调结束一定要 return 'SUCCESS', return 'SUCCESS', return 'SUCCESS',多return下不会有错,信我,信我,信我
    public  function notify()
    {
        header('content-type: '); 
        $data = file_get_contents('php://input');
 
        // 写入日志
        $dir = ROOT_PATH.'public'.DS.'log'.DS;
        if(!file_exists($dir)){
            mkdir($dir);
            chmod($dir, 0777);
        }
        $path = $dir;

        $file_name = $path.date('Ymd').'.txt';
        $myfile = fopen($file_name, "a") or die("Unable to open file!");
        fwrite($myfile, "支付的回调\n");
        fwrite($myfile, $data."\n");
        fclose($myfile);
        // 日志end

        $jsonxml = json_encode(simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA));   // 将xml转化为json格式
        $result = json_decode($jsonxml, true);   // 转成数组
        
        $order_sn = $result['out_trade_no'];
        $order = Db::name('order')->where('order_sn', $order_sn)->where('status', 1)->find();
        if (!$order) {
            return 'SUCCESS';
        }
        
        if ($result) {
            if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
                // 支付成功的处理
                return 'SUCCESS';
            }
        } else {
            // 支付失败的处理
            return 'SUCCESS';
        }
        return 'SUCCESS';
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值