支付寶支付、退款

<?php

namespace alipay;

//APP支付

use think\Loader;

class Alipay
{
    protected $gatewayUrl = "https://openapi.alipay.com/gateway.do";

    protected $appId = "";

    protected $rsaPrivateKey = "";

    public $alipayrsaPublicKey = "";

    protected $format = "json";

    protected $charset = "UTF-8";

    protected $signType = "RSA2";

    public function __construct()
    {
        Loader::import('alipay.aop.AopClient');
        Loader::import('alipay.aop.request.AlipayTradeAppPayRequest');
        Loader::import('alipay.aop.request.AlipayTradeRefundRequest');
    }

    /**
     * APP-支付
     * @param array $param
     * $param['body']           對一筆交易的具體描述信息
     * $param['subject']        商品的標(biāo)題/交易標(biāo)題/訂單標(biāo)題/訂單關(guān)鍵字等
     * $param['total_amount']   訂單總金額,單位為元,精確到小數(shù)點(diǎn)后兩位
     * $param['out_trade_no']   商戶網(wǎng)站唯一訂單號(hào)
     * @return string
     */
    public function pay(array $param)
    {
        $aop = new \AopClient();
        $aop->gatewayUrl = $this->gatewayUrl;
        $aop->appId = $this->appId;
        $aop->rsaPrivateKey = $this->rsaPrivateKey;
        $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
        $aop->format = $this->format;
        $aop->charset = $this->charset;
        $aop->signType = $this->signType;
        //實(shí)例化具體API對應(yīng)的request類,類名稱和接口名稱對應(yīng),當(dāng)前調(diào)用接口名稱:alipay.trade.app.pay
        $request = new \AlipayTradeAppPayRequest();
        //SDK已經(jīng)封裝掉了公共參數(shù),這里只需要傳入業(yè)務(wù)參數(shù)
        $data = [
            "body" => $param['body'],
            "subject" => $param['subject'],
            "timeout_express" => "30m",
            "total_amount" => $param['total_amount'],
            "out_trade_no" => $param['out_trade_no']
        ];
        $request->setNotifyUrl('http://'.$_SERVER['HTTP_HOST'].'/user/Allot/appNotifys');
        $request->setBizContent(json_encode($data));
        //這里和普通的接口調(diào)用不同,使用的是sdkExecute
        $result = $aop->sdkExecute($request);
        //htmlspecialchars是為了輸出到頁面時(shí)防止被瀏覽器將關(guān)鍵參數(shù)html轉(zhuǎn)義,實(shí)際打印到日志以及http傳輸不會(huì)有這個(gè)問題
        //就是orderString 可以直接給客戶端請求,無需再做處理。
        return $result;
    }

    /**
     * APP-退款
     * @param $out_trade_no     訂單支付時(shí)傳入的商戶訂單號(hào),不能和 trade_no同時(shí)為空
     * @param $refund_amount    訂單支付總金額
     * @param $trade_no         支付寶交易號(hào)
     * @return bool|mixed|\SimpleXMLElement
     * @throws \Exception
     */
    public function refund($out_trade_no, $refund_amount, $trade_no = "")
    {
        $aop = new \AopClient();
        $aop->gatewayUrl = $this->gatewayUrl;
        $aop->appId = $this->appId;
        $aop->rsaPrivateKey = $this->rsaPrivateKey;
        $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
        $aop->apiVersion = '1.0';
        $aop->postCharset = $this->charset;
        $aop->format = $this->format;
        $aop->signType = $this->signType;
        $request = new \AlipayTradeRefundRequest();
        $data = [
            'out_trade_no' => $out_trade_no,
            'trade_no' => $trade_no,
            'refund_amount' => $refund_amount,
        ];
        $request->setBizContent(json_encode($data));
        $result = $aop->execute($request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if (!empty($resultCode) && $resultCode == 10000) {
            $res = [
                'msg'               =>  $result->$responseNode->msg,
                'buyer_logon_id'    =>  $result->$responseNode->buyer_logon_id,     //用戶的登錄id
                'buyer_user_id'     =>  $result->$responseNode->buyer_user_id,      //買家在支付寶的用戶id
                'fund_change'       =>  $result->$responseNode->fund_change,        //本次退款是否發(fā)生了資金變化
                'gmt_refund_pay'    =>  $result->$responseNode->gmt_refund_pay,     //退款支付時(shí)間
                'out_trade_no'      =>  $result->$responseNode->out_trade_no,       //商戶訂單號(hào)
                'refund_fee'        =>  $result->$responseNode->refund_fee,         //退款總金額
                'send_back_fee'     =>  $result->$responseNode->send_back_fee,      //(文檔未找到)
                'trade_no'          =>  $result->$responseNode->trade_no            //支付寶交易號(hào)
            ];
            return $res;
        } else {
            return $result;
        }
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容