<script src="https://www.paypal.com/sdk/js?client-id=ATt8FBfDMVSlqfcaiTw-KFUQx5Br6P6lz_ISfkTvXRj-vB0L4JR_ydFTiZHTNz-JLlw-jZtQFeA0GfCW"></script>
<script>
// Define the total amount globally so it's accessible
const totalAmount = 12; // Dynamic value based on your application's logic
paypal.Buttons({
// Create Order
createOrder: (data, actions) => {
console.log("Creating PayPal Order...");
console.log("Total amount: ", totalAmount);
return actions.order.create({
purchase_units: [{
amount: {
value: totalAmount, // Use the globally defined totalAmount
},
}],
});
},
// On Approval
onApprove: async (data, actions) => {
console.log("OnApprove triggered...");
console.log("Data received: ", data);
const order = await actions.order.capture();
console.log("Order captured:", order);
// Extract payment ID and payer ID from PayPal's response
const paymentId = data.paymentID; // PayPal payment ID
const payerId = data.payerID; // PayPal payer ID
// var status = data.status; // PayPal status
console.log('Payment ID:', paymentId);
console.log('Payer ID:', payerId);
console.log('Status:', data.status);
// Send payment details to the backend to confirm payment
await fetch('http://127.0.0.1:8000/api/v1/weekly/order/paypal/confirm/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
paymentId: paymentId,
PayerID: payerId,
total_amount: totalAmount, // Dynamic amount based on the order
order_data: [{
"day_of_week": "Monday", // Example, replace with dynamic values
"order_items": [{
"product": 1, // Product ID
"quantity": 2
}],
"number_of_people": 2
}],
payment_info: {
"name": "John Doe", // Replace with user's name
"email": "johndoe@example.com", // Replace with user's email
"phone": "+123456789", // Replace with user's phone
"address": "123 Main St", // Replace with user's address
"postal_code": "12345" // Replace with user's postal code
}
})
})
.then(response => {
console.log("Backend response status:", response.status);
return response.json();
})
.then(data => {
console.log("Payment confirmed:", data);
alert('Payment confirmed successfully!');
})
.catch(error => {
console.error('Error confirming payment:', error);
alert('Error confirming payment!');
});
},
// On Error
onError: (err) => {
console.error('PayPal Error:', err);
alert('Error with PayPal payment.');
}
}).render('#paypal-button-container');
</script>
</body>
</html>
give me this code by next js
I'll create a Next.js implementation of this PayPal payment integration. Here's how you can set it up:
Page Structure:
app/page.tsx
) renders a simple layout with a heading and the PayPal button componentPayPal Integration:
Script
component to properly load the PayPal SDKBest Practices:
totalAmount
propImportant Notes:
For production, you should store your PayPal client ID in environment variables:
NEXT_PUBLIC_PAYPAL_CLIENT_ID=your_client_id_here
Then use it in the Script src: src={
https://www.paypal.com/sdk/js?client-id=${process.env.NEXT_PUBLIC_PAYPAL_CLIENT_ID}`}`
This implementation maintains all the functionality of your original code while adapting it to work properly in a Next.js application.
here make a page paypal , stipe , and many method , then if click paypa then go to this page as I given you privous code, and then do payment
I'll create a payment gateway selection page with multiple payment methods, and when PayPal is selected, it will navigate to the PayPal payment page we created earlier.
I've created a complete payment gateway system with multiple payment methods:
Payment Method Selection Page
PayPal Payment Page
Additional Payment Methods
You can easily customize this implementation by:
This implementation provides a complete payment gateway selection system that guides users through the payment process with multiple options.