繁体   English   中英

Ruby on Rails如何建立付款模型以处理Paypal REST SDK

[英]Ruby on Rails How to build a Payment model to deal with Paypal REST SDK

我想根据Github上的官方Paypal示例创建一个付款模型。 但是我一直在用所需字段创建模型。

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." }]})

rails g model Payment intent:string ...我不知道如何创建嵌套字段,例如

: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 }]},

谢谢你的帮助!

您可以使用OpenStruct为您执行此操作。 将会是这样的:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM