dsgnsdjk26586
dsgnsdjk26586
2015-10-19 22:31

转到Paypal REST API请求

已采纳

So Im currently using Go and Im trying to create a payment for Paypal I been trying this code

    payer := &Payer{"paypal"}
    amount := &Amount{"EUR", "12"}
    trans := &Transactions{amount, "A super test"}
    uris := &Redirect_urls{"http://localhost", "http://localhost"}
    p := &Payment{"sale", payer, trans, uris}
    response, err := json.Marshal(p)
    if err != nil {
        log.Println("Error at PaypalPayment - buy controller")
        log.Fatal(err)
    }
    log.Println(string(response))

    client := &http.Client{}
    buf := bytes.NewBuffer(response)
    req, err := http.NewRequest("POST", "https://api.sandbox.paypal.com/v1/payments/payment", buf)
    if err != nil {
        log.Println("Error at PaypalPayment - buy controller - 2")
        log.Fatal(err)
    }
    req.Header.Set("Content-Type", "application/json")
    req.Header.Set("Authorization", "Bearer " + token.Access_token)
    resp, err := client.Do(req)
    if err != nil {
        log.Println("Error at PaypalPayment - buy controller - 3")
        log.Fatal(err)
    }
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        log.Println("Error at PaypalPayment - buy controller - 4")
        log.Fatal(err)
    }
    log.Println(string(body))

I already got the access token, the problem is Im getting this error on the response body (last line)

MALFORMED_REQUEST

The request Im using is this (as of the println)

{
   "Intent":"sale",
   "Payer":{
      "Payment_method":"paypal"
   },
   "Transactions":{
      "Amount":{
         "Currency":"EUR",
         "Total":"12"
      },
      "Description":"Super test"
   },
   "Redirect_urls":{
      "Return_url":"http://localhost",
      "Cancel_url":"http://localhost"
   }
}

At my eyes seems a good request... no idea what im missing Error image

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dongzouhe9734 dongzouhe9734 6年前

    The problem was transactions needed to be an array. how blind I am

    Transactions []*Transactions `json:"transactions"`
    
    点赞 评论 复制链接分享
  • douxin5953 douxin5953 6年前

    As pointed out by @jcbwlkr you're casing doesn't match what is in the docs. If you don't have json tags on your types you'll have to add them. You have to keep the property names uppercase in Go because it's what marks the fields as being exported. If you're not familiar with this do a search for 'unexported vs exported fields golang'

    For example your Payment structs definition needs to look like this;

    type Payment struct {
         Amount *Amount `json:"amount"`
         Transactions *Transactions `json:"transactions"`
         RdUrls *Redirect_urls `json:"redirect_urls"`
    }
    

    Also, just fyi you can use nest those declarations where you declare the payment so you don't have to assign to local instances of Amount, Transactions and Redirect_urls in order to do the declaration.

    It's just like;

    p := &Payment{"sale", payer, &Transactions{amount, "A super test"}, uris}
    
    点赞 评论 复制链接分享

为你推荐