搜索
系统检测到您的用户名不符合规范:

thinkphp3.2对接微信H5支付

浏览:4299 发布日期:2018年03月02日 分类:用法示例
在移动端使用微信支付,官方提供一个H5支付功能,下面是详细代码。



<?php
namespace Mobile\Controller;
use Think\Controller;

class WeixinpayIosController extends Controller {

public function pay(){

if(isset($_SESSION['orderinfo']) && !empty($_SESSION['orderinfo']['orderid']) && !empty($_SESSION['orderinfo']['payprice']) ) {

$orderid = $_SESSION['orderinfo']['orderid'];
$money = $_SESSION['orderinfo']['payprice'];
}

$appid = ""; // 微信给的
$mch_id = ""; // 微信官方的
$key = ""; // 自己设置的微信商家key

$rand = rand(00000,99999);
$out_trade_no = $orderid; // 平台内部订单号
$nonce_str = $this->get_sign(); // 随机字符串
$body = "充值"; // 内容
$total_fee = $money * 100; // 金额
$spbill_create_ip = $_SERVER["REMOTE_ADDR"]; // 获取用户IP
$notify_url = ""; // 回调地址
$trade_type = 'MWEB'; // 交易类型 具体看API 里面有详细介绍
$scene_info = '{"h5_info":{"type":"Wap","wap_url":"","wap_name":"支付"}}'; // 场景信息 必要参数
$signA = "appid=$appid&body=$body&mch_id=$mch_id&nonce_str=$nonce_str¬ify_url=$notify_url&out_trade_no=$out_trade_no&scene_info=$scene_info&spbill_create_ip=$spbill_create_ip&total_fee=$total_fee&trade_type=$trade_type";
$strSignTmp = $signA."&key=$key"; // 拼接字符串 注意顺序微信有个测试网址 顺序按照他的来 直接点下面的校正测试 包括下面xml 是否正确
$sign = strtoupper(MD5($strSignTmp)); // MD5 后转换成大写

$post_data = "<xml>
<appid>$appid</appid>
<body>$body</body>
<mch_id>$mch_id</mch_id>
<nonce_str>$nonce_str</nonce_str>
<notify_url>$notify_url</notify_url>
<out_trade_no>$out_trade_no</out_trade_no>
<spbill_create_ip>$spbill_create_ip</spbill_create_ip>
<total_fee>$total_fee</total_fee>
<trade_type>$trade_type</trade_type>
<scene_info>$scene_info</scene_info>
<sign>$sign</sign>
</xml>";//拼接成xml 格式
$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";//微信传参地址
$dataxml = $this->http_post($url,$post_data); //后台POST微信传参地址 同时取得微信返回的参数 POST 方法我写下面了
$objectxml = (array)simplexml_load_string($dataxml, 'SimplexmlElement', LIBxml_NOCDATA); //将微信返回的xml 转换成数组

// 判断调用成功跳转中介页面
if( $objectxml['return_code'] == 'SUCCESS' && $objectxml['result_code'] == 'SUCCESS' ){

redirect($objectxml['mweb_url']);
}
}

public function http_post($url, $data) {

$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
$res = curl_exec($curl);
curl_close($curl);

return $res;
}

/**
* 随机生成32位签名
* @return string
*/
function get_sign(){

$num = rand(10,20);
$times = rand(1,9);
$str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for ($i=0; $i < $times ; $i++) {
for ($j=0; $j < $num; $j++) {
$b = rand(0,61);
$code .= $str[$b];
}
}
return md5($code);
}

}
收藏
Smile_菜菜
积分:856 等级:LV2
热点推荐
(追記) (追記ここまで)
最新更新

我们

合作

网站

信息

ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。

AltStyle によって変換されたページ (->オリジナル) /