appota/README.MD
2025-09-09 09:27:43 +08:00

118 lines
3.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Linkpay Grpc Service
## Introduction
This is the grpc service for Linkpay.
## Directory Structure
```
├── api //api预留
├── config //配置文件
│ └── config.toml
├── internal //应用实现
│ ├── cmd
│ │ └── cmd.go
│ ├── consts //常量/结构定义
│ │ └── consts.go
│ ├── controller //控制层
│ │ └── api
│ │ └── api.go
│ ├── logic //逻辑层(主要业务代码实现)
│ │ ├── event.go //回调逻辑实现
│ │ ├── pay.go //支付逻辑实现
│ │ ├── ping.go //碰撞测试
│ │ ├── query.go //查询逻辑实现
│ │ └── refund.go //退款逻辑实现
│ │ └── revoke.go //撤销逻辑实现
│ ├── packed //三方包
│ │ ├── packed.go
│ │ └── unarymeta.go
│ └── service //服务层
│ ├── grpc.go
│ └── request.go
├── main.go //入口
```
### 启动项目
```
go get -u && go run .
```
### 调试说明
下载postman工具 https://www.postman.com/downloads/
postman调试操作
- 新建grpc接口。
- 在url中输入127.0.0.1:9901 即可
- 在logic逻辑层实现各接口能力。
### 接口要求实现
- 回调 event 需要返回回调结果
- 碰撞测试 ping 需要返回pong/或当前时间戳
- 创建支付 pay 需要返回支付订单信息
- 查询支付 query 需要返回支付/退单的订单的状态信息
- 退款 refund 需要返回退款订单信息
- 撤销 revoke 需要返回撤销订单信息
### 回调说明
- 回调接口需要返回回调结果,包括成功或失败,及相关信息。
- 入口数据, req *protobuf.CallbackReq, 其中包含第三方的URL、Header、Body等信息。
- 返回数据, res *protobuf.Orders, 其中包含订单状态、订单号、订单金额等信息。尽可能的满足字段信息都需要返回。
其中Response字段为回调接口返回的原始数据方便业务层处理日志。
```go
func Callback(ctx context.Context, req *protobuf.CallbackReq) (res *protobuf.Orders, err error) {
res = &protobuf.Orders{
Kid: req.Kid, //商户ID
Uid: req.Uid, //用户ID
Org: req.Org, //商户名称
Via: req.Via, //支付渠道
OrgNo: "12345", //渠道订单号
OrderNo: "67890", //商户订单号
Response: string(req.GetBody()), //解签后的Body数据
State: -1, //订单状态 (参考 protobuf.State 枚举)
StateText: "支付失败", //订单状态原因,状态说明
}
return
}
```
### 通用API请求方法
service/request.go
```go
//请求API
r, err := service.Request(ctx).Header("Authorization", "{Token}").Post("/v1/pay/create", req)
//返回支付结果
res = &protobuf.Orders{
Response: r.ReadAllString(),
}
```
该方法封装了请求的通用方法,包括:
- Header设置请求头
- Post发起post请求
- Get发起get请求
其中多个Header头示例
```go
r, err := service.Request(ctx).Header("x-api-key", "{Token}").Header("x-api-secret", "{Secret}").Post("/v1/pay/create", req)
```
r 方便获取各类HTTP响应信息,数据。
### 日志说明
请严格遵守Goframe日志规范打印
如:
```go
g.Log("业务模块").Info(ctx, "this is a info log")
g.Log("pay").Error(ctx, "this is a pay error log")
```