2

I wanted to make an custom payment method option. Where customer choose the amount to pay. The process is simple Customer fills the form, Whatever amount is in the form get's redirected to payment page. After that it returns to the site back. My code works perfect till the payment gateway page, after that the page isn't getting/ running the success or failure function.

Here's My form's Front end side.

function make_my_payment() {
    if (isset($_POST['formset']) and!empty($_POST['formset'])) {
        require_once dirname(__FILE__).
        '/payu.php';
        date_default_timezone_set('Asia/Kolkata');
        //update the live credentials once done testing.
        /* Payment success logic goes here. */
        function payment_success() {
            //echo "<pre>";
            //print_r($_POST);die;
            global $wpdb;
            $wpdb - > insert('wp_payment_details', array('payment_id' => $_POST['mihpayid'], 'fname' => $_POST['firstname'], 'lname' => $_POST['lastname'], 'email' => $_POST['email'], 'phone' => $_POST['phone'], 'branch' => $_POST['udf1'], 'amount' => $_POST['udf4'], 'total_amount' => $_POST['amount'], 'additional_charge' => $_POST['udf2'], 'notification' => $_POST['udf3'], 'payment_datetime' => time(), 'notification_datetime' => $_POST['udf5'], 'status' => 1, ));
            //send text message
            $text_message = "Thank%20you%20for%20the%20payment%20of%20".$_POST['amount'].
            "%20at%20Nikita%20Jewellers.";
            $ch1 = curl_init();
            curl_setopt($ch1, CURLOPT_URL, "http://makemysms.in/api/sendsms.php?username=NIKITAJWL&password=NIKITA123&sender=NIKITA&mobile=".$_POST['phone'].
                "&type=1&message=".$text_message);
            curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
            $output = curl_exec($ch1);
            curl_close($ch1);
            echo '<h4>You have successfully paid amount '.$_POST['amount'].
            '</h4>';
        }

        function payment_failure() {
            /* Payment failure logic goes here. */
            echo "Payment Failure";
        }
        /* Payments made easy. */
        pay_page(array('key' => '4XKLg4', 'txnid' => uniqid('nikita_'), 'amount' => $_POST['total_amount'], 'firstname' => $_POST['fname'], 'lastname' => $_POST['lname'], 'email' => $_POST['email'], 'phone' => $_POST['phone'], 'udf1' => $_POST['orderScheme'], 'udf2' => $_POST['additional_charge'], 'udf3' => $_POST['send_notification'], 'udf4' => $_POST['amount'], 'udf5' => $_POST['notification_date'], 'productinfo' => 'Nikita Make payment', 'surl' => 'payment_success', 'furl' => 'payment_failure'), 'jeYETVtZ');
        /* And we are done. */
    }
    if (empty($_POST['formset'])) {
        $html. = "<form id='payment_form' method='post'> < table border = '0'
        class = 'make_payment' > <tr>
                        <td style='padding-top: 10px'>
                            <b>Full Name <span style='color:red;'>*</span></b>
                            <input type='text' required name='fname' id='fname' placeholder='First' />
                            <input type='text' required name='lname' id='lname' placeholder='Last' />
                        </td>
                    </tr> < tr > <td>
                            <b>Email <span style='color:red;'>*</span></b>
                            <input type='email' required name='email' id='email' />
                        </td> < /tr> < tr > <td>
                            <b>Mobile <span style='color:red;'>*</span></b>
                            <input type='text' minlength='10' maxlength='10' required name='phone' id='phone' />
                        </td> < /tr> < tr > <td>
                            <b>Scheme / Order No. <span style='color:red;'>*</span><i>(Enter 'none' if you don't have scheme/order no. OR 'new member' if you are starting a new scheme.)</i></b>
                            <input type='text' required name='orderScheme' id='orderScheme' />
                        </td> < /tr> < tr > <td>
                            <b>Mode of Payment <span style='color:red;'>*</span></b>
                            <input  type='radio' required name='additional_charge' id='additional_charge' value = '0'/>Debit 
                            <i>(No charge)</i> <br/>
                            <input  type='radio' required name='additional_charge' id='additional_charge' value = '0'/>Credit + NetBanking <i>(No charge)</i> <br/>
                            <input  type='radio' required name='additional_charge' id='additional_charge' value = '1.90'/>Amex + International Card <i>(1.90% Additional charge)</i>
                        </td> < /tr> < tr > <td>
                            <b>Amount <span style='color:red;'>*</span></b>
                            <input  type='number' required name='amount' id='amount' />
                        </td> < /tr> < tr > <td>
                            <b>Total Amount</b>
                            <input type='text' readonly name='total_amount' id='total_amount' /> <br/>
                            <label>Amount + Service Tax + Transaction Fee</label>
                        </td> < /tr> < tr > <td>
                            <b>Enable SMS Notification</b>
                            <input type='checkbox' name='send_notification' id='send_notification' />
                            <label>For recurring payments only : A reminder sms will be sent on your mobile number to make your next payment. Leave unchecked if making one time payment.</label>
                        </td> < /tr> < tr id = 'notification_row'
        style = 'display:none' > <td>
                            <b>Reminder Date</b>
                            <input type='text' name='notification_date' id='notification_date' />
                        </td> < /tr> < tr > <td style='text-align:center;'>
                            <input type='submit' name='pay_btn' id='pay_btn' value='Pay' style='margin: 0' />
                        </td> < /tr> < input type = 'hidden'
        name = 'formset'
        value = '1' > < /table> < /form>
        ";
        return $html;
    }

The Payment gateway's Payu.php file which was modified for this form so posting exact code which I modified.

public static
function show_page($result) {
    if ($result['status'] === Misc::SUCCESS) {
        //                      header( 'Location:' . $result['data'] );
        //              else
        //                      throw new Exception( $result['data'] );
        //
        //                        echo ($result ['data']);
        $nik = $result['data'];
        echo '<META HTTP-EQUIV="refresh" content="0;URL='.$nik.
        '">';
        echo "<script type='text/javascript'>document.location.href='{$nik}';</script>";
        $result['data'] = $nik;
    } else throw new Exception($result['data']);
    echo($result['data']);
}
public static
function show_reponse($result) {
    if ($result['status'] === Misc::SUCCESS) {
        $result['data']();
        //                     echo "<script type='text/javascript'>alert('$result['data']());</script>";
    } else {
        return $result['data'];
        //                    $nik=$result['data'];
        //                   echo "<script type='text/javascript'>alert('$nik');</script>";
    }
}
}

In the payment Payu file I had to remove the PHP redirection as it was giving error headers already sent So I had to use the javascript for the redirection. So what should I do now so it lands to perfect success/ fail message.

ɢʀᴜɴᴛ
  • 32,025
  • 15
  • 116
  • 110

1 Answers1

0

Headers are sent at the time output starts, so check for any premature output (like spaces before the opening PHP tag, etc). Reenable the PHP redirection and let it do its thing. The key is not to output anything until then. We'd need to see more code to know if there's something we can help with in that page.

Here's a reference from StackOverflow on finding the cause of the headers already sent issue: Link.

Community
  • 1
  • 1
Luke G.
  • 587
  • 1
  • 4
  • 13