#arielbo-subscription

1 messages · Page 1 of 1 (latest)

cerulean fern
#

@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

mighty stump
#

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 ?

cerulean fern
#

@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!

sudden gale
#

@mighty stump hello, let me step in from here. Please organize and summarize your questions so we can help you better!

mighty stump
#

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.

sudden gale
#

Customer A buys subscription A and registers his card in paymentmethods
After this you should have PaymentMethodId, correct?

mighty stump
#

Yes, I have the PaymentMethodId

#

my problem is with the second and subsequent purchases

sudden gale
#

In subsequent purchases you used that PaymentMethodId, as default_payment_method, but it is still not used?

mighty stump
#

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

sudden gale
#

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?

mighty stump
#

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

sudden gale
#

Then now you would want to decide which one you want to reuse first, then pick its Id

mighty stump
#

maybe I echo (array_key_last($pm)) I imagine that I could get the last id that would correspond to the last card saved

sudden gale
#

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

mighty stump
#

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

sudden gale
#

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

mighty stump
#

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

mighty stump
#

or orakaro please could you guide me with this?

sudden gale
#

Hey, sorry we are in multiple workstream this morning. Replies can be slow but we are looking at this!

mighty stump
#

thanks orakaro

#

you are the best