开发微信企业付款到银行卡步骤详解:https://blog.csdn.net/Zhihua_W/article/details/81261483
开发微信企业付款到零钱步骤详解:https://blog.csdn.net/Zhihua_W/article/details/81481181
微信APP支付申请配置过程详解:https://blog.csdn.net/Zhihua_W/article/details/80606089
微信支付开发出现redirect_uri参数错误的解决方法:https://blog.csdn.net/Zhihua_W/article/details/61199916
1、下载官方demo
下载路径:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=11_1
将此文件夹放入extensions目录下
2、在extensions目录下创建Wechatpay.php 文件 内容如下
<?php
/**
* 微信支付
*/
namespace app\extensions;
require_once dirname(__dir__) . DIRECTORY_SEPARATOR.'extensions/weixinPay/lib/WxPay.Api.php';
require_once dirname(__dir__) . DIRECTORY_SEPARATOR.'extensions/weixinPay/lib/WxPay.Data.php';
require_once dirname(__dir__) . DIRECTORY_SEPARATOR.'extensions/weixinPay/lib/WxPay.Notify.php';
require_once dirname(__dir__) . DIRECTORY_SEPARATOR.'extensions/weixinPay/lib/WxPay.Config.php';
require_once dirname(__dir__) . DIRECTORY_SEPARATOR.'extensions/weixinPay/example/WxPay.NativePay.php';
class Wechatpay
{
/**
* TODO: 修改这里配置为您自己申请的商户信息
* 微信公众号信息配置
*
* APPID:绑定支付的APPID(必须配置,开户邮件中可查看)
*
* MCHID:商户号(必须配置,开户邮件中可查看)
*
* KEY:商户支付密钥,参考开户邮件设置(必须配置,登录商户平台自行设置)
* 设置地址:https://pay.weixin.qq.com/index.php/account/api_cert
*
* APPSECRET:公众帐号secert(仅JSAPI支付的时候需要配置, 登录公众平台,进入开发者中心可设置),
* 获取地址:https://mp.weixin.qq.com/advanced/advanced?action=dev&t=advanced/dev&token=2005451881&lang=zh_CN
* @var string
*/
const APPID = 'wx426b3015555a46be';
const MCHID = '1900009851';
const KEY = '8934e7d15453e97507ef794cf7b0519d';
const APPSECRET = '7813490da6f1265e4901ffb80afaa36f';
const NOTIFY_URL = 'http://younijia.fenglianda.cn/site/wechatpay';
private $APPID;
private $MCHID;
private $KEY;
private $APPSECRET;
function __construct()
{
$this->APPID = Wechatpay::APPID;
$this->MCHID = Wechatpay::MCHID;
$this->KEY = Wechatpay::KEY;
$this->APPSECRET = Wechatpay::APPSECRET;
}
/*
* 微信支付方法
* @param $body 商品描述内容
* @param $attach 订单名称
* @param $out_trade_no 订单编号
* @param $total_fee 支付金额,
* @param $goods_tag 商品标记
* @param $product_id 商品id
*
* @return 跳转到微信二维码
*
*/
function native($out_trade_no,$total_fee,$goods_tag,$product_id,$body='',$attach='')
{
$notify = new \NativePay();
$input = new \WxPayUnifiedOrder();
$input->SetBody($body);
$input->SetAttach($attach);
//订单号
$input->SetOut_trade_no($out_trade_no); // 订单号
$input->SetTotal_fee($total_fee); //分为单位(订单金额)
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetGoods_tag($goods_tag);
// $input->SetNotify_url("http://www.nxq.com/test/weixinPay/example/notify_url.php");
$input->SetNotify_url(Wechatpay::NOTIFY_URL);
//原生扫码支付
$input->SetTrade_type("NATIVE");
//商品ID
$input->SetProduct_id($product_id);
// print_r($input);die;
//生成二维码的短链接地址
$result = $notify->GetPayUrl($input);
// print_r($result);die;
$url2 = $result["code_url"];
// print_r($url2);die;
$url = "http://127.0.0.1/XXX/basic/extensions/weixinPay/example/qrcode.php?data=".urldecode($url2);
return $url;
}
/**
* 微信退款
* @param $weixin_trade_no 微信交易号
* @param $total_fee 订单总金额
* @param $refund_fee 退款金额
*/
function refund($weixin_trade_no,$total_fee,$refund_fee)
{
if(isset($weixin_trade_no) && $weixin_trade_no != ""){
$transaction_id = $_REQUEST["transaction_id"];
$total_fee = $_REQUEST["total_fee"];
$refund_fee = $_REQUEST["refund_fee"];
$input = new \WxPayRefund();
$input->SetTransaction_id($transaction_id);
$input->SetTotal_fee($total_fee);
$input->SetRefund_fee($refund_fee);
$input->SetOut_refund_no(WxPayConfig::MCHID.date("YmdHis"));
$input->SetOp_user_id(WxPayConfig::MCHID);
printf_info(WxPayApi::refund($input));
exit();
}
}
}
?>
将demo中所有的引入文件格式改为如图所示
Demo中的文件不用添加命名空间,new \类名()时前面加\
前端用ajax请求支付时控制器调用extensions/Wechatpay类中native方法