#arielbo-subscription
1 messages · Page 1 of 1 (latest)
@mighty stump we always duplicate, you need to change your logic really. You shouldn't ask a customer for card details if you already have a card saved. You should show them the existing card and let them re-use that card
hello again, koopajh please
help me with this
$subscription = $stripe->subscriptions->create([
'customer' => $json_obj->cus,
'items' => [[
'price' => $json_obj->s_price_id,
]],
'payment_behavior' => 'default_incomplete',
'default_payment_method' => $pm,
'expand' => ['latest_invoice.payment_intent.'],
]);
how can I do this: ¿ You should show them the existing card and let them re-use that card ?
@mighty stump sorry you've been asking a lot of questions (totally fine!) but without much details. Why is someone repeatedly creating subscriptions over and over and you get duplicate cards?
feels more like testing and you don't really know yet what you are asking about!
@mighty stump hello, let me step in from here. Please organize and summarize your questions so we can help you better!
Excuse me for so many questions, I am looking to clarify how your api works, I wish I was the stripe developer and I would not ask these questions surely so obvious to you. I will make an effort to explain what happens to me in my programming in php stripe of subscriptions.
Eg Customer A buys subscription A and registers his card in paymentmethods, but then the same customer A buys subscription B and registers the card a second time in paymentmethods.
Could you tell me how I can avoid this, I don't know where or with what code.
Customer A buys subscription A and registers his card in paymentmethods
After this you should have PaymentMethodId, correct?
Yes, I have the PaymentMethodId
my problem is with the second and subsequent purchases
In subsequent purchases you used that PaymentMethodId, as default_payment_method, but it is still not used?
yes I was think this, for this reason if I have saved his PaymentMethodId I get from database with:
$pm = $stripe->paymentMethods->all([
'customer' => $json_obj->cus,
'type' => 'card'
]); }
and them I try to use in $subscription = $stripe->subscriptions->create([
'customer' => $json_obj->cus,
'items' => [[
'price' => $json_obj->s_price_id,
]],
'payment_behavior' => 'default_incomplete',
'default_payment_method' => $pm,
'expand' => ['latest_invoice.payment_intent.'],
]);
$pm variable but not work
Okie, let's talk a bit about this code first. Isn't $pm an array of objects? You are passing it directly to default_payment_method, which expects a single String value, why?
let me see if an array with 10 duplicate cards I know maybe this is the problem becouse default_payment_method wait just one id
yes, you are very right it is an array
Then now you would want to decide which one you want to reuse first, then pick its Id
maybe I echo (array_key_last($pm)) I imagine that I could get the last id that would correspond to the last card saved
That would be an object, you will still need its Id
An easy debug method is that, head over to your Stripe Dashboard and see what you sent with your request
Your method logs should be here https://dashboard.stripe.com/test/logs
ok this keep like this
$pm = $stripe->paymentMethods->all([
'customer' => $json_obj->cus,
'type' => 'card'
]);
$last_id_pm = array_key_last($pm->data);
$last_pm_id = $pm->data[$last_id_pm]['id'];
$subscription = $stripe->subscriptions->create([
'customer' => $json_obj->cus,
'items' => [[
'price' => $json_obj->s_price_id,
]],
'payment_behavior' => $last_pm_id,
'expand' => ['latest_invoice.payment_intent.'],
]);
let me try
This makes more sense to me, yes, but we still need to see if it gets the correct Id. You would need to debug it
I assume that the last card saved is the one that works for the customer, but I dont see if exist some data in array that tell me if this working in the last oportunity, sorry if my logic is bad in this point
works good !! orakaro we do it thank you so much, I fail in array $pm ...cards are no longer being duplicated in paymentMethods object
I have been intrigued by how I could show it and make the user instead of reinserting his card reuse the one he has saved, it would be something like a quick purchase button on my platform, right? maybe you know about one example? I using my custom flow with php-stripe api
or orakaro please could you guide me with this?
Hey, sorry we are in multiple workstream this morning. Replies can be slow but we are looking at this!