一、微信公众号号后台支付配置

附微信支付参考文档:https://pay.weixin.qq.com/wiki/doc/api/index.html

二、微信支付类封装

该类可以实现付款码支付、JSAPI支付、Native支付

<?php

/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/2/16 0016
* Time: 下午 4:04
*/
class Wxpay
{
const TRADETYPE_JSAPI = 'JSAPI', TRADETYPE_NATIVE = 'NATIVE', TRADETYPE_APP = 'APP';
const URL_UNIFIEDORDER = "https://api.mch.weixin.qq.com/pay/unifiedorder";
const URL_ORDERQUERY = "https://api.mch.weixin.qq.com/pay/orderquery";
const URL_CLOSEORDER = 'https://api.mch.weixin.qq.com/pay/closeorder';
const URL_REFUND = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
const URL_REFUNDQUERY = 'https://api.mch.weixin.qq.com/pay/refundquery';
const URL_DOWNLOADBILL = 'https://api.mch.weixin.qq.com/pay/downloadbill';
const URL_REPORT = 'https://api.mch.weixin.qq.com/payitil/report';
const URL_SHORTURL = 'https://api.mch.weixin.qq.com/tools/shorturl';
const URL_MICROPAY = 'https://api.mch.weixin.qq.com/pay/micropay';
/**
* 错误信息
*/
public $error = null;
/**
* 错误信息XML
*/
public $errorXML = null;
/**
* 微信支付配置数组
* appid 公众账号appid
* mch_id 商户号
* apikey 加密key
* appsecret 公众号appsecret
* sslcertPath 证书路径(apiclient_cert.pem)
* sslkeyPath 密钥路径(apiclient_key.pem)
*/
private $_config; /**
* @param $config 微信支付配置数组
*/
public function __construct($config)
{
$this->_config = $config;
} /**
* JSAPI获取prepay_id
* @param $body
* @param $out_trade_no
* @param $total_fee
* @param $notify_url
* @param $openid
* @return null
*/
public function getPrepayId($body, $out_trade_no, $total_fee, $notify_url, $openid)
{
$data = array();
$data["nonce_str"] = $this->get_nonce_string();
$data["body"] = $body;
$data["out_trade_no"] = $out_trade_no;
$data["total_fee"] = $total_fee;
$data["spbill_create_ip"] = $_SERVER["REMOTE_ADDR"];
$data["notify_url"] = $notify_url;
$data["trade_type"] = self::TRADETYPE_JSAPI;
$data["openid"] = $openid;
$result = $this->unifiedOrder($data);
if ($result["return_code"] == "SUCCESS" && $result["result_code"] == "SUCCESS") {
return $result["prepay_id"];
} else {
$this->error = $result["return_code"] == "SUCCESS" ? $result["err_code_des"] : $result["return_msg"];
$this->errorXML = $this->array2xml($result);
return null;
}
} private function get_nonce_string()
{
return substr(str_shuffle("abcdefghijklmnopqrstuvwxyz0123456789"), 0, 32);
} /**
* 统一下单接口
*/
public function unifiedOrder($params)
{
$data = array();
$data["appid"] = $this->_config["appid"];
$data["mch_id"] = $this->_config["mch_id"];
$data["device_info"] = (isset($params['device_info']) && trim($params['device_info']) != '') ? $params['device_info'] : null;
$data["nonce_str"] = $this->get_nonce_string();
$data["body"] = $params['body'];
$data["detail"] = isset($params['detail']) ? $params['detail'] : null;//optional
$data["attach"] = isset($params['attach']) ? $params['attach'] : null;//optional
$data["out_trade_no"] = isset($params['out_trade_no']) ? $params['out_trade_no'] : null;
$data["fee_type"] = isset($params['fee_type']) ? $params['fee_type'] : 'CNY';
$data["total_fee"] = $params['total_fee'];
$data["spbill_create_ip"] = $params['spbill_create_ip'];
$data["time_start"] = isset($params['time_start']) ? $params['time_start'] : null;//optional
$data["time_expire"] = isset($params['time_expire']) ? $params['time_expire'] : null;//optional
$data["goods_tag"] = isset($params['goods_tag']) ? $params['goods_tag'] : null;
$data["notify_url"] = $params['notify_url'];
$data["trade_type"] = $params['trade_type'];
$data["product_id"] = isset($params['product_id']) ? $params['product_id'] : null;//required when trade_type = NATIVE
$data["openid"] = isset($params['openid']) ? $params['openid'] : null;//required when trade_type = JSAPI
$result = $this->post(self::URL_UNIFIEDORDER, $data);
return $result;
} private function post($url, $data, $cert = false)
{
$data["sign"] = $this->sign($data);
$xml = $this->array2xml($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
if ($cert == true) {
//使用证书:cert 与 key 分别属于两个.pem文件
curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLCERT, $this->_config['sslcertPath']);
curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLKEY, $this->_config['sslkeyPath']);
}
$content = curl_exec($ch);
$array = $this->xml2array($content);
return $array;
} /**
* 数据签名
* @param $data
* @return string
*/
private function sign($data)
{
ksort($data);
$string1 = "";
foreach ($data as $k => $v) {
if ($v && trim($v) != '') {
$string1 .= "$k=$v&";
}
}
$stringSignTemp = $string1 . "key=" . $this->_config["apikey"];
$sign = strtoupper(md5($stringSignTemp));
return $sign;
} private function array2xml($array)
{
$xml = "<xml>" . PHP_EOL;
foreach ($array as $k => $v) {
if ($v && trim($v) != '')
$xml .= "<$k><![CDATA[$v]]></$k>" . PHP_EOL;
}
$xml .= "</xml>";
return $xml;
} private function xml2array($xml)
{
$array = array();
$tmp = null;
try {
$tmp = (array)simplexml_load_string($xml);
} catch (Exception $e) {
}
if ($tmp && is_array($tmp)) {
foreach ($tmp as $k => $v) {
$array[$k] = (string)$v;
}
}
return $array;
} /**
* 扫码支付(模式二)获取支付二维码
* @param $body
* @param $out_trade_no
* @param $total_fee
* @param $notify_url
* @param $product_id
* @return null
*/
public function getCodeUrl($body, $out_trade_no, $total_fee, $notify_url, $product_id)
{
$data = array();
$data["nonce_str"] = $this->get_nonce_string();
$data["body"] = $body;
$data["out_trade_no"] = $out_trade_no;
$data["total_fee"] = $total_fee;
$data["spbill_create_ip"] = $_SERVER["SERVER_ADDR"];
$data["notify_url"] = $notify_url;
$data["trade_type"] = self::TRADETYPE_NATIVE;
$data["product_id"] = $product_id;
$result = $this->unifiedOrder($data);
if ($result["return_code"] == "SUCCESS" && $result["result_code"] == "SUCCESS") {
return $result["code_url"];
} else {
$this->error = $result["return_code"] == "SUCCESS" ? $result["err_code_des"] : $result["return_msg"];
return null;
}
} /**
* 查询订单
* @param $transaction_id
* @param $out_trade_no
* @return array
*/
public function orderQuery($transaction_id, $out_trade_no)
{
$data = array();
$data["appid"] = $this->_config["appid"];
$data["mch_id"] = $this->_config["mch_id"];
$data["transaction_id"] = $transaction_id;
$data["out_trade_no"] = $out_trade_no;
$data["nonce_str"] = $this->get_nonce_string();
$result = $this->post(self::URL_ORDERQUERY, $data);
return $result;
} /**
* 关闭订单
* @param $out_trade_no
* @return array
*/
public function closeOrder($out_trade_no)
{
$data = array();
$data["appid"] = $this->_config["appid"];
$data["mch_id"] = $this->_config["mch_id"];
$data["out_trade_no"] = $out_trade_no;
$data["nonce_str"] = $this->get_nonce_string();
$result = $this->post(self::URL_CLOSEORDER, $data);
return $result;
} /**
* 申请退款 - 使用商户订单号
* @param $out_trade_no 商户订单号
* @param $out_refund_no 退款单号
* @param $total_fee 总金额(单位:分)
* @param $refund_fee 退款金额(单位:分)
* @param $op_user_id 操作员账号
* @return array
*/
public function refund($out_trade_no, $out_refund_no, $total_fee, $refund_fee, $op_user_id)
{
$data = array();
$data["appid"] = $this->_config["appid"];
$data["mch_id"] = $this->_config["mch_id"];
$data["nonce_str"] = $this->get_nonce_string();
$data["out_trade_no"] = $out_trade_no;
$data["out_refund_no"] = $out_refund_no;
$data["total_fee"] = $total_fee;
$data["refund_fee"] = $refund_fee;
$data["op_user_id"] = $op_user_id;
$result = $this->post(self::URL_REFUND, $data, true);
return $result;
} /**
* 申请退款 - 使用微信订单号
* @param $out_trade_no 商户订单号
* @param $out_refund_no 退款单号
* @param $total_fee 总金额(单位:分)
* @param $refund_fee 退款金额(单位:分)
* @param $op_user_id 操作员账号
* @return array
*/
public function refundByTransId($transaction_id, $out_refund_no, $total_fee, $refund_fee, $op_user_id)
{
$data = array();
$data["appid"] = $this->_config["appid"];
$data["mch_id"] = $this->_config["mch_id"];
$data["nonce_str"] = $this->get_nonce_string();
$data["transaction_id"] = $transaction_id;
$data["out_refund_no"] = $out_refund_no;
$data["total_fee"] = $total_fee;
$data["refund_fee"] = $refund_fee;
$data["op_user_id"] = $op_user_id;
$result = $this->post(self::URL_REFUND, $data, true);
return $result;
} /**
* 下载对账单
* @param $bill_date 下载对账单的日期,格式:20140603
* @param $bill_type 类型
* @return array
*/
public function downloadBill($bill_date, $bill_type = 'ALL')
{
$data = array();
$data["appid"] = $this->_config["appid"];
$data["mch_id"] = $this->_config["mch_id"];
$data["bill_date"] = $bill_date;
$data["bill_type"] = $bill_type;
$data["nonce_str"] = $this->get_nonce_string();
$result = $this->post(self::URL_DOWNLOADBILL, $data);
return $result;
} /**
* 获取js支付使用的第二个参数
*/
public function get_package($prepay_id)
{
$data = array();
$data["appId"] = $this->_config["appid"];
$data["timeStamp"] = time();
$data["nonceStr"] = $this->get_nonce_string();
$data["package"] = "prepay_id=$prepay_id";
$data["signType"] = "MD5";
$data["paySign"] = $this->sign($data);
return $data;
} /**
* 获取发送到通知地址的数据(在通知地址内使用)
* @return 结果数组,如果不是微信服务器发送的数据返回null
* appid
* bank_type
* cash_fee
* fee_type
* is_subscribe
* mch_id
* nonce_str
* openid
* out_trade_no 商户订单号
* result_code
* return_code
* sign
* time_end
* total_fee 总金额
* trade_type
* transaction_id 微信支付订单号
*/
public function get_back_data()
{
$xml = file_get_contents("php://input");
$data = $this->xml2array($xml);
if ($this->validate($data)) {
return $data;
} else {
return null;
}
} /**
* 验证数据签名
* @param $data 数据数组
* @return 数据校验结果
*/
public function validate($data)
{
if (!isset($data["sign"])) {
return false;
}
$sign = $data["sign"];
unset($data["sign"]);
return $this->sign($data) == $sign;
} /**
* 响应微信支付后台通知
* @param $return_code 返回状态码 SUCCESS/FAIL
* @param $return_msg 返回信息
*/
public function response_back($return_code = "SUCCESS", $return_msg = null)
{
$data = array();
$data["return_code"] = $return_code;
if ($return_msg) {
$data["return_msg"] = $return_msg;
}
$xml = $this->array2xml($data);
print $xml;
}
}

先调用getPrepayId方法,后再调用get_package方法,返回的参数传给前台。

三、前台js代码

function onBridgeReady(){
WeixinJSBridge.invoke(
'getBrandWCPayRequest', {
"appId":"wx2421b1c4370ec43b", //公众号名称,由商户传入
"timeStamp":"1395712654", //时间戳,自1970年以来的秒数
"nonceStr":"e61463f8efa94090b1f366cccfbbb444", //随机串
"package":"prepay_id=u802345jgfjsdfgsdg888",
"signType":"MD5", //微信签名方式:
"paySign":"70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名
},
function(res){
if(res.err_msg == "get_brand_wcpay_request:ok" ){
// 使用以上方式判断前端返回,微信团队郑重提示:
//res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
}
});
}
if (typeof WeixinJSBridge == "undefined"){
if( document.addEventListener ){
document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
}else if (document.attachEvent){
document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
}
}else{
onBridgeReady();
}

微信 之jsapi实现支付的更多相关文章

  1. PHP微信公众号JSAPI网页支付(下)

    上一篇PHP微信公众号JSAPI网页支付(上)中讲到了公众号平台的相关设置以及支付的大致流程. 这一篇重点讲支付后,异步接受回调通知,以及处理后同步通知微信服务器. 首先梳理下整个jsapi支付的流程 ...

  2. PHP微信公众号JSAPI网页支付(上)

    一.使用场景以及说明 使用场景:商户已有H5商城网站,用户通过消息或扫描二维码在微信内打开网页时,可以调用微信支付完成下单购买的流程. 说明:1.用户打开图文消息或者扫描二维码,在微信内置浏览器打开网 ...

  3. 微信接口出现“调用支付jsapi缺少参数appid”

    微信接口出现“调用支付jsapi缺少参数appid” 注意:@Html.Raw(ViewBag.wxJsApiParam),//json串ViewBag.wxJsApiParam是一个在后台拼的一个j ...

  4. asp.net mvc下实现微信公众号(JsApi)支付介绍

    本文主要讲解asp.net mvc框架下公众号支付如何实现,公众号支付主要包括三个核心代码,前台调起支付js代码.对应js调用参数参数生成代码.支付成功处理代码. 一.微信支付方式介绍 微信提供了各种 ...

  5. Core3.1 微信v3 JSAPI支付 退款

    1.前言 上一篇写了<Core3.1 微信v3 JSAPI支付>,这个属于v3的接口规则,现在研究了下退款的接口我写的时候它属于v2接口规则文档.但凡微信支付文档里面写清楚点我也不会在这里 ...

  6. 微信扫码i支付~官方DEMO的坑

    返回目录 之前写过关于微信支付那些坑的文章(<微信JSApi支付~集成到MVC环境后的最后一个坑(网上没有这种解决方案)>,<微信JSApi支付~坑和如何填坑>),下面说一下微 ...

  7. 【第二十篇】C#微信H5支付 非微信内浏览器H5支付 浏览器微信支付

    微信开发者文档 微信H5支付官方文档   请阅读清楚  最起码把所有参数看一遍 这个地方也可以看看 微信案例 http://wxpay.wxutil.com/mch/pay/h5.v2.php,请在微 ...

  8. 微信小程序实现支付功能

    小程序支付,没有封装支付代码:直接上一段可用的流程代码吧:微信小程序支付官网文档有详细的说明,这里我就不再赘述啦:客户端js: wx.request({ url:'https://www.xxxx.c ...

  9. 微信小程序篇(微信小程序的支付)

    微信小程序的支付和微信公众号的支付是类似的,对比起来还比公众号支付简单了一些,我们只需要调用微信的统一下单接口获取prepay_id之后我们在调用微信的支付即可. 今天我们来封装一般node的支付接口 ...

随机推荐

  1. memory management in oracle 11G R2

    When we talking about memory management in Oracle, we are refering to SGA and PGA. The management me ...

  2. CentOS6.5 64位站点压力測试工具webbench

    在Apache中有自带的ab命令能够測试服务的压力,而nginx没有自带的命令,必需要採用第三方软件来測试.今天就简介一下webbench对nginx的压力測试,压力測试是对系统管理员和运维人员必须的 ...

  3. 【翻译自mos文章】OGG replicat 进程使用的 TCP port

    OGG replicat 进程使用的 TCP port 来源于: TCP PORT USED BY REPLICAT PROCESSES (文档 ID 1060954.1) 适用于: Oracle G ...

  4. Cocos2d-x可以实现的效果总结

    Cocos2d-x可以实现的效果总结  动作(Actions) move移动:moveto/moveby 从一个位置移动到另外一个位置 从一个位置移动多少数量级 rotate旋转:rotateto/r ...

  5. oc66--代理模式应用2

    // // BabyProtocol.h #import <Foundation/Foundation.h> @class Baby; @protocol BabyProtocol < ...

  6. ROS人脸检测 使用webcam实现

    github地址https://github.com/ngunauj/facedetection 熟悉ros环境.ubuntu16.04 + ros kinetic版本.使用笔记本自带摄像头,完成人脸 ...

  7. HDU 1299Diophantus of Alexandria

    Diophantus of Alexandria Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  8. XHR ajax

    谷歌搜索xhr site:cnblogs.com https://www.cnblogs.com/xiaohuochai/p/6036475.html 在Chrome的network监视中,类型为XH ...

  9. bzoj 3721 Final Bazarek

    题目大意: n个数 选k个使和为奇数且最大 思路: 可以先将这n个数排序 然后先去最大的k个数 若和为奇数则直接输出 为偶数可以用没选的最大的奇数替换选了的最小的偶数或用没选的最大的偶数替换选了的最小 ...

  10. 【POJ 1958】 Strange Towers of Hanoi

    [题目链接] http://poj.org/problem?id=1958 [算法] 先考虑三个塔的情况,g[i]表示在三塔情况下的移动步数,则g[i] = g[i-1] * 2 + 1 再考虑四个塔 ...