官方接口文檔:
退款接口: https://opendocs.alipay.com/apis/api_1/alipay.trade.refund
支付訂單查詢接口:https://opendocs.alipay.com/apis/api_1/alipay.trade.query

示例代碼如下:
<?php
/**
* 掃碼支付
* 提醒:提交支付請求后微信會同步返回支付結(jié)果(沒有異步回調(diào)通知)。當(dāng)返回結(jié)果為“系統(tǒng)錯誤”時,商戶系統(tǒng)等待5秒后調(diào)用【查詢訂單API】,查詢支付實際交易結(jié)果;當(dāng)返回結(jié)果為“USERPAYING”時,商戶系統(tǒng)可設(shè)置間隔時間(建議10秒)重新查詢支付結(jié)果,直到支付成功或超時(建議30秒);
*/
namespace Appapicontroller;
use thinkController;
use thinkDb;
use thinkInput;
use thinkfacadeRequest;
use appapicontrollerCommon;
header("Access-Control-Allow-Origin:*");
header("Content-type: text/html; charset=utf-8");
require_once $_SERVER['DOCUMENT_ROOT'].'/vendor/Alipaydangmianfu_demo/f2fpay/model/builder/AlipayTradePayContentBuilder.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/vendor/Alipaydangmianfu_demo/f2fpay/service/AlipayTradeService.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/vendor/Alipaydangmianfu_demo/aop/AopClient.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/vendor/Alipaydangmianfu_demo/aop/request/AlipayTradeRefundRequest.php';
/**
* 支付寶當(dāng)面付
* @package appapicontroller
*/
class Alipay
{
/**
* Notes:
* User: 小海
* Time:下午 1:51
* DATE:2020/12/10 0010
*/
public function alipay($authCode, $totalAmount, $outTradeNo)
{
$common = new Common();
$config = array(
//簽名方式
'sign_type' => "RSA2",
//支付寶公鑰
// 'alipay_public_key' => "/fEsraprwCtzvzQETrNRwVxLO5jVmRGi60j8Ue1efIlzPXV9je9mkjzOmdssymZkh2QhUrCmZYI/FCEa3/cNMW0QIDAQAB",
'alipay_public_key' => '+rHrXw7L+/HlesZSXoMsYRed7Ray6oFu/YjRTJ2e3zxkhcHjcy5pZIauuT5dz1vM6XA3q38VILGQZb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h6iKb6iU1XlmZV3GWc+rTqm/RGs08uC4HKg16s2DGMXVE5VqIPObmn2mZyBd5rJrvcwWi6DKxfHYDjNJ0eO/xCFAFGeu0cc3rxiMEtMT/WtI0tsMM3TIiZZFNynOBVdwDzmt1dAdwKBgDlj9caxFTTB9h2lVAAC6rql4Cr7u6o9BhaPmMPaBfKwE309dXOq/FESuUEMOcdYVhLvBV/0+I9BqfDCZ7TdWeGs5EciTYkBCfZGBRWjULjcWC+46EKXtzpk/7SBGMfO/X74ogvAP13U7td7vvaYHsWrRIpJ6MPVLWlLhCfnhv6rAoGAPMhd6NmnnAyjXRZBDIHmqYzFS5FwTdO4gqWv/KtHRWS+oVt7YJ3wgMOPbZVaOvqJacovICv7niba+ZhQfBsECm1Za6Ft8I5U3okCsIyBUv5V80wDVExPe32qD4aTWJQgDbZ+tCSzDqbdUL94LrhxDGkifJn0S7xAjqNPY7PPBf0CgYAMG7Buyxb1ZLQq7X/0ippM1kQIBOYcmxyK5r7CHcgRmBFmNf9BzWEClunDOCrY5USD6LK+Z+XnXX2q4XkKCxug8iVWBPh4lsqd+Tsn0orDE7JRcoDwl8QwkjdlsalPq8tOv/A91YM2GRI3oWHiC9TyBLds3BgwmO0vaivhtJNVyA==",
//編碼格式
'charset' => "UTF-8",
//支付寶網(wǎng)關(guān)
'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
//應(yīng)用ID
'app_id' => "123423",
//異步通知地址,只有掃碼支付預(yù)下單可用
'notify_url' => "",
//最大查詢重試次數(shù)
'MaxQueryRetry' => "10",
//查詢間隔
'QueryDuration' => "3"
);
// (必填) 商戶網(wǎng)站訂單系統(tǒng)中唯一訂單號,64個字符以內(nèi),只能包含字母、數(shù)字、下劃線,
// 需保證商戶系統(tǒng)端不能重復(fù),建議通過數(shù)據(jù)庫sequence生成,
// $outTradeNo = "Dsx" . date('Ymdhis') . mt_rand(100, 1000);
$subject = "第三巷條形碼支付";
// $totalAmount = $_POST['payAmount']; // (必填) 訂單總金額,單位為元,不能超過1億元
// (必填) 付款條碼,用戶支付寶錢包手機app點擊“付款”產(chǎn)生的付款條碼
// $authCode = $_POST['auth_code']; //28開頭18位數(shù)字
// 支付超時,線下掃碼交易定義為5分鐘
$timeExpress = "5m";
// 創(chuàng)建請求builder,設(shè)置請求參數(shù)
$barPayRequestBuilder = new AlipayTradePayContentBuilder();
$barPayRequestBuilder->setOutTradeNo($outTradeNo);
$barPayRequestBuilder->setTotalAmount($totalAmount);
$barPayRequestBuilder->setAuthCode($authCode);
$barPayRequestBuilder->setTimeExpress($timeExpress);
$barPayRequestBuilder->setSubject($subject);
// 調(diào)用barPay方法獲取當(dāng)面付應(yīng)答
$barPay = new AlipayTradeService($config);
$barPayResult = $barPay->barPay($barPayRequestBuilder);
switch ($barPayResult->getTradeStatus()) {
case "SUCCESS":
// echo "支付寶支付成功:" . "<br>--------------------------<br>";
$data = $barPayResult->getResponse();
$result = $common->returnway(2000, "支付寶支付成功!", $data);
return json_encode($result);
break;
case "FAILED":
// echo "支付寶支付失敗!!!" . "<br>--------------------------<br>";
if (!empty($barPayResult->getResponse())) {
$data = $barPayResult->getResponse();
$result = $common->returnway(2002, "支付寶支付失敗!", $data);
return json_encode($result);
}
break;
case "UNKNOWN":
// echo "系統(tǒng)異常,訂單狀態(tài)未知!!!" . "<br>--------------------------<br>";
if (!empty($barPayResult->getResponse())) {
$result = $common->returnway(4000, "系統(tǒng)異常,訂單狀態(tài)未知!", $barPayResult->getResponse());
return json_encode($result);
}
break;
default:
echo "不支持的交易狀態(tài),交易返回異常!!!";
break;
}
return;
}
/**
* 支付寶退貨處理
* 退貨前提是必須支付成功
* @$out_trade_no 平臺訂單號
* @$wepay_serial 支付寶交易號
* @$totalFee //退款金額
*/
public function alipayRefund($out_trade_no, $wepay_serial, $totalFee)
{
$aop = new AopClient ();
$aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$aop->appId = '12342312';
$aop->alipayrsaPublicKey = '+rHrXw7L+/HlesZSXoMsYRed7Ray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Cwa6rOwKBgQCW7M1myaGB/aCxeMaCvBQNdPrwuKHaSjOzPh6iKb6iU1XlmZV3GWc+rTqm/RGs08uC4HKg16s2DGMXVE5VqIPObmn2mZyBd5rJrvcwWi6DKxfHYDjNJ0eO/xCFAFGeu0cc3rxiMEtMT/WtI0tsMM3TIiZZFNynOBVdwDzmt1dAdwKBgDlj9caxFTTB9h2lVAAC6rql4Cr7u6o9BhaPmMPaBfKwE309dXOq/FESuUEMOcdYVhLvBV/0+I9BqfDCZ7TdWeGs5EciTYkBCfZGBRWjULjcWC+46EKXtzpk/7SBGMfO/X74ogvAP13U7td7vvaYHsWrRIpJ6MPVLWlLhCfnhv6rAoGAPMhd6NmnnAyjXRZBDIHmqYzFS5FwTdO4gqWv/KtHRWS+oVt7YJ3wgMOPbZVaOvqJacovICv7niba+ZhQfBsECm1Za6Ft8I5U3okCsIyBUv5V80wDVExPe32qD4aTWJQgDbZ+tCSzDqbdUL94LrhxDGkifJn0S7xAjqNPY7PPBf0CgYAMG7Buyxb1ZLQq7X/0ippM1kQIBOYcmxyK5r7CHcgRmBFmNf9BzWEClunDOCrY5USD6LK+Z+XnXX2q4XkKCxug8iVWBPh4lsqd+Tsn0orDE7JRcoDwl8QwkjdlsalPq8tOv/A91YM2GRI3oWHiC9TyBLds3BgwmO0vaivhtJNVyA==';
$aop->apiVersion = '1.0';
$aop->signType = 'RSA2';
$aop->postCharset = 'UTF-8';
$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;
if (!empty($resultCode) && $resultCode == 10000) {
return 1000;
} else {
return -1;
}
}
}
在你的管理后臺調(diào)取支付類中的alipayRefund()即可!每天帶你們了解咱們周圍常見產(chǎn)品的實現(xiàn)原理,不明白的地方可以私聊我!