Ruby on Rails如何建立付款模式来处理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 }]},

感谢您的任何帮助!

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

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"

最新更新