Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
140
rated 0 times [  143] [ 3]  / answers: 1 / hits: 22551  / 11 Years ago, tue, december 31, 2013, 12:00:00

My goal: To process three objectives:
(1) Make insertion in a staging table before processing payment.
(2) Go to third party payment processing site like paypal using my form action attribute.
(3) Read the $_POST data from the payment landing page to determine if payment was successful and then take the data from the staging table and insert into real table.



My desired implementation would sth like:




<form name=demo method=post onsubmit=<?php insertStagingData() ?>       action=3rdpartyPaymentProcessingLink> 
<input name =Submit typesubmit valueSubmit>
</form>



I know that the web browser executes the onsubmit handler and when the handler completes, the browser proceeds with the form submit. I have used this concept for java script client side validation before forwarding the form to web server for php processing.



I dont know if this can be done when both handlers require server side processing, one on my webserver and the second on a 3rd party site.



Thanks in advance for suggestions and help!


More From » php

 Answers
131

The theory of a no-js solution would be something like this:



<form name=demo method=post action=/scriptOnYourServer.php> 
<input name =Submit typesubmit valueSubmit>
</form>


And then completely within scriptOnYourServer.php you would do any validation you want first, then do the staging table insert, then use the 3rd party payment PHP API (or restful service or soap service - whatever is available) to process payment - which you should then get a success/fail from, and you either display errors/offer retry or move from staging to a real table - all within this one request to your script.



If your goal is to avoid writing payment API integration code in PHP, then your basic outline will only work with javascript/AJAX if you must perform server side actions and still have the form action go to the payment processor, and typically a payment vendor that offers a form post solution will allow a return URL they can send the success/fail to where you do further processing which I think you are describing in your step 3.



So that is more like:



    <form name=demo method=post onsubmit=jsFunctionName() action=3rdpartyPaymentProcessingLink> 
<input name =Submit typesubmit valueSubmit>
</form>
<script type=text/javascript>
function jsFunctionName() {
// js ajax code, probably easiest to use a lib like jQuery for this
// You tell it the url on your server you will post vars to / generate a success/fail response, probably in json is best.
// based on your servers insert into staging table success/fail json response you decide whether to proceed or not (allow the submit to 3rd party), because you would not want to proceed if your staging table insert failed I presume
// so this implies you have a way to display errors in JS on this page, or you will be redirecting to a script of yours which would reload the page but display any errors - or you just completely ignore the possibility your staging data insert could fail and let the payment processor deal with it
}
</script>


See http://api.jquery.com/jQuery.ajax/



So with this a few moving parts -
your script - insertStagingData.php - it's job is to read the post data, you should do validation of some sort, then insert into your staging database (make sure you are using parametrized queries or sanitizing data since you are dumping user data into a database), then it generates a json response like:



{success:true}


or



{success:false}


And you need to have PHP use a JSON header - so at the end of this script you need to do something like this:



$response = array(success => true); // or false depending on if your db insert was successful
header(HTTP/1.1 200 OK);
header('Content-type: application/json');
echo json_encode($response);


Then after that in the script block above in jsFunctionName, you will read the success variable from your json response and either display an error and return false or use preventDefault() to stop the submit to the 3rd party script, or if it is a success you allow the form submit to post.



Then you can setup a separate script for the payment processor to post back to where their docs will tell you what data they will post to you, and based on their success/fail you can display an error, or success and then move the data to the real table.



You may also want to consider the staging/real table should be the SAME table, with a column like payment_success = 0 or 1 where it is 0 by default and if the payment processor posts back success, you just update the record to payment_success=1. There are very few good reasons this should be a completely separate table.



This should hopefully help point you in the right direction. Good luck


[#73463] Monday, December 30, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
harrisa

Total Points: 514
Total Questions: 93
Total Answers: 93

Location: Ghana
Member since Sun, Mar 27, 2022
2 Years ago
;