首页 > 分享 > 使用php的ci框架,实现微信支付(jsapi方式+完整代码)

使用php的ci框架,实现微信支付(jsapi方式+完整代码)

看了一天的微信文档,花了一天的时候调试问题,一共花了差不多2天时间,可算是把微信支付做出来了,在这里把微信支付的开发过程记录一下。

微信后台配置

要注意的是支付授权目录,应该填写你实际支付页面的上一级!

比如:用于支付的页面是www.baidu.com/abc/order/pay

那么你的授权目录应该是:www.baidu.com/abc/order/

注意要带上‘/’

1.  文件准备

 由于我用的ci框架,在微信支付官方文档下载支付的demo,不是php的CI框架,所以并不适用。随后在网上找到了一位热心网友大神的代码,将官网的demo整合到了CI框架里,链接http://www.cnblogs.com/24la/p/wxpay-jsapi-refund.html。感谢大神!大家可以看看链接中的说明,写的很清楚。

这里贴一下大佬封装的api类,很好用!

class WechatPay {

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;

public $errorXML = null;

private $_config;

public function __construct($config) {

$this->_config = $config;

}

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;

$data["attach"] = isset($params['attach'])?$params['attach']:null;

$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;

$data["time_expire"] = isset($params['time_expire'])?$params['time_expire']:null;

$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;

$data["openid"] = isset($params['openid'])?$params['openid']:null;

$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){

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;

}

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;

}

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;

}

}

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;

}

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;

}

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;

}

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;

}

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;

}

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;

}

public function get_back_data() {

$xml = file_get_contents("php://input");

$data = $this->xml2array($xml);

if ($this->validate($data)) {

return $data;

} else {

return null;

}

}

public function validate($data) {

if (!isset($data["sign"])) {

return false;

}

$sign = $data["sign"];

unset($data["sign"]);

return $this->sign($data) == $sign;

}

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;

}

}

根据文章,需要对文件做一下改动:

1.      新建一个类文件Wechatpay.php 放到applicationlibraries里

2.      将证书,日志之类的文件放到wechatpay.php 统计目录下

3.      新建配置文件wxpay_config.php 保存商户号、appid、AppSecret、API key。将该文件放到applicationconfig目录下

2.  控制器里调用

链接里有讲的十分详细该怎么写。

其中有关于openid,因为我之前有获取过openid并把它放到session,所以在调用的时候直接从session获取就行了,十分方便。大家也可是尝试这种方法,

tips:在开发中遇到了一个问题,做到统一下单的时候,微信返回的报错信息是权限不足,请到商户后台配置。网上搜了下,很多人都遇到过这种问题,百度了一圈也没有实际解决方案。

最后,要吐槽一下腾讯!花了半天时间确认商户后台配置没问题,各个参数都没有问题,结果还是失败返回权限不足。结果联系腾讯,给了各个数据对方说稍等,半个小时后后就没问题了!

相关知识

卷积神经网络实现鸢尾花数据分类python代码实现
PHP完整的订单系统订单系统
web支付基础教程
PHP开发商城中的订单管理功能实现步骤
PHP编程实现高效在线订花系统:从需求分析到代码实现详解
基于微信小程序的鲜花预定系统的设计与实现
php毕业设计基于PHP实现的鲜花批发销售网站[包运行成功+文档]
花瓣支付可以扫微信吗
鲜花微信小程序系统详解与核心代码展示
使用PHP构建动态网站的技术指南

网址: 使用php的ci框架,实现微信支付(jsapi方式+完整代码) https://m.huajiangbk.com/newsview948922.html

所属分类:花卉
上一篇: K8s 简单集成 SkyWalk
下一篇: 支付宝合花付是什么?如何使用?