微信转账、退款

<?php

/**
 * Class WeiXinRefund
 */
class WeiXinRefund{
    protected $mchid;
    protected $appid;
    protected $key;
    protected $sslcertPath;
    protected $sslkeyPath;
    protected $openid;
    protected $totalFee;
    protected $outRefundNo;
    protected $outTradeNo;
    protected $refundFee;
    protected $amount;
    protected $desc;

    /**
     * WeiXinRefund constructor.
     * @param $mchid 商户号
     * @param $appid 小程序appid
     * @param $key 商户号秘钥
     * @param $sslcertPath 证书文件路径
     * @param $sslkeyPath 证书秘钥文件路径
     */
    public function __construct($mchid, $appid, $key, $sslcertPath, $sslkeyPath){
        $this->mchid  = $mchid;//商户号
        $this->appid  = $appid;//小程序appid
        $this->key  = $key;//商户号秘钥
        $this->sslcertPath = $sslcertPath;//证书文件路径
        $this->sslkeyPath  = $sslkeyPath;//证书秘钥路径
    }

    /**
     * 退款
     * @param $openid 用户的openid
     * @param $totalFee 订单金额
     * @param $outTradeNo 商户退款单号
     * @param $outRefundNo 商户单号
     * @param $refundFee 退款金额
     * @return mixed
     */
    public function refund($openid,$totalFee,$outTradeNo,$outRefundNo,$refundFee){
        $this->openid = $openid;
        $this->totalFee = $totalFee * 100;		//订单金额
        $this->outRefundNo = $outRefundNo;		//商户退款单号
        $this->outTradeNo = $outTradeNo;		//商户单号
        $this->refundFee = $refundFee * 100;	//退款金额

        $result = $this->wxRefund();
        return $result;
    }

    //退款方法
    private function wxRefund(){
        //通过微信api进行退款流程
        $parma = array(
            'appid'=> $this->appid,
            'mch_id'=> $this->mchid,
            'nonce_str'=> $this->createNoncestr(),
            'out_trade_no'=> $this->outTradeNo,
            'out_refund_no'=> $this->outRefundNo,
            'total_fee'=> $this->totalFee,
            'refund_fee'=> $this->refundFee
        );
        $parma['sign'] = $this->getSign($parma);
        $xmldata = $this->arrayToXml($parma);
        $url = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
        $xmlresult = $this->postXmlSSLCurl($xmldata,$url);
        $result = $this->xmlToArray($xmlresult);
        return $result;
    }

    /**
     * 转账(注:商户号已入驻90日且截止今日回推30天商户号保持连续不间断的交易)
     * @param $openid 用户的openid
     * @param $outTradeNo 自定义订单号
     * @param $amount 转账金额
     * @param $desc 描述
     * @return array
     */
    public function withdraw($openid,$outTradeNo,$amount,$desc){
        $this->openid = $openid;//用户openid
        $this->outTradeNo = $outTradeNo;//订单号
        $this->amount = $amount * 100;//金额
        $this->desc = $desc;//企业付款备注

        $result = $this->wxWithdraw();
        return $result;
    }

     //提现方法
    private function wxWithdraw(){
        $arr = array();
        $arr['mch_appid'] = $this->appid;
        $arr['mchid'] =  $this->mchid;
        $arr['nonce_str'] = $this->createNoncestr();
        $arr['partner_trade_no'] = $this->outTradeNo;//订单号
        $arr['openid'] = $this->openid;
        $arr['check_name'] = 'NO_CHECK';//是否验证用户真实姓名,这里不验证
        $arr['amount'] = $this->amount;//付款金额,单位为分
        $arr['desc'] = $this->desc;//描述信息

        $arr['sign'] = $this->getSign($arr);//签名

        $var = $this->arrayToXml($arr);

        $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
        $xml = $this->postXmlSSLCurl($url, $var, 30);
        $rdata = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
        $return_code = (string)$rdata->return_code;
        $result_code = (string)$rdata->result_code;
        $return_code = trim(strtoupper($return_code));
        $result_code = trim(strtoupper($result_code));

        if ($return_code == 'SUCCESS' && $result_code == 'SUCCESS') {
            $isrr = array(
                'errmsg'=>'ok',
                'error' => 1,
            );
        } else {
            $returnmsg = (string)$rdata->err_code_des;
            $isrr = array(
                'error' => 0,
                'errmsg' => $returnmsg,
            );
        }
        return $isrr;
    }

    //需要使用证书的请求
    function postXmlSSLCurl($xml,$url,$second=30){
        $ch = curl_init();
        //超时时间
        curl_setopt($ch,CURLOPT_TIMEOUT,$second);

        //这里设置代理,如果有的话

        //curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');

        //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
        curl_setopt($ch,CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);

        //设置header
        curl_setopt($ch,CURLOPT_HEADER,FALSE);

        //要求结果为字符串且输出到屏幕上
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);

        //设置证书

        //使用证书:cert 与 key 分别属于两个.pem文件

        //默认格式为PEM,可以注释
        curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
        curl_setopt($ch,CURLOPT_SSLCERT, $this->sslcertPath);

        //默认格式为PEM,可以注释
        curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
        curl_setopt($ch,CURLOPT_SSLKEY, $this->sslkeyPath);

        //post提交方式
        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);
            echo "curl出错,错误码:$error"."<br>";die;
            curl_close($ch);
            return false;
        }
    }

    //数组转字符串方法
    protected function arrayToXml($arr){
        $xml = "<xml>";
        foreach ($arr as $key=>$val) {
            if (is_numeric($val)){
                $xml.="<".$key.">".$val."</".$key.">";
            }else{
                $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
            }
        }
        $xml.="</xml>";
        return $xml;
    }

    protected function xmlToArray($xml){
        $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $array_data;
    }
    /*
     * 对要发送到微信统一下单接口的数据进行签名
     */
    protected function getSign($Obj){
        foreach ($Obj as $k => $v){
            $Parameters[$k] = $v;
        }

        //签名步骤一:按字典序排序参数
        ksort($Parameters);
        $String = $this->formatBizQueryParaMap($Parameters, false);

        //签名步骤二:在string后加入KEY
        $String = $String."&key=".$this->KEY;

        //签名步骤三:MD5加密
        $String = md5($String);

        //签名步骤四:所有字符转为大写
        $result_ = strtoupper($String);
        return $result_;
    }

    /*
     *排序并格式化参数方法,签名时需要使用
     */
    protected function formatBizQueryParaMap($paraMap, $urlencode){
        $buff = "";
        ksort($paraMap);
        foreach ($paraMap as $k => $v) {
            if($urlencode)
            {
                $v = urlencode($v);
            }
            //$buff .= strtolower($k) . "=" . $v . "&";
            $buff .= $k . "=" . $v . "&";
        }
        $reqPar = "";
        if (strlen($buff) > 0)
        {
            $reqPar = substr($buff, 0, strlen($buff)-1);
        }
        return $reqPar;
    }

    /*
     * 生成随机字符串方法
     */
    protected function createNoncestr($length = 32){
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        $str ="";
        for ( $i = 0; $i < $length; $i++ ) {
            $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
        }
        return $str;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要开发java的微信转账到零钱功能,需要进行以下几个步骤。 首先,我们需要在微信开放平台注册一个开发者账号,并创建一个应用。在创建应用时,需要选择相应的应用类型,如公众号、小程序或移动应用程序。 接下来,我们需要使用微信支付的API来实现转账功能。微信支付提供了一套完整的API,包括支付、退款、查询余额等功能。我们可以通过调用相应的API来实现从微信转账到零钱的功能。 在进行转账之前,需要用户授权认证和绑定银行卡。用户需要在微信中进行授权认证,以便我们的应用可以获得用户的授权和支付权限。 一旦用户完成授权认证,我们就可以开始调用API来实现转账功能了。首先,我们需要通过API向微信支付申请支付的权限和生成支付订单。然后,用户可以选择转账的金额和目标账户,如零钱账户。 在用户确认转账后,我们使用API将转账请求发送给微信支付平台,并等待平台的响应。如果转账成功,平台将返回一个响应消息,并更新用户的零钱账户余额。 最后,我们可以根据需要进行适当的错误处理和结果展示,以便用户能够及时了解转账结果和可能的错误原因。 总结起来,开发java的微信转账到零钱功能需要注册开发者账号、创建应用、使用微信支付API实现转账功能,并进行用户授权认证和绑定银行卡等步骤。此外,还需要处理错误和结果展示等细节,以提供良好的用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值