0

I uses this from to submit some information to '2co' as a payment method some of this information I need to validate it by PHP server-side and in the same time send a notification to the admin by mail to tell him that someone has been paid.

now this is the form.

<form method="post" action="https://sandbox.2checkout.com/checkout/purchase">
                            <div class="body">

                                <div class="half f_right">
                                    <label for="card_holder_name" class="width_100per"><span class="fontRed2">*</span>card_holder_name
                                        </label>
                                    <input type="text" name="card_holder_name" id="card_holder_name"
                                           class="form_textarea_rec"/>
                                </div>

                                <div class="half">
                                    <label for="country" class="width_100per"><span class="fontRed2">*</span>country</label>
                                    <select name="country" id="country" class="form_select_rec">
                                        <option value="EGY" title="Egypt">Egypt</option>
                                        <option value="SaudiArabia" title="saudi Arabia">Any other</option>
                                    </select>
                                </div>

                                <div class="half">
                                    <label for="street_address" class="width_100per"><span class="fontRed2">* </span>street_address </label>
                                    <textarea name="street_address" id="street_address"
                                              class="form_textarea_rec height_50px"></textarea>
                                </div>


                                <div class="half">
                                    <label for="street_address2" class="width_100per"> address2</label>
                                    <textarea name="street_address2" id="street_address2"
                                              class="form_textarea_rec height_50px"></textarea>
                                </div>


                                <div class="half f_right">
                                    <label for="city" class="width_100per"><span class="fontRed2">* city</span>
                                        </label>
                                    <input id="city" type="text" name="city" class="form_textarea_rec"/>
                                </div>


                                <div class="half f_right">
                                    <label for="state" class="width_100per"><span class="fontRed2">*</span>
                                        States</label>
                                    <input id="state" type="text" name="state" class="form_textarea_rec"/>
                                </div>


                                <div class="half">
                                    <label for="email" class="width_100per"> <span class="fontRed2">*</span>Email</label>
                                    <input id="email" type="text" name="email" class="form_textarea_rec"/>
                                </div>


                                <div class="half">
                                    <label for="zip" class="width_100per"> <span class="fontRed2">*</span>Zip</label>
                                    <input id="zip" type="text" name="zip" class="form_textarea_rec"/>
                                </div>

                                <div class="half">
                                    <label for="phone" class="width_100per"> <span class="fontRed2">*</span>
                                        Phone</label>
                                    <input id="phone" type="text" name="phone" class="form_textarea_rec"/>
                                </div>
                            </div>

                            <?php
                            $fullName = @$_POST['card_holder_name'];
                            ?>
                            <input type='hidden' name='sid' value='*****'/>
                            <input type='hidden' name='mode' value='2CO'/>
                            <input type='hidden' name='li_0_type' value='product'/>
                            <input type='hidden' name='li_0_name' value='<?php echo $planName ?> '/>
                            <input type='hidden' name='li_0_price' value='<?php echo $planPrice ?> '/>
                            <input type='hidden' name='card_holder_name' value='<?php echo $fullName ?>'/>
                            <input type='hidden' name='street_address' value='<?php @$_POST['street_address']; ?>'/>
                            <input type='hidden' name='street_address2' value='<?php @$_POST['street_address2']; ?>'/>
                            <input type='hidden' name='city' value='<?php @$_POST['city']; ?>'/>
                            <input type='hidden' name='state' value='<?php @$_POST['state']; ?>'/>
                            <input type='hidden' name='zip' value='<?php @$_POST['zip']; ?>'/>
                            <input type='hidden' name='country' value='<?php @$_POST['country']; ?>'/>
                            <input type='hidden' name='email' value='<?php @$_POST['email']; ?>'/>

                            <div class="footer">
                                <input name="submit" type="submit" value="Continue" class="s_btn"/>
                            </div>
                        </form>

now I need to validate this fields and send the form and I need to send notes to the admin

I know about make it like this

<?php
if(isset($_POST['submit'])){
//remove the action from the form
//write the validation here ...
//and then send the mail 

//but how to post all this in the end to '2co'??
}
?>
Yousef Altaf
  • 2,631
  • 4
  • 46
  • 71

3 Answers3

1

You could use curl to finally post it to 2co http://php.net/manual/en/book.curl.php

Sending mail is as simple as using mail function http://php.net/manual/en/function.mail.php

<?php
if(isset($_POST['submit'])){
   //Do validation
   //send mail
   //post to 2co via curl 


}
?>

As said in this question there are non curl ways to do so How do I send a POST request with PHP?

Community
  • 1
  • 1
aWebDeveloper
  • 36,687
  • 39
  • 170
  • 242
1

you are asking the wrong question, you should have asked "how to post data to another server using php?" there are many answers here are some :

How do I send a POST request with PHP?

http://www.lornajane.net/posts/2010/three-ways-to-make-a-post-request-from-php

Community
  • 1
  • 1
Exlord
  • 5,009
  • 4
  • 31
  • 51
0

You need to do it by JavaScript on page load event.

Changes you need to do:

  1. change action to same php page OR different AND check submit action by if(isset($_POST['submit'])) as you said.
  2. validate all data, send mail, set php var $validated=true else false AND set some session variable which you should validate to confirm, on response from payment server. Ex: payment_id, user_id | these should also be sent to payment gateway and gateway returns on response, in your case I think sid
  3. if($validated) echo the JavaScript code to create a hidden form with all information in hidden input field and submit form automatically on page load event.
  4. on response from payment server, which will come on your specific php page validate session/user info.

If you are using jquery the autosubmit code will be like following:

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- if you are not using jQuery and want to then use above code --> 

<?php if($validated) { ?> <script type="text/javascript">
    $(function(){
        var form = $('<form></form>');
        form.attr("method", "post");
        form.attr("action", link);
        form.attr("style","display: none");

        var field = $('<input />');
        field.attr("type", "hidden");
        field.attr("name", "post_var1");
        field.attr("value", "val_1");
        form.append(field); //echo values by php <?php echo $value1; ?> like:

        var field1 = $('<input />');
        field1.attr("type", "hidden");
        field1.attr("name", "li_0_name");
        field1.attr("value", "<?php echo $planName ?>");
        form.append(field1); 

        //do same for all fields

        $(document.body).append(form);
        form.submit();
     }
 </script> <?php  } ?>

For non jQuery (Pure JavaScript) code you can search OR ask me i'll edit the answer.

Adarsh Rajput
  • 1,246
  • 14
  • 24
  • 1
    @Exlord: I know. If OP only want send info to `2co` then cUrl is ok. But I think, its payment gateway integration. **user need to be redirected on Payment Server also**. It can be only done by JavaScript automatically. **AND** carefully reed my answer. I m validating SEVER side and Submitting on Page load. If you disable JavaScript OR alter it at client side, it will not go to payment server, and payment cant be done Coz it was encrypted by key from payment gateway. **IF not then your answer is correct, and mine will help others** – Adarsh Rajput Dec 28 '14 at 09:36
  • I am try to work on your example but there is something not clear here "submit form automatically on page load event" by the way I did everything as you said but still not working it just return the form without any actions. can you please clear this for me – Yousef Altaf Dec 29 '14 at 12:11
  • @YousefAltaf sorry for late... the code I given is a JavaScript code which needs to run on page load. **See logic:** user opens ur page 'xxx.php' ->fill form & submit ->action is set to 'xxx.php' OR '' so `if(isset($_POST['submit'])){ /*code to validate,email, and set $validated=true; */}` will execute & if validated then JavaScript code will echo ->'xxx.php' page again load & it should not show to user again so we need JavaScript to auto submit ->so running it on page load by jQuery's `$(); //runs on page load` method. **I think ur JavaScript is not running automatically** are u using jQuery? – Adarsh Rajput Dec 30 '14 at 17:00
  • thanks for asking I thought u forgot everything about me. Yes I can use jQuery no problem I already try one but the problem was it's freezing the page on error. I am now trying to just validate the fields before submit but no luck. – Yousef Altaf Dec 31 '14 at 13:58
  • @YousefAltaf What error are you getting (when page freeze)?? I didn'tt got where you stuck?? Using jquery will be more easy coz, plain javascript code is complicated. – Adarsh Rajput Dec 31 '14 at 14:05
  • sorry for late ... I don't get any errors on the page it just freeze when I get a missing filed ... I need to know how they validate they form in this [2co](https://www.2checkout.com/demo#/billing) – Yousef Altaf Jan 04 '15 at 08:57