python - Django 支付集成

标签 python django

我有一个由 View 方法 A 渲染的购物车,我想通过 pesapal package 获取购物车中的详细信息以提交到 pesapal api。 。我使用与 sandbox app, PaymentView 相同的方法。购物车有一个结帐按钮,用于将数据提交到 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'),
)

最佳答案

通过让 django 将数据解析到模板(在本例中为购物车),您可以快速摆脱困境。这可以通过让您的方法返回您想要的数据来完成。像这样的东西:

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

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

{{yourvariable}}

然后让 api 完成剩下的工作。

关于python - Django 支付集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37886555/

相关文章:

python - 如何在 Django 2.1 中使用多种形式?

python正则表达式直到第一组数字

python - 每次有标点符号就将字符串拆分成句子,有标点符号吗?

python - 安装Django 1.9导致的pip错误

asp.net-mvc - 是否可以从 asp.net mvc 应用程序和 python 应用程序共享 session 数据?

javascript - 在渲染的 html 页面上使用 Django 渲染的 Javascript 列表

python - 在 Centos 6.4 上安装 mysql-python

python - 根据大纲拆分 pdf

python - Flask peewee 无法在网络服务器上工作

python - Django 中的 URLField() 可以加载本地主机路径吗?