微信支付对接海关申报
1、先微信商户中心,进行海关申报配置
重点注意:请求接口前请先在以下页面提交您的海关信息,所有你需要报关的海关信息都需要提交
2、进行海关申报(本文章是没有子订单的)
注意:商户订单号金额以支付系统记录的为准,无需上传,如有子订单号则必须上传子订单应付金额、物流费、商品价格(应付金额=物流费+商品价格),记得区分APP和H5,不同的渠道,对应不同的配置。
3、代码实例
<?php
public function send(){
$params = [
'appid' => '',
'mch_id' => '',
'out_trade_no' => '',
'transaction_id' => '',
'customs' => '',
'mch_customs_no' => ''
];
ksort($params);
$string = $this->toUrlParams($params);
$key = '';
$string = $string . "&key=" . $key;
$string = md5($string);
$result = strtoupper($string);
$params['sign'] = $result;
$url = 'https://api.mch.weixin.qq.com/cgi-bin/mch/customs/customdeclareorder';
$res = $this->toXml($params);
$result = $this->postXmlCurl($url, $res);
$result = $this->fromXml($result);
if ($result && isset($result['return_code']) && $result['return_code'] == 'SUCCESS') {
echo '成功';
} else {
echo '失败';
}
}
public function toUrlParams($params)
{
$buff = "";
foreach ($params as $k => $v) {
if ($k != "sign" && $v != "" && !is_array($v)) {
$buff .= $k . "=" . $v . "&";
}
}
$buff = trim($buff, "&");
return $buff;
}
public function toXml($params)
{
if (!is_array($params)
|| count($params) <= 0
) {
throw new PaymentException("数组数据异常!");
}
$xml = "<xml>";
foreach ($params as $key => $val) {
if (is_numeric($val)) {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
} else {
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
}
}
$xml .= "</xml>";
return $xml;
}
public function postXmlCurl($url, $xml, $sslcert_path = '', $sslkey_path = '', $second = 30)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
if (!empty($sslcert_path) && !empty($sslkey_path)) {
curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLCERT, $sslcert_path);
curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLKEY, $sslkey_path);
}
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$data = curl_exec($ch);
if ($data) {
curl_close($ch);
return $data;
} else {
$error = curl_errno($ch);
curl_close($ch);
throw new PaymentException("curl出错,错误码:$error");
}
}
public function fromXml($xml)
{
if (!$xml) {
throw new PaymentException("xml数据异常!");
}
$bPreviousValue = libxml_disable_entity_loader(true);
$params = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
libxml_disable_entity_loader($bPreviousValue);
return $params;
}
举例如下:
<xml>
<appid>wx2421b1c4370ec43b</appid>
<customs>ZHENGZHOU_BS</customs>
<mch_customs_no>D00411</mch_customs_no>
<mch_id>1262544101</mch_id>
<order_fee>13110</order_fee>
<out_trade_no>15112496832609</out_trade_no>
<product_fee>13110</product_fee>
<sign>8FF6CEF879FB9555CD580222E671E9D4</sign>
<transaction_id>1006930610201511241751403478</transaction_id>
<transport_fee>0</transport_fee>
<fee_type>CNY</fee_type>
<sub_order_no>15112496832609001</sub_order_no>
</xml>
注:参数值用XML转义即可,CDATA标签用于说明数据不被XML解析器解析。
|