php支付宝支付,退款,回调

其实小编发过很多类似这些的文章,你需要这类文章的话,直接到公众号里搜索就好了,今天再发一篇比较完整的,有关支付宝支付,退款与回调的文章!这类接口复制来复制去都是一个样,只要你理解其中的思路以及需要调用的数据就好了!

1.首先你要加载你的支付宝配置项

 include('alipay/aop/AopClient.php');
 include('alipay/aop/request/AlipayTradeAppPayRequest.php');
 include('alipay/aop/request/AlipayTradeRefundRequest.php');
 //支付宝appid  (应用标识)
 define("ALIPAY_APPID", "");
 //支付宝(支付宝私钥)
 define("PRIVATE_KEY", "");
 //支付宝(支付宝公钥)
 define("PUBLIC_KEY", "");

2,支付宝支付函数

/**
     * 支付宝支付
     */
    public function appAlipay(){
            // 时间戳
            $timeStamp = strval(time());
            $totalFee = 1;
            // 支付结果显示文字
            $serial_number = date("Ymdhis") . mt_rand(10, 99);
            $aop = new \AopClient();
            $bizcontent = json_encode([
                'body'=>"测试",                             //商品名称
                'subject'=>'测试',                          //主题
                'out_trade_no'=> $serial_number,           //此订单号为商户唯一订单号
                'total_amount'=> $totalFee,                //保留两位小数
                'product_code'=>'QUICK_MSECURITY_PAY'
            ]);

            //支付宝开始
            $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
            //实际上线app id需真实的
            $aop->appId = ALIPAY_APPID;
            $aop->rsaPrivateKey = PRIVATE_KEY;
            $aop->format = "json";
            $aop->charset  = "UTF-8";
            $aop->signType = "RSA2";
            $aop->alipayrsaPublicKey = PUBLIC_KEY;

            //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
            $request = new \AlipayTradeAppPayRequest();
            //支付宝回调
            $notifyUrl="https://openapi.alipay.com";
            $request->setNotifyUrl($notifyUrl);
            $request->setBizContent($bizcontent);
            //这里和普通的接口调用不同,使用的是sdkExecute
            $response = $aop->sdkExecute($request);
            $this->echoSuccess($response);

    }

3,支付宝回调函数

 /**
     * 支付宝支付回调
     *
     */
    public function  alipayNotify() {
        if(!empty($_POST)){
            $serial=$_POST['out_trade_no'];
            $wepay_serial = $_POST['trade_no'];
            if (!empty($wepay_serial)) {
                //这里可以写自己需求
                echo "返回的参数out_trade_no:".$serial;
                echo "返回的参数trade_no:".$wepay_serial;
            }
        }
        echo 'success';
    }

4,支付宝退款函数

/**
     * 支付宝退货处理
     * 退货前提是必须支付成功
     * @param type $orderId
     * @return bool
     */
    public function alipayRefund($out_trade_no,$wepay_serial,$totalFee) {
         $aop = new AopClient ();
         $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
         $aop->appId = ALIPAY_APPID;
         $aop->rsaPrivateKey = PRIVATE_KEY;
         $aop->alipayrsaPublicKey=PUBLIC_KEY;
         $aop->apiVersion = '1.0';
         $aop->signType = 'RSA2';
         $aop->postCharset='GBK';
         $aop->format='json';
         $request = new AlipayTradeRefundRequest ();
         $bizcontent = json_encode([
            'out_trade_no'=>$out_trade_no,
            'trade_no'=> $wepay_serial,
            'refund_amount'=> $totalFee,
            'refund_reason'=>'正常退款'
             ]);
         $request->setBizContent($bizcontent);
         $result = $aop->execute($request);
         $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
         $resultCode = $result->$responseNode->code;
         echo $resultCode;
         if(!empty($resultCode)&&$resultCode == 10000){
               echo "退款成功";
           } else {
                echo "退款失败";
           }
        }

    /**
     * 输出成功JSON消息
     */
    final public function echoSuccess($msg = 'success') {
        $this->echoMsg(0, $msg);
        exit();
    }

    /**
     * 输出JSON消息
     * @param mixed $code
     * @param mixed $msg
     */
    final public function echoMsg($code, $msg = '', $options = JSON_UNESCAPED_UNICODE) {
        return $this->echoJson(array(
                'ret_code' => $code,
                'ret_msg'  => $msg
        ), $options);
    }

    /**
     * 输出JSON
     * @param mixed $arr
     */
    final public function echoJson($arr, $options = JSON_UNESCAPED_UNICODE) {
        header('Content-Type: application/json; charset=utf-8');
        if (strpos(PHP_VERSION, '5.3') > -1) {
            // php 5.3-
            echo json_encode($arr);
        } else {
            // php 5.4+
            echo json_encode($arr, $options);
        }
        return true;
    }

}