[英]Ruby on Rails How to build a Payment model to deal with Paypal REST SDK
I'd like to create a Payment model along the official Paypal Example on Github . 我想根据Github上的官方Paypal示例创建一个付款模型。 But I'm stuck in the creating of the model with the desired fields. 但是我一直在用所需字段创建模型。
Payment.new({
:intent => "sale",
:payer => {
:payment_method => "paypal" },
:redirect_urls => {
:return_url => "http://localhost:3000/payment/execute",
:cancel_url => "http://localhost:3000/" },
:transactions => [{
:item_list => {
:items => [{
:name => "item",
:sku => "item",
:price => "5",
:currency => "USD",
:quantity => 1 }]},
:amount => {
:total => "5",
:currency => "USD" },
:description => "This is the payment transaction description." }]})
Starting with rails g model Payment intent:string
... I don't know how to create the nested fields like 从rails g model Payment intent:string
...我不知道如何创建嵌套字段,例如
:redirect_urls => {
:return_url => "http://localhost:3000/payment/execute",
:cancel_url => "http://localhost:3000/" }
and more deeper 更深
:transactions => [{
:item_list => {
:items => [{
:name => "item",
:sku => "item",
:price => "5",
:currency => "USD",
:quantity => 1 }]},
Thanks for any help! 谢谢你的帮助!
You can use OpenStruct to do this for you. 您可以使用OpenStruct为您执行此操作。 It will be something like this : 将会是这样的:
paypal_hash = {
:intent => "sale",
:payer => {
:payment_method => "paypal" },
:redirect_urls => {
:return_url => "http://localhost:3000/payment/execute",
:cancel_url => "http://localhost:3000/" },
:transactions => [{
:item_list => {
:items => [{
:name => "item",
:sku => "item",
:price => "5",
:currency => "USD",
:quantity => 1 }]},
:amount => {
:total => "5",
:currency => "USD" },
:description => "This is the payment transaction description." }]}
paypal_obj = OpenStruct.new(paypal_hash)
paypal_obj.intent
# => "sales"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.