tp5封装微信退款接口

这篇博客详细介绍了如何在TP5框架下封装微信退款服务类,包括apply_refund和refundquery方法,用于申请退款和查询退款状态。同时展示了在控制器中调用退款接口的流程,包括退款逻辑和数据库操作。
摘要由CSDN通过智能技术生成

第一步:封装退款服务类

在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();
        //设置超时
//    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值