第三方技术 支付 退款

https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1  【微信支付】APP支付开发者文档

https://doc.open.alipay.com/doc2/detail?treeId=59&articleId=103666&docType=1    服务器异步通知参数说明

https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.9cVrvO&treeId=204&articleId=105465&docType=1   App支付请求参数说明

stripe 支付 退款

第一步先到stripe 官网  https://stripe.com 注册一个账号

然后到  https://pan.baidu.com/s/1eRIVVGm  这个链接里面下载例子(这个文件里只包含支付demo和公共api接口包)

Api 接口地址 :https://stripe.com/docs/api  (这个地址可能在谷歌里面打开里面什么都没有,大家可以到火狐或者IE浏览器上看看),api地址里面包含各种有关支付、退款的例子。

退款:退款里面调用的文件在 https://pan.baidu.com/s/1eRIVVGm 这里面

/*
 * 退款测试
 */
public function refund(){
    require_once 'public/stripe-php-3.20.0/init.php';
    \Stripe\Stripe::setApiKey("sk_test_BQokikJOvBiI2HlWgH4olfQ2");

    $re = \Stripe\Refund::create(array(
        "charge" => "ch_18lF8dEm2C3rQ9STPtScvnth",
        "amount" => "50",
    ));
    echo "<pre>";
    var_dump($re);die;
}

 

支付宝 支付demo

支付宝官网demo地址:https://b.alipay.com/order/techService.htm

打开连接后在最下面有所有的demo

 

支付宝退款

    https://docs.open.alipay.com/api_1/alipay.trade.refund

微信退款

    /*
     * 订单退款
     */
    static function wx_tui($order_id,$order){
        // 引入微信api
        require_once "./api/wxpay/lib/WxPay.Api.php";
        $input = new \WxPayRefund();
        $input->SetTransaction_id($order_id);
        $input->SetTotal_fee(1);
        $input->SetRefund_fee(1);
        $input->SetOut_refund_no(\WxPayConfig::MCHID . date("YmdHis"));
        $input->SetOp_user_id(\WxPayConfig::MCHID);
        $result = \WxPayApi::refund($input);
//      var_dump($result);
//        F('wxrefund' . setName(), $result);
        // 更新退款信息
        if ($result['return_code'] == "SUCCESS")
        {
            $time = time();
            // 更新订单状态
            M("order")->where("order_id = {$order['order_id']}")
                ->save(array('refund_price'=>$order['total_price'],
                    'user_confirm_refund'=>1,
                    'refund_status_time'=>$time,
                    'refund_status'=>1,
                    'pay_status'=>6));
//            M("orders")->where(array("trade_no" => $trade))->save($saveData);
        }
    }

微信提现

  微信提现的前提需要开启    

S1D(TTGYO@T~$%_@%9)F8CS

   功能

    //微信提现
    public function wxtix(){
        $date['device_info'] = post("access_token");//微信支付分配的终端设备号
        $date['openid'] = post("openid");//商户appid下,某用户的openid
        $date['amount'] = post("money",1);//企业付款金额,单位为分
        $user_id = (int)post("user_id");
        ksort($date);
        $string = $this->ToUrlParams($date);
        $string = substr(md5('kaimi'.$string."kaimi"),8,16);
        $secret = post("secret",'');//加密字段
        if($string!=$secret){
            $this->showCode(-2, array());
        }
        $user_wallet = M("user_wallet")->where("user_id = {$user_id}")->find();
        if($user_wallet['wallet_money']<$date['amount']){
            $this->showCode(1, array(), '余额不足');
        }
        if(empty($date['device_info'])||empty($date['openid'])||empty($date['amount'])){
            $this->showCode(-1, array(), '参数缺少');
        }
        $date['partner_trade_no'] = "ti".time(). rand(10000, 99999);//商户订单号,需保持唯一性
        $date['amount'] = $date['amount'] * 100;
        $time = time();
        M("titrade_no")->add(array("user_id"=>$user_id,"type"=>2,"money"=>$date['amount']/100,"trade_no"=>$date['partner_trade_no'],"ctime"=>$time));
        $data = $this->wx_tixian($date);
        if($data['return_code']=='SUCCESS'||$data['result_code']=='SUCCESS'){
            M("user_wallet")->where("user_id = {$user_id}")->setDec("wallet_money",$date['amount']/100);
            $time = time();
            M("titrade_no")->where("trade_no = {$data['partner_trade_no']} and user_id = {$user_id}")->save(array('ti_time'=>$time,'status'=>1));
            $this->showCode(0, array(), '提现成功');
        }else{
            $this->showCode(1, array(), '提现失败');
        }
    }

    /**
     * @param $date
     * 生成签名
     */
    public function sign($date){
        //签名步骤一:按字典序排序参数
        ksort($date);
        $string = $this->ToUrlParams($date);
        //签名步骤二:在string后加入KEY
        $string = $string . "&key=".\WxPayConfig::KEY;
        //签名步骤三:MD5加密
        $string = md5($string);
        //签名步骤四:所有字符转为大写
        $result = strtoupper($string);
        return $result;
    }
    /**
     * 格式化参数格式化成url参数
     */
    public function ToUrlParams($date)
    {
        $buff = "";
        foreach ($date as $k => $v)
        {
            if($k != "sign" && $v != "" && !is_array($v)){
                $buff .= $k . "=" . $v . "&";
            }
        }

        $buff = trim($buff, "&");
        return $buff;
    }
    //生成XML
    function xml($arr){
        $str = "<xml>";
        foreach($arr as $k=>$v){
            $str .= "<{$k}>{$v}</{$k}>";
        }
        $str .= "</xml>";
        return $str;
    }
    /*
     * 微信提现
     */
    public function wx_tixian($data){
        header("Content-type: text/html; charset=utf-8");

        // 引入微信api
        require_once "./api/wxpay/lib/WxPay.Api.php";
        $date['mch_appid'] = \WxPayConfig::APPID;//微信分配的公众账号ID(企业号corpid即为此appId)
        $date['mchid'] = \WxPayConfig::MCHID;    //微信支付分配的商户号
        $date['nonce_str'] = 'kaimi' .time(). rand(10000, 99999); //随机字符串,不长于32位
        $date['partner_trade_no'] = "ti".time(). rand(10000, 99999);//商户订单号,需保持唯一性
        /**
         * NO_CHECK:不校验真实姓名
         *FORCE_CHECK:强校验真实姓名(未实名认证的用户会校验失败,无法转账)
         *OPTION_CHECK:针对已实名认证的用户才校验真实姓名(未实名认证用户不校验,可以转账成功)
         */
        $date['check_name'] = "NO_CHECK";
        $date['re_user_name']  = ""; //收款用户真实姓名。  如果check_name设置为FORCE_CHECK或OPTION_CHECK,则必填用户真实姓名
        $date['desc'] = "提现"; //企业付款操作说明信息。必填。
        $date['spbill_create_ip'] = $_SERVER["REMOTE_ADDR"]; //调用接口的机器Ip地址
        $date['sign'] = $this->sign($date);                //签名
        $data = $this->xml($date);
        $ch = curl_init();
        $MENU_URL = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
        curl_setopt($ch, CURLOPT_URL, $MENU_URL);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

        $zs1 = \WxPayConfig::getSSLcertPath();
        $zs2 = \WxPayConfig::getSSLKeyPath();
        curl_setopt($ch, CURLOPT_SSLCERT, $zs1);
        curl_setopt($ch, CURLOPT_SSLKEY, $zs2);
// curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01;
// Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $info = curl_exec($ch);

        if (curl_errno($ch)) {
            echo 'Errno' . curl_error($ch);
        }
        curl_close($ch);
        $data = \WxPayResults::Init($info);
        return $data;
    }

微信官网接口地址:https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_2

微信支付

/**
     *  帮我买下单  微信支付
     */
    public function weipay(){
        $order_id = I("order_id");
        $money = I("money");
        if($order_id == '' || $money == ''){
            $this->json("100090","缺少参数");
        }
        require_once "./public/weipay/lib/WxPay.Api.php";
        require_once "./public/weipay/example/log.php";
        require_once "./public/weipay/lib/WxPay.Config.php";
        $ordrTitle = "电达人订单支付";
        // 准备参数
        $notify_url = $_SERVER['HTTP_HOST']."/index.php/api/orders/order_callback_weipay";
        // 统一下单
        $input = new \WxPayUnifiedOrder();
        $input->SetBody($ordrTitle);
        $input->SetAttach($ordrTitle);
        $input->SetOut_trade_no($order_id);
        $input->SetTotal_fee($money * 100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetNotify_url($notify_url);
        $input->SetTrade_type("APP");
        $input->SetSpbill_create_ip($_SERVER['REMOTE_ADDR']);//终端ip
        $order = \WxPayApi::unifiedOrder($input);
        //dump($order);die;
        if ($order['return_code'] == 'SUCCESS')
        {
            $wxvalue = array();
            $wxvalue['appid'] = $order['appid'];
            $wxvalue['prepayid'] = $order['prepay_id'];
            $wxvalue['partnerid'] = $order['mch_id'];
            $wxvalue['package'] = 'Sign=WXPay';
            $wxvalue['timestamp'] = time();
            $wxvalue['noncestr'] = \WxPayApi::getNonceStr();
            ksort($wxvalue); // 排序
            $wxstr = $this->ToUrlParams($wxvalue);
            $string = $wxstr . "&key=" . \WxPayConfig::KEY;
            // 签名步骤三:MD5加密
            $md5string = md5($string);
            $wxvalue['sign'] = strtoupper($md5string);
            $wxvalue['title'] = $ordrTitle;
            $new = $wxvalue;
            $new['order_id'] = $order_id;
            //dump($new);die;
            $this->json('10001',$new);
            // json("10001", $wxvalue);

        }else{
            $this->json('405', '支付生成失败,请联系我们');
        }
    }

/**
     * 微信支付回调
     */
    public function order_callback_weipay(){
        $arr = $_POST;
        if (empty($arr))
        {
            $xml = file_get_contents('php://input');
            $arr = $this->FromXml($xml);
        }
        if (array_key_exists('openid', $arr))
        {
            if($arr['result_code']=="SUCCESS"){
                $orderid = $arr['out_trade_no'];
                $stas = M("order")->where("order_id='$orderid'")->field("statics")->find();
                $static = $stas["statics"];
                if($static == 2){
                    $da["statics"] = 3;
                    $da["is_zhi"] = 1;
                } else {
                    $da["statics"] = 9;
                    $da["is_zhi"] = 2;
                }
                M("order")->where("order_id = {$arr['out_trade_no']}")->save($da);
            }
            //$info = json_decode($arr,true);
            //    M("weixin")->add($arr);
            //echo '<xml><return_code>SUCCESS</return_code><return_msg>OK</return_msg></xml>';
            echo "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
        }
    }

    /**
     * 拼接key=>$value
     * @access public
     */
    protected function ToUrlParams ($wxarr)
    {
        $buff = "";
        foreach ($wxarr as $k => $v)
        {
            if ($k != "sign" && $v != "" && ! is_array($v))
            {
                $buff .= $k . "=" . $v . "&";
            }
        }

        $buff = trim($buff, "&");
        return $buff;
    }

    /**
     * xml数据转array
     * @param unknown $xml
     * @throws WxPayException
     * @return mixed
     */
    protected function FromXml ($xml)
    {
        if (! $xml)
        {
            return;
        }
        // 将XML转为array
        // 禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $data;
    }

微信官网demo地址 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1

支付宝支付

//支付宝
require_once "./public/alipay/lib/alipay_core.function.php";
require_once "./public/alipay/lib/alipay_rsa.function.php";

$data['notify_url'] = "http://".$_SERVER['HTTP_HOST'].'/index.php/Api/Orders/order_callback_alipay1';
$data['app_id'] = '2016112203112986';//Appid

$data['method'] = 'alipay.trade.app.pay';
$data['charset'] = 'utf-8';
$data['sign_type'] = 'RSA';
$data['timestamp'] = date("Y-m-d H:i:s");
$data['version'] = '1.0';
$data['timeout_express'] = '30m';
$data['biz_content'] = "{'seller_id':'2088521328498118','total_amount':$money,'subject':'电达人订单支付','body':'电达人','out_trade_no':'".$sheng_order."','product_code':'QUICK_MSECURITY_PAY'}";

$mygoods = argSort($data);
//拼接
$mystr = createLinkstring($mygoods);
//签名
$p = 'MIICXQIBAAKBgQDIQlDqpQXj1I2fNvGV5jEoslU2JXLhp8F0/ST9r07dip6cUBhD
03hfgm0kUZRzhTazRhK1HctvgIfBQ3aK2qKtYnSsBmXeqn5Om4CXdacbhLo17Iw+
z86i9xTo9htlWEmoSUjB49bFuZ29EW7UFO387LGHryW2WGNieI2+MrNVVwIDAQAB
AoGAKoaSolFq38e6T04enUNluwurtrTbIXo7RbmgClMY/0i5h94mWPjmPfn3bVf5
m1J1nDpMLLPoCISVz3+hA7mMA79xgL+PAvLmWnkPYHIo6rucG5BFhPLQo5z+w8J+
+e4Ri9e/I6yEOfUWAQnr74T1qmGxRihDcMHu07BE4f2EWKECQQDuq/odOY5onwSt
I7g0ojEz/0wz9X7nGIyUqmK+JwFchQARqOQQoAY1ngiCgY1co9HRmU7PdbLTi0Z8
ExNDlsgxAkEA1sxjh5Mq75ryA9BYWvL7fIM6k9Wn3GT1aMrJ41yppMVECLMi8Mat
r3KwT3vYku0x58La1G/2eFydIlfvAXacBwJBAOMDsWMa29mnZ0tILm3h1+sdad4C
JDKrpwvBBSrY7vKRjv89JbCqhT16k52OnGcBaxFyQQQTb4THdMOPYUNoqAECQAYN
tpRgkmo0TLPM0GCnzmyg6iq2b8enVL9CT9RHjURmhtdT7R0qDEUQj2V+EQl7A9gL
Rs2L5j9YJNmN7t90r0sCQQDECDHvrl6eSZMF1ZrLbGT9XdsfAkGuMt3yHVu8FU5/
fVEjJCF3e56F1qS6Hj41Kr371FhAXTW5uroAGEa07n4Z'; //
$sign = rsaSign($mystr,$p);
//对签名进行urlencode转码
$sign = urlencode($sign);
$data1['sign'] = $sign;

$orderInfor = $mystr."&sign=".$sign;
$this->json('10001',$orderInfor);

/*

*支付宝支付 回调函数

*/

if (array_key_exists('buyer_id', $arr))      //支付宝  回调函数
{
    require_once "./public/alipay/alipay.config.php";
    require_once "./public/alipay/lib/alipay_notify.class.php";
    require_once "./public/alipay/lib/alipay_rsa.function.php";
    require_once "./public/alipay/lib/alipay_core.function.php";
    //计算得出通知验证结果
    $alipayNotify = new \AlipayNotify($alipay_config);
    if($alipayNotify->getResponse($_POST['notify_id'])) {  //判断成功之后使用getResponse方法判断是否是支付宝发来的异步通知。
        if($alipayNotify->getSignVeryfy($_POST, $_POST['sign'])) {  //使用支付宝公钥验签
            if($_POST['trade_status'] == 'TRADE_FINISHED') {
            }
            if ($_POST['trade_status'] == 'TRADE_SUCCESS') {
                $da2["ding"] = $arr['total_amount']; //total_fee
                $da2["is_zhi"] = 1;//支付状态 支付定金
                $da2["statics"] = 3;//订单状态 支付定金
                $da2["zhi_type"] = 2;//支付方式 支付定金 2 支付宝
                $order_id = $arr['out_trade_no'];
                M("order")->where("ding_order ='$order_id'")->save($da2);
//                        $d = M("order")->getLastSql();
//                        $arr['c_name'] = $d;
//                        M("ceshi")->add($arr);
                $d = json_encode($arr);
                $ard['c_name'] = $d;
                M("ceshi")->add($ard);
            }
            M("zhifubao")->add($arr);
            echo "success";        //请不要修改或删除
        }else //验证签名失败
        {
//                    $arr['c_name'] = '4';
//                    M("ceshi")->add($arr);
//                    logResult('验证签名失败11');
            echo "sign fail";
        }
    }else //验证是否来自支付宝的通知失败
    {
//                $arr['c_name'] = '2';
//                M("ceshi")->add($arr);
        //    logResult('验证失败22');
        echo "response fail";
    }
}

posted on 2016-11-18 11:02  *孤独的夜行者*  阅读(2775)  评论(0编辑  收藏  举报

导航