ruby-on-rails 轨道7和条带:如何从控制器中的payment_intent. successful对象中检索webhooks控制器中的元数据

z9ju0rcb  于 2023-06-25  发布在  Ruby
关注(0)|答案(1)|浏览(131)

你可以在Payment.create行中看到,我有一个混合的语法,其中用于电子邮件值的语法包括花括号,我不知道我是如何得到的,我必须复制它,我把它留了下来,因为我的问题是,我如何从返回的event.data.object中获取元数据,在我的webhooks控制器中,以便Payment.create将创建一个新的记录与post_id?
checkouts_controller:

def create
      session = Stripe::Checkout::Session.create({
        payment_method_types: ['card'],
        line_items: [
          price_data: {
            product: 'prod_Miva1sJtJ29QeC',
            unit_amount: 500,
            currency: 'usd',
          },
          quantity: 1,
        ],
        metadata: {user_id: current_user.id, post_id: params[:post_id]},
        mode: 'payment',
        success_url: root_url(success: true),
        cancel_url: root_url,
      })

webhooks_controller:

when 'payment_intent.succeeded'
    session=event.data.object.payment_intent
    post_id = session.metadata['post_id']
    Payment.create(post_id: session.metadata["post_id"], time_stamp: Time.now.to_datetime.utc, email: "${session.email}")
    render json: { message: 'success' }
end
thigvfpy

thigvfpy1#

在请求中的Checkout Session对象上创建的metadata未填充到Payment Intent对象中,即您所描述的事件。
要在Payment Intent中显示元数据,在创建Checkout Session时,还应在请求中设置payment_intent_data.metadata。比如说

session = Stripe::Checkout::Session.create({
  line_items: [{
    price_data: {
      currency: 'usd',
      product_data: {
        name: 'T-shirt',
      },
      unit_amount: 2000,
    },
    quantity: 1,
  }],
  mode: 'payment',
  success_url: 'https://example.com/success',
  cancel_url: 'https://example.com/cancel',
  metadata: {
    user_id: 'user_123', 
    post_id: 'post_id_123'
  },
  payment_intent_data: {
    user_id: 'user_123',
    post_id: 'post_id_123',
  }
})

相关问题