When I try to pay with PayPal an error occurs and the button does not work, this is the JavaScript code:
$('.paypal-button-row').on('click', function(event) { var account_id = $('#account_id').text(); var email = $('input[name="email"]').val(); var sesid = $('input[name="sesid"]').val(); $.ajax({ url: 'ajax.php?func=create_order', data: { 'account_id': account_id, 'email': email, 'sesid': sesid }, dataType: 'text', type: 'post', success: function(response) { if (response == "success") { console.log(success); $(this).off("submit").trigger("submit"); } } }); });
this is the button:
<div id="smart-button-container"> <div style="text-align: center;"> <div id="paypal-button-container"></div> </div> </div>
This is php code of button:
public function create_order() { $sesid = $_POST["sesid"]; $account_id=(int) $_POST["account_id"]; print_r($account_id); $user_id =(string) $_COOKIE['user_id']; echo $user_id; $accountsell = $this->query_executor("SELECT * FROM accountsell WHERE status=? AND account_id=?","ii",array(1,$account_id))->fetch_assoc(); $accountsell_id = (int) $accountsell["id"]; echo '-'.$accountsell_id; $email = $_POST["email"]; echo "<br>".$email; $this->query_executor("INSERT INTO ordered_accounts VALUES(NULL,?,?,?,?,?,0,NOW())","siiss",array($sesid,$accountsell_id,$account_id,$user_id,$email)); var_dump(array($sesid,$accountsell_id,$account_id,$user_id,$email)); $_SESSION["sesid"] = $sesid; echo "success"; }
It is gaming site, Can this be corrected?
Advertisement
Answer
$('.paypal-button-row').on('click'...
Nothing about this makes any sense (it’s completely wrong to be adding your own click handler to the row), so see a correct sample here instead: https://developer.paypal.com/demo/checkout/#/pattern/server
If you need to pass additional parameters in your create call, add a body object to the fetch.
You’ll need two corresponding routes on your server that return their own JSON, one for that first ‘Create an order’ and one for the ‘Capture order’, documented here.
When the capture order is successful, immediately write whatever you need into your database before returning the JSON.