tp5.1 微信支付

参考:

http://www.thinkphp.cn/code/3593.html

Github查看最新代码https://github.com/dream2023/ThinkPHP5-wxpay

把上面的代码整合到TP5.1中

重点整理扫码支付,不修改是生成不了二维码的

1、修改\extend\wxpay\NativePay.php文件,修正后的代码

<?php

namespace wxpay;

//use think\Loader;
//
//Loader::import('wxpay.lib.WxPayNativePay');
require '../extend/wxpay/lib/WxPayNativePay.php';
//require '../extend/wxpay/lib/Phpqrcode.php';//二维码

/**
* 扫码支付
*
* 用法:
* 调用 \wxpay\NativePay::getPayImage($params) 即可
*
*/
class NativePay extends WxPayBase
{
    /**
     * 获取扫码支付的二维码图片
     *
     * @param array  $params 订单信息
     * @param string $params['body'] 商品简单描述
     * @param string $params['out_trade_no'] 商户订单号, 要保证唯一性
     * @param string $params['total_fee'] 标价金额, 请注意, 单位为分!!!!!
     * @param string $params['product_id'] 商品ID
     *
     * @param string $width 二维码宽
     * @param string $height 二维码长
     *
     * @return string img标签
     */
    public static function getPayImage($params, $width=150, $height=150)
    {
        // 1.校检参数
        $that = new self();
        $that->checkParams($params);
        if(empty($params['product_id'])) {
            throw new \WxPayException('商品ID(product_id)商品ID必须');
        }

        // 2.组装参数
        $input = $that->getPostData($params);

        // 3.进行请求
        $tools = new \WxPayNativePay();
        $result = $tools->GetPayUrl($input);

        // 4.进行结果检验
        $that->checkResult($result);

        // 5.返回支付二维码图片
        $url = urlencode($result["code_url"]);
		
		//$payImage = $that->qrCode($url);
        $payImage = "<img alt='扫码支付' src='/pay/wx/qrcodeUrl?data={$url}' style='width:{$width}px;height:{$height}px;'/>";
        return $payImage;
    }

    // 组装请求参数
    private function getPostData($params)
    {
        $input  = new \WxPayUnifiedOrder();
        $input->SetBody($params['body']);
        $input->SetOut_trade_no($params['out_trade_no']);
        $input->SetTotal_fee($params['total_fee']);
        // $input->SetGoods_tag("test");
        $input->SetNotify_url(\WxPayConfig::NOTIFY_URL);
        $input->SetTrade_type("NATIVE");
        $input->SetProduct_id($params['product_id']);
        return $input;
    }
}

重点:TP5.1不支持

use think\Loader;

Loader::import('wxpay.lib.WxPayNativePay');

引用文件,需要修改成

2、在\application\index\controller\Example.php文件中增加生成二维码函数

    // 扫码支付 生成二维码后转换成图片二维码
    public function qrcodeUrl()
    {
    	require '../extend/wxpay/lib/Phpqrcode.php';//二维码
			$url = urldecode(input('data'));
			if(substr($url, 0, 6) == "weixin"){
		         $qrcode_tools = new \QRcode();
		         echo $qrcode_tools->png($url);
				//QRcode::png($url);
			}else{
				 echo header('HTTP/1.1 404 Not Found');
			}
    }

Example.php代码:

<?php
namespace app\pay\controller;
use think\Db;
use think\Validate;

class Wx extends Common{
    // 扫码支付
    public function index()
    {
        $params = [
            'body' => '支付测试',
            'out_trade_no' => mt_rand().time(),
            'total_fee' => 1,
            'product_id' => time(),
        ];
        //db('order')->insert($params);
        $result = \wxpay\NativePay::getPayImage($params);
        echo $result;
    }

    // 公众号支付
    public function jspay()
    {
        $params = [
            'body' => '支付测试',
            'out_trade_no' => mt_rand().time(),
            'total_fee' => 1,
        ];
        $result = \wxpay\JsapiPay::getPayParams($params);
        halt($result);
    }

    // 小程序支付
    public function smallapp()
    {
        $params = [
            'body'         => '支付测试',
            'out_trade_no' => mt_rand().time(),
            'total_fee'    => 1,
        ];
        $code = '08123gA41K4EQO1RH1B41uP2A4123gAW';
        $result = \wxpay\JsapiPay::getPayParams($params, $code);

        $openId = 'oCtoK0SjxW-N5qjEDgaMyummJyig';
        $result = \wxpay\JsapiPay::getParams($params, $openId);
    }

    // 刷卡支付
    public function micropay()
    {
        $params = [
            'body' => '支付测试',
            'out_trade_no' => mt_rand().time(),
            'total_fee' => 1,
        ];

        $auth_code = '134628839776154108';
        $result = \wxpay\MicroPay::pay($params, $auth_code);
        halt($result);
    }

    // H5支付
    public function wappay()
    {
        $params = [
            'body' => '支付测试',
            'out_trade_no' => mt_rand().time(),
            'total_fee' => 1,
        ];

        $result = \wxpay\WapPay::getPayUrl($params);
        halt($result);
    }

    // 订单查询
    public function query()
    {
        $out_trade_no = '290000985120170917160005';
        $result = \wxpay\Query::exec($out_trade_no);
        halt($result);
    }

    // 退款
    public function refund()
    {
        $params = [
            'out_trade_no' => '290000985120170917160005',
            'total_fee' => 1,
            'refund_fee' => 1,
            'out_refund_no' => time()
        ];
        $result = \wxpay\Refund::exec($params);
        halt($result);
    }

    // 退款查询
    public function refundquery()
    {
        $order_no = '290000985120170917160005';
        $result = \wxpay\RefundQuery::exec($order_no);
        halt($result);
    }

    // 下载对账单
    public function download()
    {
        $result = \wxpay\DownloadBill::exec('20170923');
        echo($result);
    }

    // 通知测试
    public function notify()
    {
        $notify = new \wxpay\Notify();
        $notify->Handle();
    }
	
    // 扫码支付 生成二维码后转换成图片二维码
    public function qrcodeUrl()
    {
    	require '../extend/wxpay/lib/Phpqrcode.php';//二维码
			$url = urldecode(input('data'));
			if(substr($url, 0, 6) == "weixin"){
		         $qrcode_tools = new \QRcode();
		         echo $qrcode_tools->png($url);
				//QRcode::png($url);
			}else{
				 echo header('HTTP/1.1 404 Not Found');
			}
    }
	
	
}

猜你喜欢

转载自blog.csdn.net/haibo0668/article/details/81203930