【问题标题】:Submit form from Paypal script after approved payment in Django在 Django 中批准付款后从 Paypal 脚本提交表单
【发布时间】:2021-05-21 09:09:09
【问题描述】:

我有一个用户注册表单,我希望用户付费以注册我的应用程序。 我已经设置了用于付款的前端 paypal 以及带有提交按钮的用户注册,但我不知道如何整合两者。

<div id="paypal-button-container"></div>

   <script src="https://www.paypal.com/sdk/js?client-id=sb&currency=USD"></script>

    <script>
        // Render the PayPal button into #paypal-button-container
        paypal.Buttons({

            // Set up the transaction
            createOrder: function(data, actions) {
                return actions.order.create({
                    purchase_units: [{
                        amount: {
                            value: '00.01'
                        }
                    }]
                });
            },

            // Finalize the transaction
            onApprove: function(data, actions) {
                return actions.order.capture().then(function(details) {
                    // Show a success message to the buyer
                    alert('Transaction completed by ' + details.payer.name.given_name + '!');
                });
            }


        }).render('#paypal-button-container');
    </script>

    <button class="w3-button w3-blue w3-padding-large" type="submit">
        Buy Now
    </button>
    </form>

我希望在脚本的“onapprove”部分中使用提交按钮,但我不确定如何使用。或者,我正在考虑从视图中调用我的“购买”功能

def buy(request):
    if request.method == 'POST':
        form = UserRegisterForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            email = f"{data['email']}"
            username = f"{data['username']}"
            form.instance.username = username
            form.save()
            return redirect('login')
    else:
        form = UserRegisterForm()
    return render(request, 'users/buy.html', {'form': form})

另一件事是系统必须在付款前检查表单输入是否唯一且有效。付款成功后将用户重定向到注册页面是不是最好?

forms.py

class UserRegisterForm(UserCreationForm):
    email = forms.EmailField()
    username = forms.CharField(max_length=20)

    class Meta:
        model = User
        fields = ['email', 'username', 'password1', 'password2']

【问题讨论】:

    标签: django paypal django-forms django-registration django-paypal


    【解决方案1】:

    您可以在付款前使用onClick检查值是否有效:https://developer.paypal.com/docs/checkout/integration-features/validation/

    将其与适当的服务器集成相结合,在创建或捕获订单时将值传输到获取正文中,以便您的服务器可以存储它们。

    对于该服务器集成,您需要创建两条路由,一条用于“创建订单”,一条用于“捕获订单”,documented here。这些路由应该返回 JSON 数据。

    将这些路由与以下按钮批准流程配对:https://developer.paypal.com/demo/checkout/#/pattern/server,加上您的 onClick 函数

    【讨论】:

      猜你喜欢
      • 2013-12-26
      • 2012-12-17
      • 2013-05-26
      • 2017-11-17
      • 2012-08-21
      • 2014-04-30
      • 2013-09-03
      • 2013-02-06
      • 2021-10-11
      相关资源
      最近更新 更多