Shopify API:Shopify 中未显示退款

Shopify API: Refunds not showing in Shopify

我正在处理我的应用程序针对特定缺货产品的退款。 API 调用 post 退款似乎一直存在,但当我检查订单时,Shopify 中没有显示任何内容。

我在名为“refund_line_items”的数组中有要退款的行项目,我从中映射各个项目以进行调用。

这是我的代码:

refund_calculation = ShopifyAPI::Refund.calculate({
  currency: "USD",
  notify: false,
  note: "Out of stock",
  shipping: { 
    "full_refund": false 
  },  
  refund_line_items: refund_line_items.map { |li|
    {
      line_item_id: li.id,
      quantity: li.quantity,
      restock_type: 'no_restock',
    }
  },
}, { 
    params: { order_id: shopify_order.id },
})

order_refund = ShopifyAPI::Refund.new({
  currency: "USD",
  notify: false,
  note: "Out of stock",
  shipping: { 
    "full_refund": false 
  },
  refund_line_items: refund_line_items.map { |li|
    {
      line_item_id: li.id,
      quantity: li.quantity,
      restock_type: 'no_restock'
    }
  },
  transactions: refund_calculation.transactions.map { |tr|
    {
      kind: "refund",
      gateway: tr.gateway,
      parent_id: tr.parent_id,
      amount: tr.amount
    }
  },
}, {
  params: { order_id: shopify_order.id }
})

这里是 post 退款 API 调用之一的输出:

#<ShopifyAPI::Refund:0x0000561b935e4700
 @attributes=
  {"currency"=>"USD",
   "notify"=>false,
   "note"=>"Out of stock",
   "shipping"=>
    #<ShopifyAPI::Refund::Shipping:0x0000561b935e40c0
     @attributes={"full_refund"=>false},
     @persisted={:params=>{:order_id=>2528875905114}},
     @prefix_options={}>,
   "refund_line_items"=>
    [#<ShopifyAPI::Refund::RefundLineItem:0x0000561b935ef830
      @attributes=
       {"line_item_id"=>5596452192346,
        "quantity"=>1,
        "restock_type"=>"no_restock"},
      @persisted={:params=>{:order_id=>2528875905114}},
      @prefix_options={}>],
   "transactions"=>
    [#<ShopifyAPI::Transaction:0x0000561b935ef038
      @attributes=
       {"kind"=>"refund",
        "gateway"=>"shopify_payments",
        "parent_id"=>3161136562266,
        "amount"=>"11.72"},
      @persisted={:params=>{:order_id=>2528875905114}},
      @prefix_options={}>]},
 @persisted={:params=>{:order_id=>2528875905114}},
 @prefix_options={}>

从这个输出来看,退款似乎是持久化的,但经检查它实际上并没有持久化。我在这里做错了什么?谢谢

我发现了问题。

我必须将订单 ID 指定为 prefix_option 并明确保存记录。

像这样:

# Create refund on Shopify
order_refund = ShopifyAPI::Refund.new({
  currency: "USD",
  notify: false,
  note: "Out of stock",
  shipping: { 
    "full_refund": false 
  },
  refund_line_items: refund_line_items.map { |li|
    {
      line_item_id: li.id,
      quantity: li.quantity,
      restock_type: 'cancel'
    }
  },
  transactions: refund_calculation.transactions.map { |tr|
    {
      kind: "refund",
      gateway: tr.gateway,
      parent_id: tr.parent_id,
      amount: tr.amount
    }
  },
}, {
  params: { order_id: shopify_order.id }
})
order_refund.prefix_options = { order_id: shopify_order.id }
order_refund.save