繁体   English   中英

与客户检索卡正在抛出错误

[英]Retrieve a Card with Customer is throwing an error

我正试图在Django中为订户创建一种方式,将其默认付款方式更改为存档的现有卡。 当我尝试访问条带卡时,我收到错误“无法检索没有客户,收件人或帐户ID的卡。使用customer.sources.retrieve('card_id'),recipient.cards.retrieve('card_id')或者改为account.external_accounts.retrieve('card_id')。“ 尽管提供了客户。

我试图从视图中删除其他逻辑。 我不知道从哪里开始。

@login_required
# @is_subscriber
def update_payment(request):
    title = 'Update Payment Methods'
    description = title
    key = settings.STRIPE_PUBLISHABLE_KEY
    user_membership = UserMembership.objects.get(user=request.user)
    current_payment_methods = stripe.PaymentMethod.list(customer=user_membership.stripe_customer_id,
                                                        type='card')
    customer = stripe.Customer.retrieve(user_membership.stripe_customer_id)
    if request.method == 'POST':
        if request.POST.get('source_obj'):
            logger.info('User is attempting to delete a paid payment method')
            card_id = request.POST.get('source_obj')
            if len(current_payment_methods) > 1:
                stripe.Customer.delete_source(
                    user_membership.stripe_customer_id, card_id
                )
                message = 'This payment method has been removed from your account.'
                messages.info(request, message=message)
                logger.info('User has deleted a payment method')
            elif len(current_payment_methods) <= 1:
                message = 'You must have at least one payment method associated with your account.'
                messages.error(request, message=message)
                logger.info('User failed to delete only payment method.')
            return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
        if request.POST.get('new_prim'):
            logger.info('User is changing their default payment method.')

            card_id = request.POST.get('new_prim')
            print(card_id)
            card = stripe.Card.retrieve(id=card_id, customer=user_membership.stripe_customer_id)
            stripe.Customer.modify(user_membership.stripe_customer_id,
                                   default_source=card)

            message = 'You have changed your primary payment method.'
            messages.success(request, message=message)
        else:
            logger.info('Customer is adding a payment method.')
            stripe.Customer.create_source(
                user_membership.stripe_customer_id,
                source=request.POST.get('stripeToken')
            )
            message = 'You have added this payment method to your account.'
            messages.success(request, message=message)
        return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
    return render(request, 'memberships/update_payment.html',
                  {'title': title, 'description': description, 'key': key,
                   'current_payment_methods': current_payment_methods,
                   'customer': customer})

返回此错误而不是成功响应。

“无法检索没有客户,收件人或帐户ID的卡。使用customer.sources.retrieve('card_id'),recipient.cards.retrieve('card_id')或account.external_accounts.retrieve('card_id')代替。”

解决方案是通过客户对象检索卡。

            customer = stripe.Customer.retrieve(user_membership.stripe_customer_id)
            card = customer.sources.retrieve(card_id)
            stripe.Customer.modify(user_membership.stripe_customer_id,
                                   default_source=card)

如果该卡已与客户关联,则只需一个API请求即可执行此操作:

customer = stripe.Customer.modify('cus_123',default_source='card_123')

检索卡时,您也可以将其转换为一个API请求,而无需先检索客户:

card = stripe.Customer.retrieve_source('cus_123', 'card_123')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM