topup_creem.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. package controller
  2. import (
  3. "bytes"
  4. "crypto/hmac"
  5. "crypto/sha256"
  6. "encoding/hex"
  7. "encoding/json"
  8. "errors"
  9. "fmt"
  10. "github.com/QuantumNous/new-api/common"
  11. "github.com/QuantumNous/new-api/model"
  12. "github.com/QuantumNous/new-api/setting"
  13. "io"
  14. "log"
  15. "net/http"
  16. "time"
  17. "github.com/gin-gonic/gin"
  18. "github.com/thanhpk/randstr"
  19. )
  20. const (
  21. PaymentMethodCreem = "creem"
  22. CreemSignatureHeader = "creem-signature"
  23. )
  24. var creemAdaptor = &CreemAdaptor{}
  25. // 生成HMAC-SHA256签名
  26. func generateCreemSignature(payload string, secret string) string {
  27. h := hmac.New(sha256.New, []byte(secret))
  28. h.Write([]byte(payload))
  29. return hex.EncodeToString(h.Sum(nil))
  30. }
  31. // 验证Creem webhook签名
  32. func verifyCreemSignature(payload string, signature string, secret string) bool {
  33. if secret == "" {
  34. log.Printf("Creem webhook secret not set")
  35. if setting.CreemTestMode {
  36. log.Printf("Skip Creem webhook sign verify in test mode")
  37. return true
  38. }
  39. return false
  40. }
  41. expectedSignature := generateCreemSignature(payload, secret)
  42. return hmac.Equal([]byte(signature), []byte(expectedSignature))
  43. }
  44. type CreemPayRequest struct {
  45. ProductId string `json:"product_id"`
  46. PaymentMethod string `json:"payment_method"`
  47. }
  48. type CreemProduct struct {
  49. ProductId string `json:"productId"`
  50. Name string `json:"name"`
  51. Price float64 `json:"price"`
  52. Currency string `json:"currency"`
  53. Quota int64 `json:"quota"`
  54. }
  55. type CreemAdaptor struct {
  56. }
  57. func (*CreemAdaptor) RequestPay(c *gin.Context, req *CreemPayRequest) {
  58. if req.PaymentMethod != PaymentMethodCreem {
  59. c.JSON(200, gin.H{"message": "error", "data": "不支持的支付渠道"})
  60. return
  61. }
  62. if req.ProductId == "" {
  63. c.JSON(200, gin.H{"message": "error", "data": "请选择产品"})
  64. return
  65. }
  66. // 解析产品列表
  67. var products []CreemProduct
  68. err := json.Unmarshal([]byte(setting.CreemProducts), &products)
  69. if err != nil {
  70. log.Println("解析Creem产品列表失败", err)
  71. c.JSON(200, gin.H{"message": "error", "data": "产品配置错误"})
  72. return
  73. }
  74. // 查找对应的产品
  75. var selectedProduct *CreemProduct
  76. for _, product := range products {
  77. if product.ProductId == req.ProductId {
  78. selectedProduct = &product
  79. break
  80. }
  81. }
  82. if selectedProduct == nil {
  83. c.JSON(200, gin.H{"message": "error", "data": "产品不存在"})
  84. return
  85. }
  86. id := c.GetInt("id")
  87. user, _ := model.GetUserById(id, false)
  88. // 生成唯一的订单引用ID
  89. reference := fmt.Sprintf("creem-api-ref-%d-%d-%s", user.Id, time.Now().UnixMilli(), randstr.String(4))
  90. referenceId := "ref_" + common.Sha1([]byte(reference))
  91. // 先创建订单记录,使用产品配置的金额和充值额度
  92. topUp := &model.TopUp{
  93. UserId: id,
  94. Amount: selectedProduct.Quota, // 充值额度
  95. Money: selectedProduct.Price, // 支付金额
  96. TradeNo: referenceId,
  97. CreateTime: time.Now().Unix(),
  98. Status: common.TopUpStatusPending,
  99. }
  100. err = topUp.Insert()
  101. if err != nil {
  102. log.Printf("创建Creem订单失败: %v", err)
  103. c.JSON(200, gin.H{"message": "error", "data": "创建订单失败"})
  104. return
  105. }
  106. // 创建支付链接,传入用户邮箱
  107. checkoutUrl, err := genCreemLink(referenceId, selectedProduct, user.Email, user.Username)
  108. if err != nil {
  109. log.Printf("获取Creem支付链接失败: %v", err)
  110. c.JSON(200, gin.H{"message": "error", "data": "拉起支付失败"})
  111. return
  112. }
  113. log.Printf("Creem订单创建成功 - 用户ID: %d, 订单号: %s, 产品: %s, 充值额度: %d, 支付金额: %.2f",
  114. id, referenceId, selectedProduct.Name, selectedProduct.Quota, selectedProduct.Price)
  115. c.JSON(200, gin.H{
  116. "message": "success",
  117. "data": gin.H{
  118. "checkout_url": checkoutUrl,
  119. "order_id": referenceId,
  120. },
  121. })
  122. }
  123. func RequestCreemPay(c *gin.Context) {
  124. var req CreemPayRequest
  125. // 读取body内容用于打印,同时保留原始数据供后续使用
  126. bodyBytes, err := io.ReadAll(c.Request.Body)
  127. if err != nil {
  128. log.Printf("read creem pay req body err: %v", err)
  129. c.JSON(200, gin.H{"message": "error", "data": "read query error"})
  130. return
  131. }
  132. // 打印body内容
  133. log.Printf("creem pay request body: %s", string(bodyBytes))
  134. // 重新设置body供后续的ShouldBindJSON使用
  135. c.Request.Body = io.NopCloser(bytes.NewReader(bodyBytes))
  136. err = c.ShouldBindJSON(&req)
  137. if err != nil {
  138. c.JSON(200, gin.H{"message": "error", "data": "参数错误"})
  139. return
  140. }
  141. creemAdaptor.RequestPay(c, &req)
  142. }
  143. // 新的Creem Webhook结构体,匹配实际的webhook数据格式
  144. type CreemWebhookEvent struct {
  145. Id string `json:"id"`
  146. EventType string `json:"eventType"`
  147. CreatedAt int64 `json:"created_at"`
  148. Object struct {
  149. Id string `json:"id"`
  150. Object string `json:"object"`
  151. RequestId string `json:"request_id"`
  152. Order struct {
  153. Object string `json:"object"`
  154. Id string `json:"id"`
  155. Customer string `json:"customer"`
  156. Product string `json:"product"`
  157. Amount int `json:"amount"`
  158. Currency string `json:"currency"`
  159. SubTotal int `json:"sub_total"`
  160. TaxAmount int `json:"tax_amount"`
  161. AmountDue int `json:"amount_due"`
  162. AmountPaid int `json:"amount_paid"`
  163. Status string `json:"status"`
  164. Type string `json:"type"`
  165. Transaction string `json:"transaction"`
  166. CreatedAt string `json:"created_at"`
  167. UpdatedAt string `json:"updated_at"`
  168. Mode string `json:"mode"`
  169. } `json:"order"`
  170. Product struct {
  171. Id string `json:"id"`
  172. Object string `json:"object"`
  173. Name string `json:"name"`
  174. Description string `json:"description"`
  175. Price int `json:"price"`
  176. Currency string `json:"currency"`
  177. BillingType string `json:"billing_type"`
  178. BillingPeriod string `json:"billing_period"`
  179. Status string `json:"status"`
  180. TaxMode string `json:"tax_mode"`
  181. TaxCategory string `json:"tax_category"`
  182. DefaultSuccessUrl *string `json:"default_success_url"`
  183. CreatedAt string `json:"created_at"`
  184. UpdatedAt string `json:"updated_at"`
  185. Mode string `json:"mode"`
  186. } `json:"product"`
  187. Units int `json:"units"`
  188. Customer struct {
  189. Id string `json:"id"`
  190. Object string `json:"object"`
  191. Email string `json:"email"`
  192. Name string `json:"name"`
  193. Country string `json:"country"`
  194. CreatedAt string `json:"created_at"`
  195. UpdatedAt string `json:"updated_at"`
  196. Mode string `json:"mode"`
  197. } `json:"customer"`
  198. Status string `json:"status"`
  199. Metadata map[string]string `json:"metadata"`
  200. Mode string `json:"mode"`
  201. } `json:"object"`
  202. }
  203. func CreemWebhook(c *gin.Context) {
  204. // 读取body内容用于打印,同时保留原始数据供后续使用
  205. bodyBytes, err := io.ReadAll(c.Request.Body)
  206. if err != nil {
  207. log.Printf("读取Creem Webhook请求body失败: %v", err)
  208. c.AbortWithStatus(http.StatusBadRequest)
  209. return
  210. }
  211. // 获取签名头
  212. signature := c.GetHeader(CreemSignatureHeader)
  213. // 打印关键信息(避免输出完整敏感payload)
  214. log.Printf("Creem Webhook - URI: %s", c.Request.RequestURI)
  215. if setting.CreemTestMode {
  216. log.Printf("Creem Webhook - Signature: %s , Body: %s", signature, bodyBytes)
  217. } else if signature == "" {
  218. log.Printf("Creem Webhook缺少签名头")
  219. c.AbortWithStatus(http.StatusUnauthorized)
  220. return
  221. }
  222. // 验证签名
  223. if !verifyCreemSignature(string(bodyBytes), signature, setting.CreemWebhookSecret) {
  224. log.Printf("Creem Webhook签名验证失败")
  225. c.AbortWithStatus(http.StatusUnauthorized)
  226. return
  227. }
  228. log.Printf("Creem Webhook签名验证成功")
  229. // 重新设置body供后续的ShouldBindJSON使用
  230. c.Request.Body = io.NopCloser(bytes.NewReader(bodyBytes))
  231. // 解析新格式的webhook数据
  232. var webhookEvent CreemWebhookEvent
  233. if err := c.ShouldBindJSON(&webhookEvent); err != nil {
  234. log.Printf("解析Creem Webhook参数失败: %v", err)
  235. c.AbortWithStatus(http.StatusBadRequest)
  236. return
  237. }
  238. log.Printf("Creem Webhook解析成功 - EventType: %s, EventId: %s", webhookEvent.EventType, webhookEvent.Id)
  239. // 根据事件类型处理不同的webhook
  240. switch webhookEvent.EventType {
  241. case "checkout.completed":
  242. handleCheckoutCompleted(c, &webhookEvent)
  243. default:
  244. log.Printf("忽略Creem Webhook事件类型: %s", webhookEvent.EventType)
  245. c.Status(http.StatusOK)
  246. }
  247. }
  248. // 处理支付完成事件
  249. func handleCheckoutCompleted(c *gin.Context, event *CreemWebhookEvent) {
  250. // 验证订单状态
  251. if event.Object.Order.Status != "paid" {
  252. log.Printf("订单状态不是已支付: %s, 跳过处理", event.Object.Order.Status)
  253. c.Status(http.StatusOK)
  254. return
  255. }
  256. // 获取引用ID(这是我们创建订单时传递的request_id)
  257. referenceId := event.Object.RequestId
  258. if referenceId == "" {
  259. log.Println("Creem Webhook缺少request_id字段")
  260. c.AbortWithStatus(http.StatusBadRequest)
  261. return
  262. }
  263. // Try complete subscription order first
  264. LockOrder(referenceId)
  265. defer UnlockOrder(referenceId)
  266. if err := model.CompleteSubscriptionOrder(referenceId, common.GetJsonString(event)); err == nil {
  267. c.Status(http.StatusOK)
  268. return
  269. } else if err != nil && !errors.Is(err, model.ErrSubscriptionOrderNotFound) {
  270. log.Printf("Creem订阅订单处理失败: %s, 订单号: %s", err.Error(), referenceId)
  271. c.AbortWithStatus(http.StatusInternalServerError)
  272. return
  273. }
  274. // 验证订单类型,目前只处理一次性付款(充值)
  275. if event.Object.Order.Type != "onetime" {
  276. log.Printf("暂不支持的订单类型: %s, 跳过处理", event.Object.Order.Type)
  277. c.Status(http.StatusOK)
  278. return
  279. }
  280. // 记录详细的支付信息
  281. log.Printf("处理Creem支付完成 - 订单号: %s, Creem订单ID: %s, 支付金额: %d %s, 客户邮箱: <redacted>, 产品: %s",
  282. referenceId,
  283. event.Object.Order.Id,
  284. event.Object.Order.AmountPaid,
  285. event.Object.Order.Currency,
  286. event.Object.Product.Name)
  287. // 查询本地订单确认存在
  288. topUp := model.GetTopUpByTradeNo(referenceId)
  289. if topUp == nil {
  290. log.Printf("Creem充值订单不存在: %s", referenceId)
  291. c.AbortWithStatus(http.StatusBadRequest)
  292. return
  293. }
  294. if topUp.Status != common.TopUpStatusPending {
  295. log.Printf("Creem充值订单状态错误: %s, 当前状态: %s", referenceId, topUp.Status)
  296. c.Status(http.StatusOK) // 已处理过的订单,返回成功避免重复处理
  297. return
  298. }
  299. // 处理充值,传入客户邮箱和姓名信息
  300. customerEmail := event.Object.Customer.Email
  301. customerName := event.Object.Customer.Name
  302. // 防护性检查,确保邮箱和姓名不为空字符串
  303. if customerEmail == "" {
  304. log.Printf("警告:Creem回调中客户邮箱为空 - 订单号: %s", referenceId)
  305. }
  306. if customerName == "" {
  307. log.Printf("警告:Creem回调中客户姓名为空 - 订单号: %s", referenceId)
  308. }
  309. err := model.RechargeCreem(referenceId, customerEmail, customerName)
  310. if err != nil {
  311. log.Printf("Creem充值处理失败: %s, 订单号: %s", err.Error(), referenceId)
  312. c.AbortWithStatus(http.StatusInternalServerError)
  313. return
  314. }
  315. log.Printf("Creem充值成功 - 订单号: %s, 充值额度: %d, 支付金额: %.2f",
  316. referenceId, topUp.Amount, topUp.Money)
  317. c.Status(http.StatusOK)
  318. }
  319. type CreemCheckoutRequest struct {
  320. ProductId string `json:"product_id"`
  321. RequestId string `json:"request_id"`
  322. Customer struct {
  323. Email string `json:"email"`
  324. } `json:"customer"`
  325. Metadata map[string]string `json:"metadata,omitempty"`
  326. }
  327. type CreemCheckoutResponse struct {
  328. CheckoutUrl string `json:"checkout_url"`
  329. Id string `json:"id"`
  330. }
  331. func genCreemLink(referenceId string, product *CreemProduct, email string, username string) (string, error) {
  332. if setting.CreemApiKey == "" {
  333. return "", fmt.Errorf("未配置Creem API密钥")
  334. }
  335. // 根据测试模式选择 API 端点
  336. apiUrl := "https://api.creem.io/v1/checkouts"
  337. if setting.CreemTestMode {
  338. apiUrl = "https://test-api.creem.io/v1/checkouts"
  339. log.Printf("使用Creem测试环境: %s", apiUrl)
  340. }
  341. // 构建请求数据,确保包含用户邮箱
  342. requestData := CreemCheckoutRequest{
  343. ProductId: product.ProductId,
  344. RequestId: referenceId, // 这个作为订单ID传递给Creem
  345. Customer: struct {
  346. Email string `json:"email"`
  347. }{
  348. Email: email, // 用户邮箱会在支付页面预填充
  349. },
  350. Metadata: map[string]string{
  351. "username": username,
  352. "reference_id": referenceId,
  353. "product_name": product.Name,
  354. "quota": fmt.Sprintf("%d", product.Quota),
  355. },
  356. }
  357. // 序列化请求数据
  358. jsonData, err := json.Marshal(requestData)
  359. if err != nil {
  360. return "", fmt.Errorf("序列化请求数据失败: %v", err)
  361. }
  362. // 创建 HTTP 请求
  363. req, err := http.NewRequest("POST", apiUrl, bytes.NewBuffer(jsonData))
  364. if err != nil {
  365. return "", fmt.Errorf("创建HTTP请求失败: %v", err)
  366. }
  367. // 设置请求头
  368. req.Header.Set("Content-Type", "application/json")
  369. req.Header.Set("x-api-key", setting.CreemApiKey)
  370. log.Printf("发送Creem支付请求 - URL: %s, 产品ID: %s, 用户邮箱: %s, 订单号: %s",
  371. apiUrl, product.ProductId, email, referenceId)
  372. // 发送请求
  373. client := &http.Client{
  374. Timeout: 30 * time.Second,
  375. }
  376. resp, err := client.Do(req)
  377. if err != nil {
  378. return "", fmt.Errorf("发送HTTP请求失败: %v", err)
  379. }
  380. defer resp.Body.Close()
  381. // 读取响应
  382. body, err := io.ReadAll(resp.Body)
  383. if err != nil {
  384. return "", fmt.Errorf("读取响应失败: %v", err)
  385. }
  386. log.Printf("Creem API resp - status code: %d, resp: %s", resp.StatusCode, string(body))
  387. // 检查响应状态
  388. if resp.StatusCode/100 != 2 {
  389. return "", fmt.Errorf("Creem API http status %d ", resp.StatusCode)
  390. }
  391. // 解析响应
  392. var checkoutResp CreemCheckoutResponse
  393. err = json.Unmarshal(body, &checkoutResp)
  394. if err != nil {
  395. return "", fmt.Errorf("解析响应失败: %v", err)
  396. }
  397. if checkoutResp.CheckoutUrl == "" {
  398. return "", fmt.Errorf("Creem API resp no checkout url ")
  399. }
  400. log.Printf("Creem 支付链接创建成功 - 订单号: %s, 支付链接: %s", referenceId, checkoutResp.CheckoutUrl)
  401. return checkoutResp.CheckoutUrl, nil
  402. }