支付扩展
本页面介绍如何扩展支付方式。
支付架构
PayService (支付服务接口)
│
├── WechatPayServiceImpl (微信支付)
├── AliPayServiceImpl (支付宝支付)
└── CustomPayServiceImpl (自定义支付)实现步骤
1. 实现支付接口
java
@Service
public class CustomPayServiceImpl implements PayService {
@Override
public String getPayType() {
return "custom";
}
@Override
public PayResult createPay(PayRequest request) {
// 1. 构建支付参数
// 2. 调用第三方支付接口
// 3. 返回支付结果
PayResult result = new PayResult();
result.setPayUrl("https://pay.example.com/...");
return result;
}
@Override
public boolean verifyNotify(Map<String, String> params) {
// 验证支付回调签名
return true;
}
@Override
public RefundResult refund(RefundRequest request) {
// 处理退款
return new RefundResult();
}
}2. 注册支付通道
java
@Configuration
public class PayConfig {
@Bean
public PayService customPayService() {
return new CustomPayServiceImpl();
}
}3. 配置支付参数
yaml
# application.yml
pay:
custom:
appId: your_app_id
appSecret: your_app_secret
notifyUrl: https://your-domain.com/api/pay/notify/custom4. 实现回调处理
java
@RestController
@RequestMapping("/api/pay/notify")
public class PayNotifyController {
@PostMapping("/custom")
public String customNotify(HttpServletRequest request) {
// 1. 解析回调参数
// 2. 验证签名
// 3. 更新订单状态
// 4. 返回成功响应
return "success";
}
}支付接口说明
PayRequest
| 字段 | 类型 | 说明 |
|---|---|---|
| orderNo | String | 订单号 |
| amount | BigDecimal | 支付金额 |
| subject | String | 商品标题 |
| body | String | 商品描述 |
| clientIp | String | 客户端IP |
PayResult
| 字段 | 类型 | 说明 |
|---|---|---|
| success | Boolean | 是否成功 |
| payUrl | String | 支付链接 |
| payParams | Map | 支付参数 |
| errorMsg | String | 错误信息 |
现有支付通道
| 通道 | 类 | 说明 |
|---|---|---|
| 微信支付 | WechatPayServiceImpl | 微信支付 |
| 支付宝 | AliPayServiceImpl | 支付宝支付 |
| 余额支付 | BalancePayServiceImpl | 余额支付 |
