英文:
Paypal IPN and Golang integration on GAE
问题
我正在编写一个用于处理Paypal IPN消息和响应的监听器。
根据Paypal IPN的要求,监听器必须将从Paypal接收到的值按照相同的顺序回传,同时在值列表的前面插入一个新的参数“cmd=_notify-validate”。
>您的监听器通过HTTP POST将完整且未更改的消息发送回PayPal。
>注意:此消息必须包含与来自PayPal的原始IPN相同的字段,并且必须按照相同的顺序排列,所有字段前面都要加上cmd=_notify-validate。此外,此消息必须使用与原始消息相同的编码。
然而,Go的url.Values变量是使用映射数据结构实现的,当每次迭代时,值的顺序不能保证相同。
>>...在使用range循环迭代映射时,迭代顺序未指定,并且不能保证与下一次迭代相同"
而且,当调用url.Values的编码方法时,它会按键进行排序。
>>Encode方法按键进行排序,将值编码为“URL编码”形式(“bar=baz&foo=quux”)。
该监听器在GAE上运行,因此我使用“appengine/urlfetch”的PostForm函数,该函数将url.Values作为第二个参数。
c := appengine.NewContext(r)
client := urlfetch.Client(c)
resp, err := client.PostForm("https://www.sandbox.paypal.com/cgi-bin/webscr", r.Form)
由于url.Values是一个映射,其中值的顺序不能保证一致。我该如何使用GAE的urlfetch服务将从Paypal IPN接收到的参数值按照相同的顺序传递回Paypal呢?
英文:
I am writing a listener for handling Paypal IPN messages and responses.
From Paypal IPN requirement, The listener have to post the values received from Paypal back in the same order with a new parameter "cmd=_notify-validate" inserted at the front of the value list.
>Your listener HTTP POSTs the complete, unaltered message back to PayPal.
>Note: This message must contain the same fields, <b>in the same order</b>, as the original IPN from PayPal, all preceded by cmd=_notify-validate. Further, this message must use the same encoding as the original.
However, Go's url.Values variable is implemented in map data structure which order of the value is not guaranteed to be the same when being iterated each time.
>>...When iterating over a map with a range loop, the iteration order is not specified and is not guaranteed to be the same from one iteration to the next"
And when url.Values encoded method is called, it will be sorted by key
>>Encode encodes the values into “URL encoded” form ("bar=baz&foo=quux") sorted by key.
The listener is running on GAE thus I use "appengine/urlfetch"'s PostForm function which takes url.Values as the second parameter
c := appengine.NewContext(r)
client := urlfetch.Client(c)
resp, err := client.PostForm("https://www.sandbox.paypal.com/cgi-bin/webscr", r.Form)
As url.Values is a map, the order of values in the map are not guaranteed to be in order. How can I possibly pass the parameter values back in the same order received from Paypal IPN back to Paypal with GAE urlfetch service?
答案1
得分: 4
使用Post
而不是PostForm
。您可以从请求中获取请求体:
var buf bytes.Buffer
buf.WriteString("cmd=_notify-validate&")
io.Copy(&buf, r.Body)
client.Post("http://localhost", "application/x-www-form-urlencoded", &buf)
英文:
Use Post
instead of PostForm
. You can probably use the body from the request:
var buf bytes.Buffer
buf.WriteString("cmd=_notify-validate&")
io.Copy(&buf, r.Body)
client.Post("http://localhost", "application/x-www-form-urlencoded", &buf)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论