用烧瓶获取PayPal服务器端订阅

发布于 2025-01-28 11:15:33 字数 1969 浏览 4 评论 0原文

我想拥有一个简单的解决方案来在烧瓶应用程序中付款后获得订阅ID。

PayPal仅为客户端提供此代码:

<div id="paypal-button-container-P-REF"></div>
<script src="https://www.paypal.com/sdk/js?client-id=REF_ID=subscription" data-sdk-integration-source="button-factory"></script>
<script>
  paypal.Buttons({
      style: {
          shape: 'pill',
          color: 'gold',
          layout: 'vertical',
          label: 'subscribe'
      },
      createSubscription: function(data, actions) {
        return actions.subscription.create({
          /* Creates the subscription */
          plan_id: 'P-REF',
          quantity: 1 // The quantity of the product for a subscription
        });
      },
      onApprove: function(data, actions) {
        alert(data.subscriptionID); // You can add optional success message for the subscriber here

        actions.redirect('/thankyou', _external=True);
      }
  }).render('#paypal-button-container-REF'); // Renders the PayPal button
</script>

在服务器端,它应该是这样的:

@app.route('/upgrade',methods = ['POST','GET'])
@login_required
def upgrade():
    global user, user_email,user_category
    
    print(request.method )
    if request.method == 'POST':
        subscriptionID = request.args.get('subscriptionID')
        print(subscriptionID)
        
        if subscriptionID == None:
            flash('Payment error, please try again.','error')
        else:
            flash('Payment successful, thanks!','success')
            
            insert_payment(subscriptionID,user.id,'best_plan')#Add payment id to DB
            
            update_user_subscription(user.id,'best_plan')#Update user plan
            
            
    return render_template('upgrade.html', u_email=user_email, u_category = user_category)

但是我需要使用POST方法检索订阅方法以将其保存在数据库中。

客户群方法未发送帖子信息。

烧瓶和JS可以轻松地做到这一点吗?我不明白任何电子市场只能与客户端代码一起使用。

请注意,PayPal中有关服务器端的代码主要用于经典订单支付,而不是订阅。

I'd like to have a simple solution to get the subscription ID after a payment approval in a Flask app.

Paypal provides this code only for client side:

<div id="paypal-button-container-P-REF"></div>
<script src="https://www.paypal.com/sdk/js?client-id=REF_ID=subscription" data-sdk-integration-source="button-factory"></script>
<script>
  paypal.Buttons({
      style: {
          shape: 'pill',
          color: 'gold',
          layout: 'vertical',
          label: 'subscribe'
      },
      createSubscription: function(data, actions) {
        return actions.subscription.create({
          /* Creates the subscription */
          plan_id: 'P-REF',
          quantity: 1 // The quantity of the product for a subscription
        });
      },
      onApprove: function(data, actions) {
        alert(data.subscriptionID); // You can add optional success message for the subscriber here

        actions.redirect('/thankyou', _external=True);
      }
  }).render('#paypal-button-container-REF'); // Renders the PayPal button
</script>

In server side, it should be something like:

@app.route('/upgrade',methods = ['POST','GET'])
@login_required
def upgrade():
    global user, user_email,user_category
    
    print(request.method )
    if request.method == 'POST':
        subscriptionID = request.args.get('subscriptionID')
        print(subscriptionID)
        
        if subscriptionID == None:
            flash('Payment error, please try again.','error')
        else:
            flash('Payment successful, thanks!','success')
            
            insert_payment(subscriptionID,user.id,'best_plan')#Add payment id to DB
            
            update_user_subscription(user.id,'best_plan')#Update user plan
            
            
    return render_template('upgrade.html', u_email=user_email, u_category = user_category)

But I need to retrieve the subscriptionID with a post method to save it in a database.

The client base method doesn't send back post information.

Any idea to do it easily with flask and JS? I don't understand how any e-market could work only with a client side code.

Note that the code available about server side in Paypal is mainly for classic order payment, not subscriptions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南汐寒笙箫 2025-02-04 11:15:33

Subscribe to a webhook for the PAYMENT.SALE.COMPLETED event. This way you will get notifications for every payment on the subscription, not just the first one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文