1

I would like to put php syntax into the javascript payment completion function.

1. This is a JavaScript code that will run when your online payment is finalized.

<script>
......
function(rsp) {
    if ( rsp.success ) {
        var msg = 'payment success';
        msg += 'Store ID : ' + rsp.imp_uid;
        msg += 'Store deal ID : ' + rsp.merchant_uid;
        msg += 'Price : ' + rsp.paid_amount;
        msg += 'Card Authorization Number : ' + rsp.apply_num;
    } else {
        var msg = 'Billing failed.';
        msg += 'Error reason : ' + rsp.error_msg;
    }
    alert(msg);
}
</script>

2. This is the php code that adds the user's credits once the payment is successfully completed.

<?php $user_wallet->balance =  $user_wallet->balance + $paid_amount;?>

I want to put this code inside script section like this.

if ( rsp.success ) {
<?php $user_wallet->balance =  $user_wallet->balance + $paid_amount;?>
}

But the php syntax does not work in the script section. How do I solve this problem?

dlsl
  • 31
  • 3

2 Answers2

1

Perhaps you need to write the php variable to the js function, something like:

<script>
    function(rsp) {
        if ( rsp.success ) {
            <?php 
                $user_wallet->balance =  $user_wallet->balance + $paid_amount;
            ?>
            var msg = 'payment success';
            msg += 'Store ID : ' + rsp.imp_uid;
            msg += 'Store deal ID : ' + rsp.merchant_uid;
            msg += 'Price : ' + rsp.paid_amount;
            msg += 'Card Authorization Number : ' + rsp.apply_num;
            msg += 'Balance: <?php echo {$user_wallet->balance}; ?>';
        } else {
            var msg = 'Billing failed.';
            msg += 'Error reason : ' + rsp.error_msg;
        }
        alert(msg);
    }
</script>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
0

You need to understand that all PHP code in the requested page will run first, then whatever it produces (HTML, CSS, Javascript and all the client side stuff) is send back to client. When client receives the response is when Javascript can run. So your if statement in Javascript actually runs after PHP code. One solution to your problem is to use Ajax to request the PHP code to run. This will work for you.