簡體   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