Skip to main content
 首页 » 编程设计

python之Django 和 Stripe : How to send error messages

2025年01月19日25bonelee

我在我的项目中集成了 Stripe,只要所有信用卡详细信息都没有问题,我的应用程序就可以完美运行。但是,我还没有集成错误消息处理,而且我有点难以理解它是如何工作的。我对 Ajax 还不是很熟悉,也许这就是问题所在。调查 reference API ,然后单击 Python,这对我来说看起来不像 Python 代码,而且我不知道如何处理那里解释的错误消息处理。

任何人都可以帮我解决附加的代码吗?我也会对我必须研究和学习的内容感兴趣,以便更好地理解 API 引用中用 Python 编写的内容。

def stripe_charge(self, transaction_profile, **kwargs): 
        try: 
            c = stripe.Charge.create( 
                amount=kwargs['total']*100, 
                currency='eur', 
                description=kwargs['order_id'], 
                receipt_email=transaction_profile.email, 
                source=kwargs['token'], 
            ) 
 
            new_charge_obj = self.model( 
                    transaction_profile = transaction_profile, 
                    stripe_id = c.id, 
                    paid = c.paid, 
                    refunded = c.refunded, 
                    outcome = c.outcome, 
                    outcome_type = c.outcome['type'], 
                    seller_message = c.outcome.get('seller_message'), 
                    risk_level = c.outcome.get('risk_level'), 
            ) 
            new_charge_obj.save() 
 
        except stripe.error.CardError as e: 
          # Since it's a decline, stripe.error.CardError will be caught 
          body = e.json_body 
          err  = body.get('error', {}) 
 
          # print "Status is: %s" % e.http_status 
          # print "Type is: %s" % err.get('type') 
          # print "Code is: %s" % err.get('code') 
          # # param is '' in this case 
          # print "Param is: %s" % err.get('param') 
          # print "Message is: %s" % err.get('message') 
 
          return False, e 
 
        return new_charge_obj.paid, new_charge_obj.seller_message 

请您参考如下方法:

您可以使用 django 中包含的消息模块。

import django.contrib.messages 
# usage 
messages.error(request, "Something bad happened") 

文档:Django messages framework