自动创建免费试用 Stripe 结账

问题描述 投票:0回答:1

我们使用 Stripe 进行结账,并启用免费试用。因此,当用户访问该网站启用免费试用时,他们只需在结帐中输入他们的电子邮件即可。

我想要的是将免费试用的开始与在我的网站上创建帐户联系起来。由于我收到了用户创建帐户的电子邮件,因此我想以编程方式创建免费试用版并删除该步骤。这可能吗?

这是我创建结帐会话的代码。

def create_checkout_session(request, app_type):
    subscription_plan_duration = SubscriptionPlanDuration.objects.filter().first()

    user_subscription = UserSubscription.objects.create(
                            subscription_plan_duration_id = subscription_plan_duration.id,
                            user_id = request.user.id,
                            app_type = app_type,
                            is_active = False #True - change for 3.
                        )
    
    discount_coupon = DiscountCoupon.objects.filter(subscription_plan_duration_id = subscription_plan_duration.id).first()

    checkout_session = stripe.checkout.Session.create(
        line_items=[
                {
                    'price': subscription_plan_duration.price_id,
                    'quantity': 1,
                },
            ],
        metadata={ 
            "user_subscription_id": user_subscription.id
        },
        mode="subscription",
        # discounts=[{
        #     'coupon': discount_coupon.coupon_id,
        # }],
        subscription_data={
            "trial_period_days": 30,
            "trial_settings": {
                "end_behavior": {
                    "missing_payment_method": "cancel"
                }
            }
        },
        payment_method_collection="if_required",
        allow_promotion_codes=True,
        success_url=settings.APP_END_POINT + '/stripe/success',
        cancel_url=settings.APP_END_POINT + '/stripe/cancel-subscription/' + app_type
    )

    return redirect(checkout_session.url)
python django stripe-payments
1个回答
0
投票

如果您不希望客户必须经历结帐会话,您可以通过 API 创建订阅:https://docs.stripe.com/api/subscriptions/create

请注意,您需要已经创建一个客户,因为它是 https://docs.stripe.com/api/subscriptions/create#create_subscription-customer

中的必填字段
© www.soinside.com 2019 - 2024. All rights reserved.