目录
- 付款码支付
-
- post请求
- 随机数
- 商户号
- XML数据转换诚数组
- 数组转换为XML格式
- 生成签名
- 查询订单支付状态
付款码支付
public function getScanCodePayment($nonce_str,$out_trade_no,$spbill_create_ip,$auth_code,$money)
{
$baseConfigModel = new \app\admin\model\Baseconfig();
$app_id = $baseConfigModel->where(['name' => 'app_id'])->value('data');
$mch_id = $baseConfigModel->where(['name' => 'mch_id'])->value('data');
$payment_key = $baseConfigModel->where(['name' => 'payment_key'])->value('data');
$data = [
'appid' => $app_id,
'mch_id' => $mch_id,
'nonce_str' => $nonce_str,
'body' => '云舍-按摩',
'out_trade_no' => $out_trade_no,
'total_fee' => 1,
'spbill_create_ip' => $spbill_create_ip,
'auth_code' => $auth_code,
'sign_type' => 'MD5',
];
$data['sign'] = $this->getSign($data, $payment_key);
$xml = $this->ToXml($data);
$url = "https://api.mch.weixin.qq.com/pay/micropay";
return $this->post_url($url,$xml);
}
post请求
public function post_url($url,$xml){
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$dataxml = curl_exec($ch);
if (!$dataxml) {
$error = curl_errno($ch);
throw new ValidateException('付款失败,错误码:'.$error);
}
curl_close($ch);
return $this->FromXml($dataxml);
}
随机数
function generate_password($length = 32)
{
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$password = "";
for ($i = 0; $i < $length; $i++) {
$password .= $chars[mt_rand(0, strlen($chars) - 1)];
}
return $password;
}
商户号
function order_number($order_no)
{
return $order_no . date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 14);
}
XML数据转换诚数组
public function FromXml($xml)
{
if (!$xml) {
echo "xml数据异常!";
}
libxml_disable_entity_loader(true);
$data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $data;
}
数组转换为XML格式
public function ToXml($data = array())
{
if (!is_array($data) || count($data) <= 0) {
return '数组异常';
}
$xml = "";
foreach ($data as $key => $val) {
if (is_numeric($val)) {
$xml .= "<" . $key . ">" . $val . "" . $key . ">";
} else {
$xml .= "<" . $key . "> . $val . "]]>" . $key . ">";
}
}
$xml .= "";
return $xml;
}
生成签名
public function getSign($params, $key1)
{
ksort($params);
foreach ($params as $key => $item) {
if (!empty($item)) {
$newArr[] = $key . '=' . $item;
}
}
$stringA = implode("&", $newArr);
$stringSignTemp = $stringA . "&key=" . $key1;
$stringSignTemp = MD5($stringSignTemp);
$sign = strtoupper($stringSignTemp);
return $sign;
}
查询订单支付状态
public function getViewOrder($out_trade_no){
$baseConfigModel = new \app\admin\model\Baseconfig();
$app_id = $baseConfigModel->where(['name' => 'app_id'])->value('data');
$mch_id = $baseConfigModel->where(['name' => 'mch_id'])->value('data');
$payment_key = $baseConfigModel->where(['name' => 'payment_key'])->value('data');
$data = [
'appid' => $app_id,
'mch_id' => $mch_id,
'out_trade_no' => $out_trade_no,
'nonce_str' => $this->generate_password(),
'sign_type' => 'MD5',
];
$data['sign'] = $this->getSign($data, $payment_key);
$xml = $this->ToXml($data);
$url = "https://api.mch.weixin.qq.com/pay/orderquery";
return $this->post_url($url,$xml);
}