|
| 1 | +package b2b |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + |
| 7 | + "github.com/ArtisanCloud/PowerLibs/v3/object" |
| 8 | + "github.com/ArtisanCloud/PowerWeChat/v3/src/kernel" |
| 9 | + "github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/b2b/request" |
| 10 | + "github.com/ArtisanCloud/PowerWeChat/v3/src/miniProgram/b2b/response" |
| 11 | +) |
| 12 | + |
| 13 | +type Client struct { |
| 14 | + BaseClient *kernel.BaseClient |
| 15 | + |
| 16 | + appKey string |
| 17 | + sandboxAppKey string |
| 18 | +} |
| 19 | + |
| 20 | +// CreatePayment 创建 B2b 支付 https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/B2b_store_assistant.html#_3-3-%E6%94%AF%E4%BB%98%E4%B8%8E%E9%80%80%E6%AC%BE |
| 21 | +func (c *Client) CreatePayment(sessionKey string, data any, overAppKey ...string) (*object.StringMap, error) { |
| 22 | + appKey := c.appKey |
| 23 | + if len(overAppKey) > 0 && overAppKey[0] != "" { |
| 24 | + appKey = overAppKey[0] |
| 25 | + } |
| 26 | + jsonData, err := json.Marshal(data) |
| 27 | + if err != nil { |
| 28 | + return nil, err |
| 29 | + } |
| 30 | + strData := string(jsonData) |
| 31 | + return &object.StringMap{ |
| 32 | + "sign_data": strData, |
| 33 | + "mode": "retail_pay_goods", |
| 34 | + "signature": kernel.CalcSignature(strData, sessionKey), |
| 35 | + "pay_sig": kernel.CalcSignature("requestCommonPayment"+"&"+strData, appKey), |
| 36 | + }, nil |
| 37 | +} |
| 38 | + |
| 39 | +func postJson[I, O any](ctx context.Context, c *Client, path string, in *I) (out *O, err error) { |
| 40 | + postBody, err := json.Marshal(in) |
| 41 | + if err != nil { |
| 42 | + return nil, err |
| 43 | + } |
| 44 | + signPost := string(postBody) |
| 45 | + |
| 46 | + paySign := kernel.CalcSignature(path+"&"+signPost, c.appKey) |
| 47 | + query := &object.StringMap{ |
| 48 | + "pay_sig": paySign, |
| 49 | + } |
| 50 | + out = new(O) |
| 51 | + _, err = c.BaseClient.HttpPostJson(ctx, path, signPost, query, nil, out) |
| 52 | + return out, err |
| 53 | +} |
| 54 | + |
| 55 | +// GetOrder 查询订单 https://developers.weixin.qq.com/miniprogram/dev/platform-capabilities/industry/B2b_store_assistant.html#_3-3-%E6%94%AF%E4%BB%98%E4%B8%8E%E9%80%80%E6%AC%BE |
| 56 | +func (c *Client) GetOrder(ctx context.Context, in *request.GetOrderReq) (*response.GetOrderRes, error) { |
| 57 | + return postJson[request.GetOrderReq, response.GetOrderRes](ctx, c, "/retail/B2b/getorder", in) |
| 58 | +} |
| 59 | + |
| 60 | +// Refund 退款 |
| 61 | +func (c *Client) Refund(ctx context.Context, in *request.RefundReq) (*response.RefundRes, error) { |
| 62 | + if in.AppKey != "" { |
| 63 | + c.appKey = in.AppKey |
| 64 | + } |
| 65 | + return postJson[request.RefundReq, response.RefundRes](ctx, c, "/retail/B2b/refund", in) |
| 66 | +} |
| 67 | + |
| 68 | +// GetRefund 查询退款 |
| 69 | +func (c *Client) GetRefund(ctx context.Context, in *request.GetRefundReq) (*response.GetRefundRes, error) { |
| 70 | + return postJson[request.GetRefundReq, response.GetRefundRes](ctx, c, "/retail/B2b/getrefund", in) |
| 71 | +} |
| 72 | + |
| 73 | +// AddProfitSharingAccount 添加分账方 |
| 74 | +func (c *Client) AddProfitSharingAccount(ctx context.Context, in *request.AddProfitSharingAccountReq) (*response.AddProfitSharingAccountRes, error) { |
| 75 | + return postJson[request.AddProfitSharingAccountReq, response.AddProfitSharingAccountRes](ctx, c, "/retail/B2b/addprofitsharingaccount", in) |
| 76 | +} |
| 77 | + |
| 78 | +// DelProfitSharingAccount 删除分账方 |
| 79 | +func (c *Client) DelProfitSharingAccount(ctx context.Context, in *request.DelProfitSharingAccountReq) (*response.DelProfitSharingAccountRes, error) { |
| 80 | + return postJson[request.DelProfitSharingAccountReq, response.DelProfitSharingAccountRes](ctx, c, "/retail/B2b/delprofitsharingaccount", in) |
| 81 | +} |
| 82 | + |
| 83 | +// QueryProfitSharingAccount 查询分账方 |
| 84 | +func (c *Client) QueryProfitSharingAccount(ctx context.Context, in *request.QueryProfitSharingAccountReq) (*response.QueryProfitSharingAccountRes, error) { |
| 85 | + return postJson[request.QueryProfitSharingAccountReq, response.QueryProfitSharingAccountRes](ctx, c, "/retail/B2b/queryprofitsharingaccount", in) |
| 86 | +} |
| 87 | + |
| 88 | +// CreateProfitSharingOrder 请求分账 |
| 89 | +func (c *Client) CreateProfitSharingOrder(ctx context.Context, in *request.CreateProfitSharingOrderReq) (*response.CreateProfitSharingOrderRes, error) { |
| 90 | + return postJson[request.CreateProfitSharingOrderReq, response.CreateProfitSharingOrderRes](ctx, c, "/retail/B2b/createprofitsharingorder", in) |
| 91 | +} |
| 92 | + |
| 93 | +// QueryProfitSharingOrder 查询分账结果 |
| 94 | +func (c *Client) QueryProfitSharingOrder(ctx context.Context, in *request.QueryProfitSharingOrderReq) (*response.QueryProfitSharingOrderRes, error) { |
| 95 | + return postJson[request.QueryProfitSharingOrderReq, response.QueryProfitSharingOrderRes](ctx, c, "/retail/B2b/queryprofitsharingorder?", in) |
| 96 | +} |
| 97 | + |
| 98 | +// QueryProfitSharingRemainAmt 查询分账剩余金额 |
| 99 | +func (c *Client) QueryProfitSharingRemainAmt(ctx context.Context, in *request.QueryProfitSharingRemainAmtReq) (*response.QueryProfitSharingRemainAmtRes, error) { |
| 100 | + return postJson[request.QueryProfitSharingRemainAmtReq, response.QueryProfitSharingRemainAmtRes](ctx, c, "/retail/B2b/queryprofitsharingremainamt", in) |
| 101 | +} |
| 102 | + |
| 103 | +// FinishProfitSharingOrder 完成分账 |
| 104 | +func (c *Client) FinishProfitSharingOrder(ctx context.Context, in *request.FinishProfitSharingOrderReq) (*response.FinishProfitSharingOrderRes, error) { |
| 105 | + return postJson[request.FinishProfitSharingOrderReq, response.FinishProfitSharingOrderRes](ctx, c, "/retail/B2b/finishprofitsharingorder", in) |
| 106 | +} |
| 107 | + |
| 108 | +// RefundProfitSharing 请求分账回退 |
| 109 | +func (c *Client) RefundProfitSharing(ctx context.Context, in *request.RefundProfitSharingReq) (*response.RefundProfitSharingRes, error) { |
| 110 | + return postJson[request.RefundProfitSharingReq, response.RefundProfitSharingRes](ctx, c, "/retail/B2b/refundprofitsharing", in) |
| 111 | +} |
| 112 | + |
| 113 | +// QueryRefundProfitSharingOrder 查询分账回退结果 |
| 114 | +func (c *Client) QueryRefundProfitSharingOrder(ctx context.Context, in *request.QueryRefundProfitSharingOrderReq) (*response.QueryRefundProfitSharingOrderRes, error) { |
| 115 | + return postJson[request.QueryRefundProfitSharingOrderReq, response.QueryRefundProfitSharingOrderRes](ctx, c, "/retail/B2b/queryrefundprofitsharingorder", in) |
| 116 | +} |
| 117 | + |
| 118 | +// DownloadBill 查看账单 |
| 119 | +func (c *Client) DownloadBill(ctx context.Context, in *request.DownloadBillReq) (*response.DownloadBillRes, error) { |
| 120 | + return postJson[request.DownloadBillReq, response.DownloadBillRes](ctx, c, "/retail/B2b/downloadbill", in) |
| 121 | +} |
0 commit comments