支付宝退款背后的秘密
citgpt 2024-09-26 11:30 9 浏览 0 评论
官方接口文档:
退款接口: 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 app\api\controller;
use think\Controller;
use think\Db;
use think\Input;
use think\facade\Request;
use app\api\controller\Common;
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 app\api\controller
*/
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/a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PObmn2mZyBd5rJrvcwWi6DKxfHYDjNJ0eO/xCFAFGeu0cc3rxiMEtMT/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/ZcU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lVAAC6rql4Cr7u6o9BhaPmMPaBfKwE309dXOq/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;
}
}
}
在你的管理后台调取支付类中的alipayRefund()即可!每天带你们了解咱们周围常见产品的实现原理,不明白的地方可以私聊我!
相关推荐
- js中arguments详解
-
一、简介了解arguments这个对象之前先来认识一下javascript的一些功能:其实Javascript并没有重载函数的功能,但是Arguments对象能够模拟重载。Javascrip中每个函数...
- firewall-cmd 常用命令
-
目录firewalldzone说明firewallzone内容说明firewall-cmd常用参数firewall-cmd常用命令常用命令 回到顶部firewalldzone...
- epel-release 是什么
-
EPEL-release(ExtraPackagesforEnterpriseLinux)是一个软件仓库,它为企业级Linux发行版(如CentOS、RHEL等)提供额外的软件包。以下是关于E...
- FullGC详解 什么是 JVM 的 GC
-
前言:背景:一、什么是JVM的GC?JVM(JavaVirtualMachine)。JVM是Java程序的虚拟机,是一种实现Java语言的解...
-
2024-10-26 08:50 citgpt
- 跨域(CrossOrigin)
-
1.介绍 1)跨域问题:跨域问题是在网络中,当一个网络的运行脚本(通常时JavaScript)试图访问另一个网络的资源时,如果这两个网络的端口、协议和域名不一致时就会出现跨域问题。 通俗讲...
- 微服务架构和分布式架构的区别
-
1、含义不同微服务架构:微服务架构风格是一种将一个单一应用程序开发为一组小型服务的方法,每个服务运行在自己的进程中,服务间通信采用轻量级通信机制(通常用HTTP资源API)。这些服务围绕业务能力构建并...
- 深入理解与应用CSS clip-path 属性
-
clip-pathclip-path是什么clip-path 是一个CSS属性,允许开发者创建一个剪切区域,从而决定元素的哪些部分可见,哪些部分会被隐...
-
2024-10-25 11:51 citgpt
- Request.ServerVariables 大全
-
Request.ServerVariables("Url")返回服务器地址Request.ServerVariables("Path_Info")客户端提供的路...
- python操作Kafka
-
目录一、python操作kafka1.python使用kafka生产者2.python使用kafka消费者3.使用docker中的kafka二、python操作kafka细...
- Runtime.getRuntime().exec详解
-
Runtime.getRuntime().exec详解概述Runtime.getRuntime().exec用于调用外部可执行程序或系统命令,并重定向外部程序的标准输入、标准输出和标准错误到缓冲池。...
- promise.all详解 promise.all是干什么的
-
promise.all详解promise.all中所有的请求成功了,走.then(),在.then()中能得到一个数组,数组中是每个请求resolve抛出的结果...
-
2024-10-24 16:21 citgpt
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- oracleclient (56)
- springbatch (59)
- oracle恢复数据 (56)
- 简单工厂模式 (68)
- 函数指针 (72)
- fill_parent (135)
- java配置环境变量 (140)
- linux文件系统 (56)
- 计算机操作系统教程 (60)
- 静态ip (63)
- notifyicon (55)
- 线程同步 (58)
- xcode 4 5 (60)
- 调试器 (60)
- c0000005 (63)
- html代码大全 (61)
- header utf 8 (61)
- 多线程多进程 (65)
- require_once (60)
- 百度网盘下载速度慢破解方法 (72)
- 谷歌浏览器免费入口 (72)
- npm list (64)
- 网站打开速度检测 (59)
- 网站建设流程图 (58)
- this关键字 (67)