贝宝订阅 Django

PayPal Subscriptions Django

我正在尝试在我的应用程序中实现 PayPal 订阅功能,这就是我到目前为止的进展。我创建了一个处理支付过程的函数,并为用户订阅了选定的订阅计划,但我不知道如何为手动取消订阅创建类似的函数?那可能吗?我的意思是,作为用户我怎样才能取消我的订阅?

这是我的支付处理函数。

def process_order(request, plan_slug):
    host = request.get_host()

    plan = Subscription.objects.get(slug__iexact=plan_slug)
    request.session['plan_id'] = plan.pk

    order, created = Order.objects.get_or_create(
        plan=plan,
        user=request.user,
        total_cost=plan.sum_price,
    )
    if created:
        request.session['order_id'] = order.pk
    elif order:
        request.session['order_id'] = order.pk
        order.created = timezone.now()
        order.save()

    if plan.slug == 'some_slug':
        user = Customuser.objects.get(email=request.user.email)
        user.subscription_plan = plan
        user.sub_renewal_date = None
        user.save()
        messages.success(request, 'You are now some_slug plan!')
        return redirect('accounts:profile', user.email)

    paypal_dict = {
        "cmd": "_xclick-subscriptions",
        'business': settings.PAYPAL_RECEIVER_EMAIL,
        'a1': 1,
        'period1': '1 M',
        "a3": plan.sum_price,  # monthly price
        "p3": plan.plan_duration,  # duration of each unit (depends on unit)
        "t3": 'M',  # duration unit ("M for Month")
        "src": "1",  # make payments recur
        "sra": "1",  # reattempt payment on payment error
        "no_note": "1",
        'item_name': plan_slug,
        'item_number': order.pk,
        'invoice': str(order.pk),
        'custom': {
            'order': order.pk,
            'user': request.user.email,
        },
        'currency_code': 'USD',
        'notify_url': 'http://{}{}'.format(host,
                                           reverse('billing:paypal-ipn')),
        'return_url': 'http://{}{}'.format(host,
                                           reverse('billing:payment_done')),
        'cancel_return': 'http://{}{}'.format(host,
                                              reverse('billing:payment_canceled')),
    }
    form = CustomPayPalForm(initial=paypal_dict)

    context = {
        'plan': plan,
        'form': form,
    }
    return render(request, 'subscriptions/checkout.html', context)

这可以通过使用 django-paypal 离子信号来完成,这些信号由 PayPal API 发送回服务器。