I'm attempting to create a way in Django for a subscriber to change their default payment method to an existing card on file with them. When I attempt to access the Stripe card I receive an error "Can't retrieve a card without a customer, recipient or account ID. Use customer.sources.retrieve('card_id'), recipient.cards.retrieve('card_id'), or account.external_accounts.retrieve('card_id') instead." despite providing the customer.
I've attempted to remove other logic from the view. I have no idea where to go from here.
@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})
Return this error instead of a successful response.
"Can't retrieve a card without a customer, recipient or account ID. Use customer.sources.retrieve('card_id'), recipient.cards.retrieve('card_id'), or account.external_accounts.retrieve('card_id') instead."
Solution is to retrieve the card THROUGH the customer object.
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)
If the card is already associated with the Customer, you can do this with just one API request:
customer = stripe.Customer.modify('cus_123',default_source='card_123')
When retrieve a Card, you can also do that into one API request without having to retrieve the Customer first:
card = stripe.Customer.retrieve_source('cus_123', 'card_123')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.