官方接口文档:
退款接口: https://opendocs.alipay.com/apis/api_1/alipay.trade.refund
支付订单查询接口:https://opendocs.alipay.com/apis/api_1/alipay.trade.query
示例代码如下:
<?php
/**
* 扫码支付
* 提醒:提交支付请求后微信会同步返回支付结果(没有异步回调通知)。当返回结果为“系统错误”时,商户系统等待5秒后调用【查询订单API】,查询支付实际交易结果;当返回结果为“USERPAYING”时,商户系统可设置间隔时间(建议10秒)重新查询支付结果,直到支付成功或超时(建议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';
/**
* 支付宝当面付
* @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/YjRTJ2e3zxkhcHjcy5pZIauuT5dz1vM6XA3q38VILGQZbK3RusYlcpNopbR5f6j8HOoIgSk3+qdmRgga2hgwhgd1JOj7R/aTmPGzTJ81E6FXCGN83PLnHYr0URgtnpjLVOg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CFAFGeu0cc3rxiMEtMT/WtI0tsMM3TIiZZFNynOBVdwDzmt1dAdwKBgDlj9caxFTTB9h2lVAAC6rql4Cr7u6o9BhaPmMPaBfKwE309dXOq/FESuUEMOcdYVhLvBV/0+I9BqfDCZ7TdWeGs5EciTYkBCfZGBRWjULjcWC+46EKXtzpk/7SBGMfO/X74ogvAP13U7td7vvaYHsWrRIpJ6MPVLWlLhCfnhv6rAoGAPMhd6NmnnAyjXRZBDIHmqYzFS5FwTdO4gqWv/KtHRWS+oVt7YJ3wgMOPbZVaOvqJacovICv7niba+ZhQfBsECm1Za6Ft8I5U3okCsIyBUv5V80wDVExPe32qD4aTWJQgDbZ+tCSzDqbdUL94LrhxDGkifJn0S7xAjqNPY7PPBf0CgYAMG7Buyxb1ZLQq7X/0ippM1kQIBOYcmxyK5r7CHcgRmBFmNf9BzWEClunDOCrY5USD6LK+Z+XnXX2q4XkKCxug8iVWBPh4lsqd+Tsn0orDE7JRcoDwl8QwkjdlsalPq8tOv/A91YM2GRI3oWHiC9TyBLds3BgwmO0vaivhtJNVyA==",
//编码格式
'charset' => "UTF-8",
//支付宝网关
'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
//应用ID
'app_id' => "123423",
//异步通知地址,只有扫码支付预下单可用
'notify_url' => "",
//最大查询重试次数
'MaxQueryRetry' => "10",
//查询间隔
'QueryDuration' => "3"
);
// (必填) 商户网站订单系统中唯一订单号,64个字符以内,只能包含字母、数字、下划线,
// 需保证商户系统端不能重复,建议通过数据库sequence生成,
// $outTradeNo = "Dsx" . date('Ymdhis') . mt_rand(100, 1000);
$subject = "第三巷条形码支付";
// $totalAmount = $_POST['payAmount']; // (必填) 订单总金额,单位为元,不能超过1亿元
// (必填) 付款条码,用户支付宝钱包手机app点击“付款”产生的付款条码
// $authCode = $_POST['auth_code']; //28开头18位数字
// 支付超时,线下扫码交易定义为5分钟
$timeExpress = "5m";
// 创建请求builder,设置请求参数
$barPayRequestBuilder = new AlipayTradePayContentBuilder();
$barPayRequestBuilder->setOutTradeNo($outTradeNo);
$barPayRequestBuilder->setTotalAmount($totalAmount);
$barPayRequestBuilder->setAuthCode($authCode);
$barPayRequestBuilder->setTimeExpress($timeExpress);
$barPayRequestBuilder->setSubject($subject);
// 调用barPay方法获取当面付应答
$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 "系统异常,订单状态未知!!!" . "<br>--------------------------<br>";
if (!empty($barPayResult->getResponse())) {
$result = $common->returnway(4000, "系统异常,订单状态未知!", $barPayResult->getResponse());
return json_encode($result);
}
break;
default:
echo "不支持的交易状态,交易返回异常!!!";
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+/HlesZSXoMsYRed7Ray6oFu/YjRTJ2e3zxkhcHjcy5pZIauuT5dz1vM6XA3q38VILGQZbK3RusYlcpNopbR5f6j8HOoIgSk3+qdmRgga2hgwhgd1JOj7R/aTmPGzTJ81E6FXCGN83PLnHYr0URgtnpjLVOg+xV5fiAeWGe9SOUasW3sfqFk54WRwBOcOm70Owl9+juDhGICJObdIxrbck0ij7xkSlU89/ZcUQQ4UNLSya6jghdF8oQzeeszHR0/QCYfWELTCoxUwH46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UEMOcdYVhLvBV/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;
}
}
}
在你的管理后台调取支付类中的alipayRefund()即可!每天带你们了解咱们周围常见产品的实现原理,不明白的地方可以私聊我!