Django支付集成

2024-05-14 06:48:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个由视图方法a呈现的购物车,我想通过pesapal package获取购物车中要提交给pesapal api的细节。我使用的方法与sandbox app, PaymentView相同。购物车有一个checkout按钮,用于将数据提交给api。Sandbox方法simlpy将数据作为字典(order_info)获取,并将其馈送到url(get_payment_url),后者随后为api提供数据,因此当我打开模板时,api的iframe指示我正在提取字典中指定的数量。如何通过购物车提交数据?在

## Sandbox method submitting data to the api
class PaymentView(TemplateView, PaymentRequestMixin):
    template_name = 'payment/payment.html'
    # how the sandbox app submits data to api:

    def get_context_data(self, **kwargs):
        ctx = super(PaymentView, self).get_context_data(**kwargs)

        order_info = {
        'amount': '1000',
        'description': 'Payment for Stuff',
        'reference': 2,
        'email': 'you@email.com'
    }

    ctx['pesapal_url'] = self.get_payment_url(**order_info)
    return ctx

## view method A:
def show_cart(request, template_name="payment/cart.html"):
    if request.method == 'POST':
        postdata = request.POST.copy()
        if postdata['submit'] == 'Remove':
            cart.remove_from_cart(request)
        if postdata['submit'] == 'Update':
            cart.update_cart(request)
        if postdata['submit'] == 'Checkout':
            # submission to api should occur here 
    cart_items = cart.get_cart_items(request)
    cart_subtotal = cart.cart_subtotal(request)
    return render_to_response(template_name, locals(), context_instance=RequestContext(request))

# my url
urlpatterns = patterns('cart.views',
(r'^$', 'show_cart', { 'template_name': 'payment/cart.html' }, 'show_cart'),
)

Tags: to方法nameapiurldatagetif
2条回答

查看文档和source code,似乎没有设置任何数据的方法。你有这样做的例子吗?在

您可以通过让django将数据解析为模板或在本例中是您的购物车,从而快速地摆脱它。这可以通过让方法返回所需的数据来完成。比如:

return render(request, 'yourpage.html', 
                {'yourvariable':yourvariable},  context_instance=RequestContext(request))

然后,只需在模板中获取您的值(在您的情况下为cart),例如:

^{pr2}$

然后让api来完成其余的工作。

相关问题 更多 >

    热门问题