How to get charge id after creating a subscription using Stripe?

后端 未结 6 2338
忘掉有多难
忘掉有多难 2021-02-13 21:36

I am using Stripe as a payment gateway. Now there\'s a big problem bothers me.

I used code below to create a subscription:



        
6条回答
  •  春和景丽
    2021-02-13 21:56

    Well there is no straight-forward way to do this. There is however a hack to get charge_id for that subscription with out waiting for invoice.payment_succeeded callback.

    This how I did in Ruby, you can treat this as a pseudo code. May be you can do this using PHP APIs

      # Lets not retrieve all the invoices
      #  use filter
      lower_limit_date = DateTime.strptime(that_subscription.start.to_s, '%s') - 1.hour
      upper_limit_date = 2.hours.from_now
    
      list_object_of_all_invoices_in_range = Stripe::Invoice.all(
          {
              customer: customer_id,
              date: {
                  gt: lower_limit_date.to_i, # Start TimeStamp
                  lt: upper_limit_date.to_i # End TimeStamp
              }
          })
    
      particular_invoice = list_object_of_all_invoices_in_range.data.
          keep_if { |s| s[:subscription] == that_subscription.id }.first
    
      stripe_charge_id = particular_invoice.charge # gives charge_id
    

    See structure of ListObject for Invoices

提交回复
热议问题