【问题标题】:Retrieve a Card with Customer is throwing an errorRetrieve a Card with Customer 引发错误
【发布时间】:2019-06-22 02:36:30
【问题描述】:

我正在尝试在 Django 中创建一种方式,让订阅者将其默认付款方式更改为与他们一起存档的现有卡。当我尝试访问 Stripe 卡时,我收到一条错误消息“无法在没有客户、收件人或帐户 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') 代替。”

【问题讨论】:

    标签: python stripe-payments


    【解决方案1】:

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

                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)
    

    【讨论】:

      【解决方案2】:

      如果卡已与客户关联,您只需一个 API 请求即可:

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

      在检索卡片时,您也可以在一个 API 请求中执行此操作,而无需先检索客户:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-12-01
        • 2021-06-27
        • 2014-11-28
        • 2020-10-29
        • 2019-02-10
        • 1970-01-01
        • 2022-08-09
        • 2019-01-20
        相关资源
        最近更新 更多