<?php
namespace Api\Controller;
class TestPayController
{
/**
* 微信支付-发起订单.
*
* @param float $totalFee 收款总费用 单位元
* @param string $outTradeNo 唯一的订单号
* @param string $orderName 订单名称
*
* @return array
*/
public function weChatPay($totalFee, $outTradeNo, $orderName)
{
header('Content-type:text/html; Charset=utf-8');
//获取微信支付参数
$config = $this->weChatPayConfig();
$notifyUrl = '';//支付成功异步回调地址
//订单发起时间
$timestamp = time();
$unified = array(
'appid' => $config['appid'],
'attach' => 'pay', //商家数据包,原样返回,如果填写中文,请注意转换为utf-8
'body' => $orderName,
'mch_id' => $config['mch_id'],
'nonce_str' => $this->createNonceStr(),
'notify_url' => $notifyUrl,
'out_trade_no' => $outTradeNo,
'spbill_create_ip' => get_client_ip(),
'total_fee' => intval($totalFee * 100), //单位 转为分
'trade_type' => 'NATIVE',
);
$unified['sign'] = $this->getSign($unified, $config['key']);
$responseXml = $this->curlPost('https://api.mch.weixin.qq.com/pay/unifiedorder', $this->arrayToXml($unified));
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
$unifiedOrder = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
if (false === $unifiedOrder) {
die('parse xml error');
}
if ('SUCCESS' != $unifiedOrder->return_code) {
die($unifiedOrder->return_msg);
}
if ('SUCCESS' != $unifiedOrder->result_code) {
die($unifiedOrder->err_code);
}
$codeUrl = (array)($unifiedOrder->code_url);
if (!$codeUrl[0]) {
exit('get code_url error');
}
echo $this->wePayQRCode($codeUrl[0]);
}
public function weChatPayConfig()
{
//公众号id
$config['appid'] = '';
//商户号
$config['mch_id'] = '';
//key
$config['key'] = '';
return $config;
}
//获取随机字符串
public function createNonceStr($length = 16)
{
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$str = '';
for ($i = 0; $i < $length; ++$i) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
//获取当前服务器时间
public function get_client_ip()
{
if ($_SERVER['REMOTE_ADDR']) {
$cip = $_SERVER['REMOTE_ADDR'];
} elseif (getenv('REMOTE_ADDR')) {
$cip = getenv('REMOTE_ADDR');
} elseif (getenv('HTTP_CLIENT_IP')) {
$cip = getenv('HTTP_CLIENT_IP');
} else {
$cip = 'unknown';
}
return $cip;
}
//获取签名
public function getSign($params, $key)
{
ksort($params, SORT_STRING);
$unSignParaString = $this->formatQueryParaMap($params, false);
$signStr = strtoupper(md5($unSignParaString . '&key=' . $key));
return $signStr;
}
//生成签名
public function formatQueryParaMap($paraMap, $urlEncode = false)
{
$buff = '';
ksort($paraMap);
foreach ($paraMap as $k => $v) {
if (null != $v && 'null' != $v) {
if ($urlEncode) {
$v = urlencode($v);
}
$buff .= $k . '=' . $v . '&';
}
}
$reqPar = '';
if (strlen($buff) > 0) {
$reqPar = substr($buff, 0, strlen($buff) - 1);
}
return $reqPar;
}
//发送微信支付请求 get方式 一般用post方式 不用此方法
public function curlGet($url = '', $options = array())
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
if (!empty($options)) {
curl_setopt_array($ch, $options);
}
//https请求 不验证证书和host
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
//将数组转为xml-WeChatPay
public function arrayToXml($arr)
{
$xml = '<xml>';
foreach ($arr as $key => $val) {
if (is_numeric($val)) {
$xml .= '<' . $key . '>' . $val . '</' . $key . '>';
} else {
$xml .= '<' . $key . '><![CDATA[' . $val . ']]></' . $key . '>';
}
}
$xml .= '</xml>';
return $xml;
}
//生成微信支付二维码
public function wePayQRCode($url)
{
require_once VENDOR_PATH . 'WxpayAPI/example/phpqrcode/phpqrcode.php';
$value = $url; //二维码内容
$errorCorrectionLevel = 'L'; //容错级别
$matrixPointSize = 5; //生成图片大小
//生成二维码图片
$filename = 'qrcode/' . 'local' . time() . '.png';
\QRcode::png($value)
}
}