更改代码组织,因此ID相同

时间:2015-03-26 15:07:25

标签: python django payment-gateway braintree

我有一个使用Braintree支付网关的Django应用程序。 我的问题是订阅ID和ID历史订阅彼此不同。 我认为问题也可能是取消订阅。

这是ID表格创建的订阅:id:u'hjg8gm',这里是来自状态历史ID的ID:u'9ppndw'。它们是不同的,但我希望它们是相同的。

def create_subscription():
    try:
        result = braintree.Subscription.create({
            "payment_method_token": payment_method_token,
            "plan_id": "myPlanId"
        })
        if result.is_success:
            return result
    except braintree.exceptions.NotFoundError:
        return "No customer found."

def get_subscription_history():
    result = create_subscription()
    history = braintree.Subscription.find(result.subscription.id)
    return history

def cancel_subscription():
    result = create_subscription()
    cancel = braintree.Subscription.cancel(result.subscription.id)
    return cancel

def create_subscription_view(request):
    return render(request, "sheet/subscript.html", {'subscript': create_subscription()})

def get_history(request):
    return render(request, "sheet/history.html", {"history": get_subscription_history()})

def cancel_subscription_view(request):
    return render(request, "sheet/cancel.html", {"cancel": cancel_subscription()})

1 个答案:

答案 0 :(得分:0)

我对Braintree没有任何经验。但正如我已经观察到你的代码。在每种方法上,您都要创建一个新订阅。

这些步骤可以帮助您:

1。仅在以下时间创建订阅:

def create_subscription_view(request):
    return render(request, "sheet/subscript.html", {'subscript': create_subscription()})

2. 然后将订阅ID保存到非易失性内存中。 (例如:在数据库中)

3。从数据库中提取subscription_id并向Braintree请求了解详情:

# An example to fetch subscription id
subscription_id = SubscriptionModel.objects.filter(user=request.user)[:1].subscription_id
# Request to braintree for subscription_id
history = braintree.Subscription.find(result.subscription.id)
cancel  = braintree.Subscription.cancel(result.subscription.id)

抱歉,如果我的建议无效!