#your-friendly-neighborhood_code
1 messages · Page 1 of 1 (latest)
Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.
- your-friendly-neighborhood_unexpected, 6 days ago, 13 messages
👋 Welcome to your new thread!
⏲️ We'll be here soon! We typically respond in a few minutes, but in some cases we might need a bit more time (e.g., server's busy, you've got a complex question, etc.).
⏱️ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can start a new thread if you have another question.
🔗 This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1247657673382559785
📝 Have more to share? Add details, code, screenshots, videos, etc. below.
Hello
I'm trying to make an ajax post after the payment has been done.
<script>// This is your test publishable API key.
const stripe = Stripe("x");
// The items the customer wants to buy
const items = [{ id: "consultanta" }];
let elements;
initialize();
checkStatus();
document
.querySelector("#payment-form")
.addEventListener("submit", handleSubmit);
// Fetches a payment intent and captures the client secret
async function initialize() {
const { clientSecret } = await fetch("/create.php", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ items }),
}).then((r) => r.json());
elements = stripe.elements({ clientSecret });
const paymentElementOptions = {
layout: "tabs",
};
const paymentElement = elements.create("payment", paymentElementOptions);
paymentElement.mount("#payment-element");
}
async function handleSubmit(e) {
e.preventDefault();
setLoading(true);
const { error, paymentIntent } = await stripe.confirmPayment({
elements,
confirmParams: {
return_url: "https://radierebc.eu/oferta",
},
redirect: "if_required",
});
// Handle the response
if (error) {
if (error.type === "card_error" || error.type === "validation_error") {
showMessage(error.message);
Swal.fire({
title: "Eroare la plată",
text: error.message,
icon: "error"
});
} else {
showMessage("An unexpected error occurred.");
}
} else {
// Log or alert the paymentIntent object
const payment_method = paymentIntent.payment_method;
const payment_intent = paymentIntent.id;
var credite = $('#credite').val();
var valoare = $('#valoare').val();
var contact = $('#contact').val();
$.ajax({
url: '/scripts?script=payment',
type: "POST",
data: {
'payment_method': payment_method,
'payment_intent': payment_intent,
'credits': credite,
'value': valoare,
'contact': contact,
'nume': nume,
'salar': salar,
'valoare_credite': valoare_credite,
'ora_contact': ora_contact,
}, // Send paymentId as form data
success: function (data) {
$('#live').html(data);
},
error: function (xhr, status, error) {
console.error('There was a problem sending the PaymentIntent. Status:', xhr.status);
// Optionally, show an error message to the user or perform error handling
}
});
}
setLoading(false);
}
// Fetches the payment intent status after payment submission
async function checkStatus() {
const clientSecret = new URLSearchParams(window.location.search).get(
"payment_intent_client_secret"
);
if (!clientSecret) {
return;
}
const { paymentIntent } = await stripe.retrievePaymentIntent(clientSecret);
switch (paymentIntent.status) {
case "succeeded":
showMessage("Payment succeeded!");
break;
case "processing":
showMessage("Your payment is processing.");
break;
case "requires_payment_method":
showMessage("Your payment was not successful, please try again.");
break;
default:
showMessage("Something went wrong.");
break;
}
}
}</script> ```
my problem is that, after user pays, eeven if it's sccesful / failure, ajax won't get sent.
it won't appear even at network
Hi there!
Just to be clear, after a user completes a payment, they'll be redirected to https://radierebc.eu/oferta. Is this the point at which you're trying to make an ajax post?
No.
After a user completes a payment, I don't want any redirect.
But, to send the ajax post instead.
Because, on server-side, it will return messages as 'succes' or whatever else messages there are,
and it will save the payment in local database
anyfurther, after the user makes the payment, it does not get redirected to https://radierebc.eu/oferta
even so
i'm trying to make the ajax post on the same page where user clicks on payment button
payment works fine, as it gets saved on stripe, it returns
return_url: https://radierebc.eu/oferta
payment_method_data[type]: card
... + and so on
But just it does not run my ajax post part
i have missed the
var nume = $('#nume').val();
var salar = $('#salar').val();
var valoare_credite = $('#valoare_credite').val();
var ora_contact = $('#ora_contact').val();
Sorry, it's kind of hard to follow the one-off messages.
but now I get
[05-Jun-2024 00:17:21 Europe/Bucharest] Stripe Notice: Undefined property of Stripe\PaymentMethod instance: exp_month
[05-Jun-2024 00:17:21 Europe/Bucharest] Stripe Notice: Undefined property of Stripe\PaymentMethod instance: exp_year
[05-Jun-2024 00:17:21 Europe/Bucharest] Stripe Notice: Undefined property of Stripe\PaymentMethod instance: last4
So, to confirm, after a customer submits the PaymentElement, what happens and where are you blocked?
Following> I have fixed the code. It was missing those 4 last lines, which I have not included.
Now, AJAX gets posted good. But, I am getting an php error.
[05-Jun-2024 00:17:21 Europe/Bucharest] Stripe Notice: Undefined property of Stripe\PaymentMethod instance: exp_month
[05-Jun-2024 00:17:21 Europe/Bucharest] Stripe Notice: Undefined property of Stripe\PaymentMethod instance: exp_year
[05-Jun-2024 00:17:21 Europe/Bucharest] Stripe Notice: Undefined property of Stripe\PaymentMethod instance: last4
``` those are the errors.
And, the code I'm running, it is ->
$stripe = new \Stripe\StripeClient('x');
$paymentMethod = $stripe->paymentMethods->retrieve($payment_method, []);
$paymentIntent = $stripe->paymentIntents->retrieve($payment_intent);
$amountPaid = $paymentIntent->amount;
$status = $paymentIntent->status;
if ($paymentMethod instanceof \Stripe\PaymentMethod) {
// Payment method retrieval was successful
$card_brand = $paymentMethod->card->brand;
$exp_month = $paymentMethod->exp_month;
$exp_year = $paymentMethod->exp_year;
$last4 = $paymentMethod->last4;
Have you tried logging paymentMethod to ensure it has the properties you're trying to access?
payment_method: pm_1PO4jIEt4ff5.............
payment_intent: pi_3PO4itEt4ff5g.............
it shows the correct method & intent
Or, you're refeering to, what arrays does paymentMethod has?
i should check, if my paymentMethod has exp_month, exp_year, last4 available?
correct
"paid": true,
"payment_method": "xxxxxxxxxxxx",
"payment_method_details": {
"card": {
"amount_authorized": 200,
"brand": "xxxxx",
"checks": {
"address_line1_check": null,
"address_postal_code_check": null,
"cvc_check": "pass"
},
"country": "RO",
"exp_month": xx,
"exp_year": xxxx,
"extended_authorization": {
"status": "disabled"
},
taken from charge.updated (stripe dashboard)
That doesn't really help. You want to make sure paymentMethod.exp_month for example is actually available in your code
I'll make a log fthen to get all of paymentMethod ?
Yes
[05-Jun-2024 00:26:52 Europe/Bucharest] Stripe\PaymentMethod JSON: {
"id": "xaxax",
"object": "payment_method",
"allow_redisplay": "unspecified",
"billing_details": {
"address": {
"city": null,
"country": "RO",
"line1": null,
"line2": null,
"postal_code": null,
"state": null
},
"email": null,
"name": null,
"phone": null
},
"card": {
"brand": "xaxa",
"checks": {
"address_line1_check": null,
"address_postal_code_check": null,
"cvc_check": "pass"
},
"country": "RO",
"display_brand": "aaxx",
"exp_month": aaa,
"exp_year": aaaaa,
"fingerprint": "xaxa",
"funding": "credit",
"generated_from": null,
"last4": "aaaa",
"networks": {
"available": [
"xaxa"
],
"preferred": null
},
"three_d_secure_usage": {
"supported": true
},
"wallet": null
},
"created": 1717536407,
"customer": null,
"livemode": true,
"metadata": [],
"type": "card"
}
[05-Jun-2024 00:26:53 Europe/Bucharest] Stripe Notice: Undefined property of Stripe\PaymentMethod instance: exp_month
[05-Jun-2024 00:26:53 Europe/Bucharest] Stripe Notice: Undefined property of Stripe\PaymentMethod instance: exp_year
[05-Jun-2024 00:26:53 Europe/Bucharest] Stripe Notice: Undefined property of Stripe\PaymentMethod instance: last4
Right, so paymentMethod.exp_month doesn't exist but paymentMethod[card].exp_month does
so i should go for $exp_month = $paymentMethod->card->exp_month ?
Try it
Sure thing!
Link as in the Link payment method type? https://docs.stripe.com/payments/link