javascript - Error getting details of PayPal v2 Order created on client side
Get the solution ↓↓↓I have a website with a membership with 3 pages, one for presenting the membership (with my PayPal button), one with registration form (for adding the member in my database) and a last one when all this is complete( say " you can now login ").
I just followed a video on YouTube to add the PayPal button for my member to pay an amount before starting filling the registration form.
I am using PayPal SDK. Here is my code:
FIRST PAGE (account-step1.php)
<!DOCTYPE html>
<html lang="en">
<?php
// Include HEADER
include ("includes/head.php");
?>
<!-- SPECIFIC CSS -->
<link href="css/booking-sign_up.css" rel="stylesheet">
<style type="text/css">
.circle {
width: 70px;
height: 70px;
line-height: 70px;
border-radius: 50%;
font-size: 24px;
color: red;
text-align: center;
background: gold;
font-weight: bold;
}
/* Media query for mobile viewport */
@media screen and (max-width: 400px) {
#paypal-button-container {
width: 100%;
}
}
/* Media query for desktop viewport */
@media screen and (min-width: 400px) {
#paypal-button-container {
width: 250px;
}
}
</style>
<script src="https://www.paypal.com/sdk/js?client-id=sb¤cy=USD"></script>
</head>
<body>
<header class="header_in clearfix">
<div class="container">
<div id="logo">
<a href="index.php">
<img src="img/logo_sticky.svg" width="250" height="40" alt="" class="logo_sticky">
</a>
</div>
<?php include ("includes/topmenu.php"); ?>
<a href="#0" class="open_close">
<i class="icon_menu"></i><span>Menu</span>
</a>
<?php include ("includes/menu.php"); ?>
</div>
</header>
<!-- /header -->
<main class="bg_gray pattern">
<div class="container margin_60_40">
<div class="main_title center">
<span><em></em></span>
<h2><?php echo _e("Our Subscription Plan"); ?></h2>
<p>VIP 2021 - 2022</p>
</div>
<div class="row justify-content-center">
<div class="col-lg-12">
<div class="sign_up">
<div class="head">
<div class="title">
<h3>1 YEAR SUBSCRIPTION</h3>
</div>
</div>
<!-- /head -->
<div class="main text-center" style="font-size:16px;">
<p><i class="icon_star"></i> <?php echo _e("VIP Membership Card"); ?> <i class="icon_star"></i></p>
<p><i class="icon_star"></i> <?php echo _e("Take advantage of original discounts at all our partners everywhere near you"); ?> <i class="icon_star"></i></p>
<p><i class="icon_star"></i> <?php echo _e("Your subscription pays for itself in just 1 or 2 uses of discounts"); ?> <i class="icon_star"></i></p>
<p><i class="icon_star"></i> <?php echo _e("Each year your easy-to-use privileges are recharged in the application to make you enjoy again"); ?> <i class="icon_star"></i></p>
<p><i class="icon_star"></i> <?php echo _e("Contribute to better nutritional health for our children with $ 1 donated to the Club des petits déjeuners by subscription"); ?> <i class="icon_star"></i></p>
<p class="text-center">
<center>
<div class="circle">20$</div>
</center>
</p>
<hr>
<center><div id="paypal-button-container"></div></center>
</div>
</div>
<!-- /box_booking -->
</div>
<!-- /col -->
</div>
<!-- /row -->
</div>
<!-- /container -->
</main>
<!-- /main -->
<?php include ("includes/footer.php"); ?>
<div id="toTop"></div><!-- Back to top button -->
<div class="layer"></div><!-- Opacity Mask Menu Mobile -->
<?php include("includes/signin-modal.php"); ?>
<!-- COMMON SCRIPTS -->
<script src="js/common_scripts.min.js"></script>
<script src="js/common_func.js"></script>
<script src="assets/validate.js"></script>
<!-- Include the PayPal JavaScript SDK -->
<script src="https://www.paypal.com/sdk/js?client-id=AeQdfTQ2soaOdV7vgLYyTsnsSC3ptpSJJWFsDGwkTXmQocaCBfYPYWV7mHO-iPGX3_ZiLdCqxmqY9h6H¤cy=USD"></script>
<script>
paypal.Buttons({
style: {
color: 'gold',
shape: 'pill',
label: 'pay'
},
createOrder: function(data, actions) {
// This function sets up the details of the transaction, including the amount and line item details.
return actions.order.create({
purchase_units: [{
amount: {
value: '20.00'
}
}]
});
},
onApprove: function(data, actions) {
// This function captures the funds from the transaction.
return actions.order.capture().then(function() {
window.location = "transaction-completed.php?&orderID="+data.orderID;
});
}
}).render('#paypal-button-container');
//This function displays Smart Payment Buttons on your web page.
</script>
</body>
</html>
Answer
Solution:
I see this is a client-side only integration for payment creation and capture, where then after that client-side capture you use a server=side AI call to get the details of the order. That's a very strange hybrid, one wonders why you didn't integrate 'Set Up Transaction' and 'Capture Transaction on the server side (and paired with the approval JS for a server side integration) to get all the benefits of that robustness, and then you wouldn't need an additional API call to get the details of the order since you would already have it after the server-side capture and be able to store it atomically. Basically, it seems you really should switch to that instead of this unnecessary hybrid you're trying to do.
But if you insist on doing weird and inadvisable things that nobody recommends, Order IDs like 7KM15574UC313712P weren't created by your server-side code, and so can't be accessed that way. What you can try instead is to look up the successful Capture ID (not Order ID) in the JS, frompurchase_units[0].captures.id
, and pass this actual PayPal transaction ID to your server code instead. Then use it to get the v2/payment capture object, which will use a different API/SDK call -- in your case, this one: https://github.com/paypal/Checkout-PHP-SDK/blob/develop/lib/PayPalCheckoutSdk/Payments/CapturesGetRequest.php
(But again, ideally this whole paragraph would be moot and you would pay more attention to paragraph #1)
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to undefined function illuminate\encryption\openssl_cipher_iv_length()
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
About the technologies asked in this question
PHP
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
CSS
CSS (Cascading Style Sheets) is a formal language for describing the appearance of a document written using a markup language.
It is mainly used as a means of describing, decorating the appearance of web pages written using HTML and XHTML markup languages, but can also be applied to any XML documents, such as SVG or XUL.
https://www.w3.org/TR/CSS/#css
HTML
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to programmierfrage.com
programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.
Get answers to specific questions
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Help Others Solve Their Issues
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.