首页 > 解决方案 > Razorpay 集成错误(调用 client.Order.Create 时参数不足)

问题描述

尝试实现,基于razorpay网站 代码中的官方文档:

 11 func indexpageGet(c *gin.Context) {
 12
 13         client := razorpay.NewClient("rzp_test_zlsYshjhjhjln", "9LtUy4qhghghgh2asp59es")
 14
 15         data := map[string]interface{}{
 16                 "amount":          1234,
 17                 "currency":        "INR",
 18                 "receipt_id":      "some_receipt_id",
 19         }
 20         body, err := client.Order.Create(data)
 21         if err != nil {
 22                 fmt.Println(err)
 23         }
 24
 25         fmt.Println(body)

编译错误:

./main.go:20:34: not enough arguments in call to client.Order.Create
        have (map[string]interface {})
        want (map[string]interface {}, map[string]string)

我的观察:函数client.Order.Create(data)需要两个参数我从 razorpay 库中确认了它(参见函数定义部分)。所以解决方案是我也需要传递第二个参数(extraHeaders)。

// Create creates a new order for the given data
func (order *Order) Create(data map[string]interface{}, extraHeaders map[string]string) (map[string]interface{}, error) {
    return order.Request.Post(constants.ORDER_URL, data, extraHeaders)
} 

我被卡住的一点是什么是extraHeaders我需要传递的价值?

标签: apigorazorpay

解决方案


根据问题中的评论,做了两件事使它起作用:(请参阅下面代码中的评论)

 11 func indexpageGet(c *gin.Context) {
 12
 13         client := razorpay.NewClient("rzp_test_zlsYsrvuUxxhln", "9LtUy4qpLCOtl4Gz2asp59es")
 14
 15         data := map[string]interface{}{
 16                 "amount":          1234,
 17                 "currency":        "INR",
 18                 "receipt":              "110",
 19                 //"receipt_id":      "some_receipt_id", // 1st Mistake in documentation it should be 'receipt', corrected in above line
 20         }
 21         
 22         extraHeader := make(map[string]string) // 2nd Mistake in documentation , corrected by creating an empty map and passed as the second argument in below function.
 23         body, err := client.Order.Create(data,extraHeader)
 24         if err != nil {
 25                 fmt.Println(err)
 26         }
 27
 28         fmt.Println(body)

推荐阅读