去PayPal REST API请求

huangapple go评论73阅读模式
英文:

Go paypal REST API request

问题

所以我目前正在使用Go语言,尝试为PayPal创建一个支付功能。我一直在尝试以下代码:

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))

我已经获得了访问令牌,问题是我在响应体(最后一行)中得到了这个错误:

MALFORMED_REQUEST

我正在使用的请求如下(根据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"
   }
}

在我看来,这是一个正确的请求...不知道我漏掉了什么。

英文:

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
去PayPal REST API请求

答案1

得分: 2

如@jcbwlkr所指出的,你的大小写不符合文档中的要求。如果你的类型没有json标签,你需要添加它们。在Go语言中,属性名称必须大写,因为这标志着字段被导出。如果你对此不熟悉,请搜索“unexported vs exported fields golang”。

例如,你的Payment结构定义应该像这样:

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

另外,你可以在声明支付时嵌套这些声明,这样就不需要为了声明而分配AmountTransactionsRedirect_urls的本地实例。

就像这样:

p := &Payment{"sale", payer, &Transactions{amount, "A super test"}, uris}
英文:

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}

答案2

得分: 0

问题是交易需要是一个数组。我是多么盲目啊。

Transactions []*Transactions `json:"transactions"`
英文:

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

Transactions []*Transactions `json:"transactions"`

huangapple
  • 本文由 发表于 2015年10月20日 06:31:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/33225037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定