第一步:封装退款服务类
在common/service目录下新建RefundService.php
内容如下:
namespace app\common\service;
use think\Db;
use SimpleXMLElement;
class RefundService
{
/**
* API申请退款接口
*/
public function apply_refund($parameter)
{
$url = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
$data = array(
'appid' => $parameter['appid'],
'mch_id' => $parameter['mch_id'],
'nonce_str' => createnoncestr(32),
'out_trade_no' => $parameter['out_trade_no'],
'out_refund_no' => $this->createnumbers('drug_refundlog'),
'total_fee' => $parameter['total_fee'] * 100,
'refund_fee' => $parameter['refund_fee'] * 100,
);
$data['sign'] = $this->getsign($data, $parameter);//生成签名
$xml = new SimpleXMLElement('<xml></xml>');
$this->data2xml($xml, $data);
$data_xml = $xml->asXML();
$response = $this->curl_post_ssl($url, $data_xml, $second = 30, $aHeader = array(), $parameter);
$result = $this->xmltoarray($response);
return $result;
}
/* 退款查询 */
public function refundquery($parameter){
$url = "https://api.mch.weixin.qq.com/pay/refundquery";
$data = array(
'appid' => $parameter['appid'],
'mch_id' => $parameter['mch_id'],
'out_trade_no' => $parameter['out_trade_no'],
'nonce_str' => createnoncestr(32),
'sign_type' => 'MD5',
);
$data['sign'] =$this->getsign($data,$parameter);
$xml = new SimpleXMLElement('<xml></xml>');
data2xml($xml, $data);
$data_xml = $xml->asXML();
//$s = print_r($data_xml, true);
//file_put_contents(time().'server_date.log',$s);
//设置curl超时时间
$curl_timeout = 30;
$response = $this-> postxmlcurl($data_xml,$url,$curl_timeout);
print_r($response);die;
$result = xmltoarray($response);
$prepay_id = $result["prepay_id"];
log_data(array($data,$result));
return $result;
}
/**
* API_统一下单接口 以post方式提交xml到对应的接口url 2016-08-17
*/
public function postxmlcurl($xml,$url,$second=30){
$ch = curl_init();
//设置超时
//