蛋蛋星球-制度模式
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

237 linhas
6.8 KiB

  1. package lakala
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "github.com/go-pay/gopay"
  10. )
  11. // 获取当前汇率
  12. // 文档:https://payjp.lakala.com/docs/cn/#api-CommonApi-GetExchange
  13. func (c *Client) GetExchangeRate(ctx context.Context) (rsp *ExchangeRateRsp, err error) {
  14. url := fmt.Sprintf(getExchangeRate, c.PartnerCode)
  15. bs, err := c.doGet(ctx, url, "")
  16. if err != nil {
  17. return nil, err
  18. }
  19. rsp = new(ExchangeRateRsp)
  20. err = json.Unmarshal(bs, rsp)
  21. if err != nil {
  22. return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
  23. }
  24. return rsp, nil
  25. }
  26. // 获取加密密钥
  27. // 文档:https://payjp.lakala.com/docs/cn/#api-CommonApi-GetEncrypt
  28. func (c *Client) GetEncrypt(ctx context.Context) (rsp *EncryptRsp, err error) {
  29. url := fmt.Sprintf(getEncrypt, c.PartnerCode)
  30. bs, err := c.doGet(ctx, url, "")
  31. if err != nil {
  32. return nil, err
  33. }
  34. rsp = new(EncryptRsp)
  35. err = json.Unmarshal(bs, rsp)
  36. if err != nil {
  37. return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
  38. }
  39. return rsp, nil
  40. }
  41. // 关闭订单
  42. // 文档:https://payjp.lakala.com/docs/cn/#api-CommonApi-CloseOrder
  43. func (c *Client) CloseOrder(ctx context.Context, orderId string) (rsp *ErrorCode, err error) {
  44. url := fmt.Sprintf(closeOrder, c.PartnerCode, orderId)
  45. bs, err := c.doPut(ctx, url, nil)
  46. if err != nil {
  47. return nil, err
  48. }
  49. rsp = new(ErrorCode)
  50. err = json.Unmarshal(bs, rsp)
  51. if err != nil {
  52. return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
  53. }
  54. return rsp, nil
  55. }
  56. // 查询订单状态
  57. // 文档:https://payjp.lakala.com/docs/cn/#api-CommonApi-OrderStatus
  58. func (c *Client) OrderStatus(ctx context.Context, orderId string) (rsp *OrdersRsp, err error) {
  59. if orderId == gopay.NULL {
  60. return nil, fmt.Errorf("orderId is empty")
  61. }
  62. url := fmt.Sprintf(getOrderStatus, c.PartnerCode, orderId)
  63. bs, err := c.doGet(ctx, url, "")
  64. if err != nil {
  65. return nil, err
  66. }
  67. rsp = new(OrdersRsp)
  68. err = json.Unmarshal(bs, rsp)
  69. if err != nil {
  70. return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
  71. }
  72. return rsp, nil
  73. }
  74. // 申请退款
  75. // 文档:https://payjp.lakala.com/docs/cn/#api-CommonApi-RefundOrder
  76. func (c *Client) ApplyRefund(ctx context.Context, orderId string, refundId string, bm gopay.BodyMap) (rsp *RefundRsp, err error) {
  77. rsp = new(RefundRsp)
  78. if orderId == gopay.NULL {
  79. return nil, fmt.Errorf("orderId is empty")
  80. }
  81. if refundId == gopay.NULL {
  82. return nil, fmt.Errorf("refundId is empty")
  83. }
  84. if err = bm.CheckEmptyError("fee"); err != nil {
  85. return nil, err
  86. }
  87. url := fmt.Sprintf(applyRefund, c.PartnerCode, orderId, refundId)
  88. bs, err := c.doPut(ctx, url, bm)
  89. if err != nil {
  90. return nil, err
  91. }
  92. err = json.Unmarshal(bs, rsp)
  93. if err != nil {
  94. return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
  95. }
  96. return rsp, nil
  97. }
  98. // 查询退款状态
  99. // 文档:https://payjp.lakala.com/docs/cn/#api-CommonApi-RefundQuery
  100. func (c *Client) RefundQuery(ctx context.Context, orderId string, refundId string) (rsp *RefundRsp, err error) {
  101. if orderId == gopay.NULL {
  102. return nil, fmt.Errorf("orderId is empty")
  103. }
  104. if refundId == gopay.NULL {
  105. return nil, fmt.Errorf("refundId is empty")
  106. }
  107. url := fmt.Sprintf(getRefundStatus, c.PartnerCode, orderId, refundId)
  108. bs, err := c.doGet(ctx, url, "")
  109. if err != nil {
  110. return nil, err
  111. }
  112. rsp = new(RefundRsp)
  113. err = json.Unmarshal(bs, rsp)
  114. if err != nil {
  115. return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
  116. }
  117. return rsp, nil
  118. }
  119. // 查看订单
  120. // 文档:https://payjp.lakala.com/docs/cn/#api-CommonApi-ListOrder
  121. func (c *Client) OrderList(ctx context.Context, date, status string, page, limit int) (rsp *OrderListRsp, err error) {
  122. bm := make(gopay.BodyMap)
  123. bm.Set("date", date).Set("status", status).Set("page", page).Set("limit", limit)
  124. url := fmt.Sprintf(queryOrderList, c.PartnerCode)
  125. bs, err := c.doGet(ctx, url, bm.EncodeURLParams())
  126. if err != nil {
  127. return nil, err
  128. }
  129. rsp = new(OrderListRsp)
  130. err = json.Unmarshal(bs, rsp)
  131. if err != nil {
  132. return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
  133. }
  134. return rsp, nil
  135. }
  136. // 查看账单流水
  137. // 文档:https://payjp.lakala.com/docs/cn/#api-CommonApi-ListTransaction
  138. func (c *Client) TransactionList(ctx context.Context, date string) (rsp *TransactionListRsp, err error) {
  139. bm := make(gopay.BodyMap)
  140. bm.Set("date", date)
  141. url := fmt.Sprintf(queryTransactionList, c.PartnerCode)
  142. bs, err := c.doGet(ctx, url, bm.EncodeURLParams())
  143. if err != nil {
  144. return nil, err
  145. }
  146. rsp = new(TransactionListRsp)
  147. err = json.Unmarshal(bs, rsp)
  148. if err != nil {
  149. return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
  150. }
  151. return rsp, nil
  152. }
  153. // 查看清算详情
  154. // 文档:https://payjp.lakala.com/docs/cn/#api-CommonApi-SettleLog
  155. func (c *Client) Settlements(ctx context.Context, date string) (rsp *SettlementsRsp, err error) {
  156. bm := make(gopay.BodyMap)
  157. bm.Set("date", date)
  158. url := fmt.Sprintf(querySettlements, c.PartnerCode)
  159. bs, err := c.doGet(ctx, url, bm.EncodeURLParams())
  160. if err != nil {
  161. return nil, err
  162. }
  163. rsp = new(SettlementsRsp)
  164. err = json.Unmarshal(bs, rsp)
  165. if err != nil {
  166. return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
  167. }
  168. return rsp, nil
  169. }
  170. // 查询可用钱包
  171. // 文档:https://payjp.lakala.com/docs/cn/#api-CommonApi-ConsultPayment
  172. func (c *Client) ConsultPayment(ctx context.Context, bm gopay.BodyMap) (rsp *ConsultPaymentRsp, err error) {
  173. if err = bm.CheckEmptyError("currency", "amount", "terminal_type"); err != nil {
  174. return nil, err
  175. }
  176. url := fmt.Sprintf(queryConsultPayment, c.PartnerCode)
  177. bs, err := c.doPost(ctx, url, bm)
  178. if err != nil {
  179. return nil, err
  180. }
  181. rsp = new(ConsultPaymentRsp)
  182. err = json.Unmarshal(bs, rsp)
  183. if err != nil {
  184. return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
  185. }
  186. return rsp, nil
  187. }
  188. // 获取优惠券信息
  189. // 文档:https://payjp.lakala.com/docs/cn/#api-CommonApi-GetCoupon
  190. func (c *Client) GetCoupon(ctx context.Context, couponId string) (rsp *GetCouponRsp, err error) {
  191. url := fmt.Sprintf(getCoupon, c.PartnerCode, couponId)
  192. bs, err := c.doGet(ctx, url, "")
  193. if err != nil {
  194. return nil, err
  195. }
  196. rsp = new(GetCouponRsp)
  197. err = json.Unmarshal(bs, rsp)
  198. if err != nil {
  199. return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(bs))
  200. }
  201. return rsp, nil
  202. }
  203. // 付款通知
  204. // 文档:https://payjp.lakala.com/docs/cn/#api-CommonApi-PayNotice
  205. func ParseNotify(req *http.Request) (notifyReq *NotifyRequest, err error) {
  206. var buf bytes.Buffer
  207. if _, err = buf.ReadFrom(req.Body); err != nil {
  208. return nil, err
  209. }
  210. if err = req.Body.Close(); err != nil {
  211. return nil, err
  212. }
  213. body := buf.Bytes()
  214. req.Body = io.NopCloser(bytes.NewReader(body))
  215. notifyReq = new(NotifyRequest)
  216. err = json.Unmarshal(body, notifyReq)
  217. if err != nil {
  218. return nil, fmt.Errorf("[%w], bytes: %s", gopay.UnmarshalErr, string(body))
  219. }
  220. return
  221. }