<?php
/**
* 微信支付接口升级方法
* 官方文档中提到所有接口升级方法
* 文档地址:
* https://pay.weixin.qq.com/wiki/doc/api/micropay_sl.php?chapter=23_15
* https://pay.weixin.qq.com/wiki/doc/api/download/fws_mczjksj.pdf
* https://pay.weixin.qq.com/wiki/doc/api/download/mczyscsyl.pdf
* 接口升级状态查看:
* https://pay.weixin.qq.com/index.php/extend/open_norecharge/showsandbox?pid=48
* 注意:
* 查看接口升级状态次数过多可能会导致问题
* 商户信息必须为真实商户号信息
* APPID可以是公众号、小程序,示例中使用的是真实存在的APPID
*/
class lvupv3
{
// 商户号
private $mch_id = '**********';
// 商户密钥
private $mch_key = '**********';
// 公众号、小程序APPID
private $appid = '**********';
public function getsignkey()
{
$url = 'https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey';
$data['mch_id'] = $this->mch_id;
$data['nonce_str'] = $this->randomString(32);
$data['sign'] = $this->sign($data, true);
$xmlData = $this->toXml($data);
$doing = $this->requestXml($url, $xmlData);
// var_dump($doing);
$xmlData = $this->fromXml($doing['info']);
// var_dump($xmlData);
return $xmlData['sandbox_signkey'];
}
public function sandbox_test_1001()
{
$url = 'https://api.mch.weixin.qq.com/sandboxnew/pay/micropay';
$pay_config['mch_id'] = $this->mch_id;
$pay_config['appid'] = $this->appid;
$pay_config['total_fee'] = '501';
$pay_config['settlement_total_fee'] = '500';
$pay_config['coupon_fee'] = '1';
$pay_config['out_trade_no'] = 'sandbox_test_501';
$pay_config['sign'] = $this->sign($pay_config);
$xmlData = $this->toXml($pay_config);
$doing = $this->requestXml($url, $xmlData);
var_dump($doing);
$this->orderquery($pay_config['out_trade_no']);
}
public function sandbox_test_1002()
{
$url = 'https://api.mch.weixin.qq.com/sandboxnew/pay/micropay';
$pay_config['mch_id'] = $this->mch_id;
$pay_config['appid'] = $this->appid;
$pay_config['total_fee'] = '502';
$pay_config['settlement_total_fee'] = '501';
$pay_config['coupon_fee'] = '1';
$pay_config['out_trade_no'] = 'sandbox_test_502';
$pay_config['sign'] = $this->sign($pay_config);
$xmlData = $this->toXml($pay_config);
$doing = $this->requestXml($url, $xmlData);
var_dump($doing);
$this->orderquery($pay_config['out_trade_no']);
$this->refund($pay_config['out_trade_no'], $pay_config['total_fee']);
$this->refundquery($pay_config['out_trade_no']);
}
public function sandbox_test_1003()
{
$pay_config['appid'] = $this->appid;
$pay_config['mch_id'] = $this->mch_id;
$pay_config['attach'] = 'test';
$pay_config['body'] = 'test';
$pay_config['openid'] = '**********';
$pay_config['out_trade_no'] = 'sandbox_test_551';
$pay_config['total_fee'] = 551;
$pay_config['cash_fee'] = 550;
$pay_config['coupon_fee'] = 1;
$pay_config['coupon_count'] = 1;
$pay_config['coupon_fee_0'] = 1;
$pay_config['coupon_type_0'] = 'NOCASH';
$pay_config['nonce_str'] = $this->randomString(32);
$pay_config['trade_type'] = 'JSAPI';
$pay_config['notify_url'] = 'http://coupon.cc/admin/index/index';
$pay_config['sign'] = $this->sign($pay_config);
$result['pay_config'] = $pay_config;
$xmlData = $this->toXml($pay_config);
$url = 'https://api.mch.weixin.qq.com/sandboxnew/pay/unifiedorder';
$doing = $this->requestXml($url, $xmlData);
var_dump($doing);
$this->orderquery($pay_config['out_trade_no']);
}
public function sandbox_test_1004()
{
$pay_config['appid'] = $this->appid;
$pay_config['mch_id'] = $this->mch_id;
$pay_config['attach'] = 'test';
$pay_config['body'] = 'test';
$pay_config['openid'] = '**********';
$pay_config['out_trade_no'] = 'sandbox_test_552';
$pay_config['total_fee'] = 552;
$pay_config['cash_fee'] = 551;
$pay_config['coupon_fee'] = 1;
$pay_config['coupon_count'] = 1;
$pay_config['coupon_fee_0'] = 1;
$pay_config['coupon_type_0'] = 'NOCASH';
$pay_config['nonce_str'] = $this->randomString(32);
$pay_config['trade_type'] = 'JSAPI';
$pay_config['notify_url'] = 'http://coupon.cc/admin/index/index';
$pay_config['sign'] = $this->sign($pay_config);
$result['pay_config'] = $pay_config;
$xmlData = $this->toXml($pay_config);
$url = 'https://api.mch.weixin.qq.com/sandboxnew/pay/unifiedorder';
$doing = $this->requestXml($url, $xmlData);
var_dump($doing);
$this->orderquery($pay_config['out_trade_no']);
$this->refund($pay_config['out_trade_no'], $pay_config['total_fee']);
$this->refundquery($pay_config['out_trade_no']);
}
public function sandbox_test_1005()
{
$url = 'https://api.mch.weixin.qq.com/sandboxnew/pay/downloadbill';
$pay_config['appid'] = $this->appid;
$pay_config['mch_id'] = $this->mch_id;
$pay_config['nonce_str'] = $this->randomString(32);
$pay_config['bill_date'] = date('Ymd');
$pay_config['bill_type'] = 'ALL';
$pay_config['bill_type'] = 'SUCCESS';
$pay_config['bill_type'] = 'REFUND';
$pay_config['sign'] = $this->sign($pay_config);
$xmlData = $this->toXml($pay_config);
$doing = $this->requestXml($url, $xmlData);
var_dump($doing);
}
public function orderquery($out_trade_no)
{
$url = 'https://api.mch.weixin.qq.com/sandboxnew/pay/orderquery';
$pay_config['mch_id'] = $this->mch_id;
$pay_config['out_trade_no'] = $out_trade_no;
$pay_config['sign'] = $this->sign($pay_config);
$xmlData = $this->toXml($pay_config);
$doing = $this->requestXml($url, $xmlData);
var_dump($doing);
}
public function refund($out_no, $fee)
{
$url = 'https://api.mch.weixin.qq.com/sandboxnew/pay/refund';
$pay_config['mch_id'] = $this->mch_id;
$pay_config['out_trade_no'] = $out_no;
$pay_config['out_refund_no'] = $out_no;
$pay_config['total_fee'] = $fee;
$pay_config['refund_fee'] = $fee;
$pay_config['sign'] = $this->sign($pay_config);
$xmlData = $this->toXml($pay_config);
$doing = $this->requestXml($url, $xmlData);
var_dump($doing);
}
public function refundquery($out_no)
{
$url = 'https://api.mch.weixin.qq.com/sandboxnew/pay/refundquery';
$pay_config['mch_id'] = $this->mch_id;
$pay_config['out_trade_no'] = $out_no;
$pay_config['sign'] = $this->sign($pay_config);
$xmlData = $this->toXml($pay_config);
$doing = $this->requestXml($url, $xmlData);
var_dump($doing);
}
// 生成随机字符串
private function randomString($len = 32)
{
$string = '';
$char = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
for ($i = 0; $i < $len; $i++) {
$string .= $char[mt_rand(0, strlen($char) - 1)];
}
return $string;
}
// 生成签名
private function sign($array, $key = false)
{
ksort($array);
$string = $this->toUrlParams($array);
if ($key) {
$string .= '&key=' . $this->mch_key;
} else {
$string .= '&key=' . $this->getsignkey();
}
$string = md5($string);
$string = strtoupper($string);
return $string;
}
// 拼接字符串
private function toUrlParams($array)
{
$string = '';
foreach ($array as $key => $value) {
if ($key != 'sign' && $value != '') {
$string .= $key . "=" . $value . "&";
}
}
$string = trim($string, '&');
return $string;
}
// 转换为xml格式
private function toXml($array)
{
$xml = "<xml>\n";
foreach ($array as $key => $value) {
if (is_numeric($value)) {
$xml .= "<" . $key . ">" . $value . "</" . $key . ">";
} else {
$xml .= "<" . $key . "><![CDATA[" . $value . "]]></" . $key . ">";
}
}
$xml .= "</xml>";
return $xml;
}
// 从xml格式转换
private function fromXml($xml)
{
$xml = str_replace('<![CDATA]>', '', $xml);
$xml = str_replace('&', '{xml}', $xml);
// libxml_disable_entity_loader(true);
$xml = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$xml = json_decode(json_encode($xml), true);
$xml = str_replace('{xml}', '&', $xml);
return $xml;
}
// 获取xml参数的接口返回值
private function requestXml($url, $xmlData)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:text/xml; charset=utf-8'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlData);
$result['info'] = curl_exec($ch);
$result['status'] = curl_error($ch);
curl_close($ch);
return $result;
}
}
PHP 微信商户接口升级方法
最新推荐文章于 2022-07-16 23:00:23 发布