How can I pass PHP variables value to Client Code for Paypal












0















I am trying to integrate Paypal with my website and facing issues with this. The problem is that I cannot seem to find a way to send the PHP variables value, that is the TOTAL AMOUNT to Paypal code



Suppose I have a variable



$finalAmount = 220;


This value is dynamic and will change with each user. I need to pass it below inside the Paypal code



<script>

paypal.Button.render({
// Configure environment
env: 'sandbox', // sandbox | production

// PayPal Client IDs - replace with your own
// Create a PayPal app: https://developer.paypal.com/developer/applications/create
client: {
sandbox: 'CODE_HERE',
// production: 'demo_production_client_id'
},
// Customize button (optional)
locale: 'en_US',
style: {
layout: 'vertical', // horizontal | vertical
size: 'medium', // medium | large | responsive
shape: 'rect', // pill | rect
color: 'gold'
},
// Set up a payment
payment: function (data, actions) {
return actions.payment.create({
transactions: [{
amount: {
total: '1',
currency: 'USD'
},
invoice_number : 123111241, // invoice must be unique for each transaction.
item_list: {
items: [
{
name: 'hat',
description: 'Brown hat.',
quantity: '2',
price: '3',
sku: '1',
currency: 'INR'
},
{
name: 'handbag',
description: 'Black handbag.',
quantity: '1',
price: '7',
sku: 'product34',
currency: 'INR'
}
],
shipping_phone_number : '8888888888',
shipping_address: {
recipient_name: 'Logan loga',
line1: '4th Floor',
line2: 'Unit #34',
city: 'Chennai',
country_code: 'IN',
postal_code: '600113',
phone: '9999999999',
state: 'Tamil Nadu'
}
}
}],
payer: {
payment_method: "paypal",
payer_info : {
email: 'test@gmail.com',
first_name :'Logan',
last_name :'loga',
billing_address : {
line1: '4th Floor',
line2: 'Unit #34',
city: 'Chennai',
country_code: 'IN',
postal_code: '600114',
phone: '7777777777',
state: 'Tamil Nadu'
}
}
},
});
},
// Execute the payment
onAuthorize: function (data, actions) {
return actions.payment.execute()
.then(function (res2) {
// Show a confirmation message to the buyer
console.log(res2);
console.log(res2.transactions[0].related_resources[0].sale.state);
//state codes - completed, partially_refunded, pending, refunded, denied
if(res2.transactions[0].related_resources[0].sale.state == 'completed'){
alert("Payment received for the invoice number" + res2.transactions[0].invoice_number);

}
});
},
onCancel: function (data, actions) {
// Show a cancel page or return to cart
alert("Payment cancelled");
},
onError: function (err) {
// Show an error page here, when an error occurs
alert("Payment error");
}
}, '#paypal-button');
</script>


I want to pass the value of $finalAmount to the PayPal script at



amount: {
total: 'HERE'
}


How can I implement this?










share|improve this question























  • Remember that javascript is client side. If you have the amount to be paid in your javascript then anyone with a little web development experience can edit this amount and pay way less (ore more?) than they should.

    – Dirk Scholten
    Nov 20 '18 at 12:17











  • @DirkScholten any tips you'd like to give to me to avoid this thing?

    – DigitalDecoder
    Nov 20 '18 at 12:33











  • I'm not sure what the exact flow of your website is. But at some point you will have to tell paypal about the payment. At this point you should use PHP to query your database for the price. If you get this value from your javascript there will always be a chance that a user has edited the value.

    – Dirk Scholten
    Nov 20 '18 at 13:02











  • Is it just me or is the whole Client Integration scheme horribly insecure? All the credentials, order details, callback URLs, etc, are sitting there vulnerable to be edited, replayed, etc... Is there an example anywhere of a real-world implementation of client side integration that is secure?

    – Chris Nadovich
    Nov 20 '18 at 20:36
















0















I am trying to integrate Paypal with my website and facing issues with this. The problem is that I cannot seem to find a way to send the PHP variables value, that is the TOTAL AMOUNT to Paypal code



Suppose I have a variable



$finalAmount = 220;


This value is dynamic and will change with each user. I need to pass it below inside the Paypal code



<script>

paypal.Button.render({
// Configure environment
env: 'sandbox', // sandbox | production

// PayPal Client IDs - replace with your own
// Create a PayPal app: https://developer.paypal.com/developer/applications/create
client: {
sandbox: 'CODE_HERE',
// production: 'demo_production_client_id'
},
// Customize button (optional)
locale: 'en_US',
style: {
layout: 'vertical', // horizontal | vertical
size: 'medium', // medium | large | responsive
shape: 'rect', // pill | rect
color: 'gold'
},
// Set up a payment
payment: function (data, actions) {
return actions.payment.create({
transactions: [{
amount: {
total: '1',
currency: 'USD'
},
invoice_number : 123111241, // invoice must be unique for each transaction.
item_list: {
items: [
{
name: 'hat',
description: 'Brown hat.',
quantity: '2',
price: '3',
sku: '1',
currency: 'INR'
},
{
name: 'handbag',
description: 'Black handbag.',
quantity: '1',
price: '7',
sku: 'product34',
currency: 'INR'
}
],
shipping_phone_number : '8888888888',
shipping_address: {
recipient_name: 'Logan loga',
line1: '4th Floor',
line2: 'Unit #34',
city: 'Chennai',
country_code: 'IN',
postal_code: '600113',
phone: '9999999999',
state: 'Tamil Nadu'
}
}
}],
payer: {
payment_method: "paypal",
payer_info : {
email: 'test@gmail.com',
first_name :'Logan',
last_name :'loga',
billing_address : {
line1: '4th Floor',
line2: 'Unit #34',
city: 'Chennai',
country_code: 'IN',
postal_code: '600114',
phone: '7777777777',
state: 'Tamil Nadu'
}
}
},
});
},
// Execute the payment
onAuthorize: function (data, actions) {
return actions.payment.execute()
.then(function (res2) {
// Show a confirmation message to the buyer
console.log(res2);
console.log(res2.transactions[0].related_resources[0].sale.state);
//state codes - completed, partially_refunded, pending, refunded, denied
if(res2.transactions[0].related_resources[0].sale.state == 'completed'){
alert("Payment received for the invoice number" + res2.transactions[0].invoice_number);

}
});
},
onCancel: function (data, actions) {
// Show a cancel page or return to cart
alert("Payment cancelled");
},
onError: function (err) {
// Show an error page here, when an error occurs
alert("Payment error");
}
}, '#paypal-button');
</script>


I want to pass the value of $finalAmount to the PayPal script at



amount: {
total: 'HERE'
}


How can I implement this?










share|improve this question























  • Remember that javascript is client side. If you have the amount to be paid in your javascript then anyone with a little web development experience can edit this amount and pay way less (ore more?) than they should.

    – Dirk Scholten
    Nov 20 '18 at 12:17











  • @DirkScholten any tips you'd like to give to me to avoid this thing?

    – DigitalDecoder
    Nov 20 '18 at 12:33











  • I'm not sure what the exact flow of your website is. But at some point you will have to tell paypal about the payment. At this point you should use PHP to query your database for the price. If you get this value from your javascript there will always be a chance that a user has edited the value.

    – Dirk Scholten
    Nov 20 '18 at 13:02











  • Is it just me or is the whole Client Integration scheme horribly insecure? All the credentials, order details, callback URLs, etc, are sitting there vulnerable to be edited, replayed, etc... Is there an example anywhere of a real-world implementation of client side integration that is secure?

    – Chris Nadovich
    Nov 20 '18 at 20:36














0












0








0








I am trying to integrate Paypal with my website and facing issues with this. The problem is that I cannot seem to find a way to send the PHP variables value, that is the TOTAL AMOUNT to Paypal code



Suppose I have a variable



$finalAmount = 220;


This value is dynamic and will change with each user. I need to pass it below inside the Paypal code



<script>

paypal.Button.render({
// Configure environment
env: 'sandbox', // sandbox | production

// PayPal Client IDs - replace with your own
// Create a PayPal app: https://developer.paypal.com/developer/applications/create
client: {
sandbox: 'CODE_HERE',
// production: 'demo_production_client_id'
},
// Customize button (optional)
locale: 'en_US',
style: {
layout: 'vertical', // horizontal | vertical
size: 'medium', // medium | large | responsive
shape: 'rect', // pill | rect
color: 'gold'
},
// Set up a payment
payment: function (data, actions) {
return actions.payment.create({
transactions: [{
amount: {
total: '1',
currency: 'USD'
},
invoice_number : 123111241, // invoice must be unique for each transaction.
item_list: {
items: [
{
name: 'hat',
description: 'Brown hat.',
quantity: '2',
price: '3',
sku: '1',
currency: 'INR'
},
{
name: 'handbag',
description: 'Black handbag.',
quantity: '1',
price: '7',
sku: 'product34',
currency: 'INR'
}
],
shipping_phone_number : '8888888888',
shipping_address: {
recipient_name: 'Logan loga',
line1: '4th Floor',
line2: 'Unit #34',
city: 'Chennai',
country_code: 'IN',
postal_code: '600113',
phone: '9999999999',
state: 'Tamil Nadu'
}
}
}],
payer: {
payment_method: "paypal",
payer_info : {
email: 'test@gmail.com',
first_name :'Logan',
last_name :'loga',
billing_address : {
line1: '4th Floor',
line2: 'Unit #34',
city: 'Chennai',
country_code: 'IN',
postal_code: '600114',
phone: '7777777777',
state: 'Tamil Nadu'
}
}
},
});
},
// Execute the payment
onAuthorize: function (data, actions) {
return actions.payment.execute()
.then(function (res2) {
// Show a confirmation message to the buyer
console.log(res2);
console.log(res2.transactions[0].related_resources[0].sale.state);
//state codes - completed, partially_refunded, pending, refunded, denied
if(res2.transactions[0].related_resources[0].sale.state == 'completed'){
alert("Payment received for the invoice number" + res2.transactions[0].invoice_number);

}
});
},
onCancel: function (data, actions) {
// Show a cancel page or return to cart
alert("Payment cancelled");
},
onError: function (err) {
// Show an error page here, when an error occurs
alert("Payment error");
}
}, '#paypal-button');
</script>


I want to pass the value of $finalAmount to the PayPal script at



amount: {
total: 'HERE'
}


How can I implement this?










share|improve this question














I am trying to integrate Paypal with my website and facing issues with this. The problem is that I cannot seem to find a way to send the PHP variables value, that is the TOTAL AMOUNT to Paypal code



Suppose I have a variable



$finalAmount = 220;


This value is dynamic and will change with each user. I need to pass it below inside the Paypal code



<script>

paypal.Button.render({
// Configure environment
env: 'sandbox', // sandbox | production

// PayPal Client IDs - replace with your own
// Create a PayPal app: https://developer.paypal.com/developer/applications/create
client: {
sandbox: 'CODE_HERE',
// production: 'demo_production_client_id'
},
// Customize button (optional)
locale: 'en_US',
style: {
layout: 'vertical', // horizontal | vertical
size: 'medium', // medium | large | responsive
shape: 'rect', // pill | rect
color: 'gold'
},
// Set up a payment
payment: function (data, actions) {
return actions.payment.create({
transactions: [{
amount: {
total: '1',
currency: 'USD'
},
invoice_number : 123111241, // invoice must be unique for each transaction.
item_list: {
items: [
{
name: 'hat',
description: 'Brown hat.',
quantity: '2',
price: '3',
sku: '1',
currency: 'INR'
},
{
name: 'handbag',
description: 'Black handbag.',
quantity: '1',
price: '7',
sku: 'product34',
currency: 'INR'
}
],
shipping_phone_number : '8888888888',
shipping_address: {
recipient_name: 'Logan loga',
line1: '4th Floor',
line2: 'Unit #34',
city: 'Chennai',
country_code: 'IN',
postal_code: '600113',
phone: '9999999999',
state: 'Tamil Nadu'
}
}
}],
payer: {
payment_method: "paypal",
payer_info : {
email: 'test@gmail.com',
first_name :'Logan',
last_name :'loga',
billing_address : {
line1: '4th Floor',
line2: 'Unit #34',
city: 'Chennai',
country_code: 'IN',
postal_code: '600114',
phone: '7777777777',
state: 'Tamil Nadu'
}
}
},
});
},
// Execute the payment
onAuthorize: function (data, actions) {
return actions.payment.execute()
.then(function (res2) {
// Show a confirmation message to the buyer
console.log(res2);
console.log(res2.transactions[0].related_resources[0].sale.state);
//state codes - completed, partially_refunded, pending, refunded, denied
if(res2.transactions[0].related_resources[0].sale.state == 'completed'){
alert("Payment received for the invoice number" + res2.transactions[0].invoice_number);

}
});
},
onCancel: function (data, actions) {
// Show a cancel page or return to cart
alert("Payment cancelled");
},
onError: function (err) {
// Show an error page here, when an error occurs
alert("Payment error");
}
}, '#paypal-button');
</script>


I want to pass the value of $finalAmount to the PayPal script at



amount: {
total: 'HERE'
}


How can I implement this?







javascript php paypal






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 12:07









DigitalDecoderDigitalDecoder

102




102













  • Remember that javascript is client side. If you have the amount to be paid in your javascript then anyone with a little web development experience can edit this amount and pay way less (ore more?) than they should.

    – Dirk Scholten
    Nov 20 '18 at 12:17











  • @DirkScholten any tips you'd like to give to me to avoid this thing?

    – DigitalDecoder
    Nov 20 '18 at 12:33











  • I'm not sure what the exact flow of your website is. But at some point you will have to tell paypal about the payment. At this point you should use PHP to query your database for the price. If you get this value from your javascript there will always be a chance that a user has edited the value.

    – Dirk Scholten
    Nov 20 '18 at 13:02











  • Is it just me or is the whole Client Integration scheme horribly insecure? All the credentials, order details, callback URLs, etc, are sitting there vulnerable to be edited, replayed, etc... Is there an example anywhere of a real-world implementation of client side integration that is secure?

    – Chris Nadovich
    Nov 20 '18 at 20:36



















  • Remember that javascript is client side. If you have the amount to be paid in your javascript then anyone with a little web development experience can edit this amount and pay way less (ore more?) than they should.

    – Dirk Scholten
    Nov 20 '18 at 12:17











  • @DirkScholten any tips you'd like to give to me to avoid this thing?

    – DigitalDecoder
    Nov 20 '18 at 12:33











  • I'm not sure what the exact flow of your website is. But at some point you will have to tell paypal about the payment. At this point you should use PHP to query your database for the price. If you get this value from your javascript there will always be a chance that a user has edited the value.

    – Dirk Scholten
    Nov 20 '18 at 13:02











  • Is it just me or is the whole Client Integration scheme horribly insecure? All the credentials, order details, callback URLs, etc, are sitting there vulnerable to be edited, replayed, etc... Is there an example anywhere of a real-world implementation of client side integration that is secure?

    – Chris Nadovich
    Nov 20 '18 at 20:36

















Remember that javascript is client side. If you have the amount to be paid in your javascript then anyone with a little web development experience can edit this amount and pay way less (ore more?) than they should.

– Dirk Scholten
Nov 20 '18 at 12:17





Remember that javascript is client side. If you have the amount to be paid in your javascript then anyone with a little web development experience can edit this amount and pay way less (ore more?) than they should.

– Dirk Scholten
Nov 20 '18 at 12:17













@DirkScholten any tips you'd like to give to me to avoid this thing?

– DigitalDecoder
Nov 20 '18 at 12:33





@DirkScholten any tips you'd like to give to me to avoid this thing?

– DigitalDecoder
Nov 20 '18 at 12:33













I'm not sure what the exact flow of your website is. But at some point you will have to tell paypal about the payment. At this point you should use PHP to query your database for the price. If you get this value from your javascript there will always be a chance that a user has edited the value.

– Dirk Scholten
Nov 20 '18 at 13:02





I'm not sure what the exact flow of your website is. But at some point you will have to tell paypal about the payment. At this point you should use PHP to query your database for the price. If you get this value from your javascript there will always be a chance that a user has edited the value.

– Dirk Scholten
Nov 20 '18 at 13:02













Is it just me or is the whole Client Integration scheme horribly insecure? All the credentials, order details, callback URLs, etc, are sitting there vulnerable to be edited, replayed, etc... Is there an example anywhere of a real-world implementation of client side integration that is secure?

– Chris Nadovich
Nov 20 '18 at 20:36





Is it just me or is the whole Client Integration scheme horribly insecure? All the credentials, order details, callback URLs, etc, are sitting there vulnerable to be edited, replayed, etc... Is there an example anywhere of a real-world implementation of client side integration that is secure?

– Chris Nadovich
Nov 20 '18 at 20:36












2 Answers
2






active

oldest

votes


















0














You Can use...
amount: {
total : '<?php echo $finalAmount ?>'
}






share|improve this answer
























  • i tried this, its not working. This was my first try

    – DigitalDecoder
    Nov 20 '18 at 12:12





















0














create a new hidden input field with a unique id and get the value of that field in js. for example



In your PHP Page :



<input type="hidden" id="finalAmount" value="<?php echo $finalAmount ?>">



In Your js code :



var finalAmount = $('#finalAmount').val();


Hopefully, you will get the amount you are looking for :)






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392680%2fhow-can-i-pass-php-variables-value-to-client-code-for-paypal%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You Can use...
    amount: {
    total : '<?php echo $finalAmount ?>'
    }






    share|improve this answer
























    • i tried this, its not working. This was my first try

      – DigitalDecoder
      Nov 20 '18 at 12:12


















    0














    You Can use...
    amount: {
    total : '<?php echo $finalAmount ?>'
    }






    share|improve this answer
























    • i tried this, its not working. This was my first try

      – DigitalDecoder
      Nov 20 '18 at 12:12
















    0












    0








    0







    You Can use...
    amount: {
    total : '<?php echo $finalAmount ?>'
    }






    share|improve this answer













    You Can use...
    amount: {
    total : '<?php echo $finalAmount ?>'
    }







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 20 '18 at 12:10









    OneJeetOneJeet

    962310




    962310













    • i tried this, its not working. This was my first try

      – DigitalDecoder
      Nov 20 '18 at 12:12





















    • i tried this, its not working. This was my first try

      – DigitalDecoder
      Nov 20 '18 at 12:12



















    i tried this, its not working. This was my first try

    – DigitalDecoder
    Nov 20 '18 at 12:12







    i tried this, its not working. This was my first try

    – DigitalDecoder
    Nov 20 '18 at 12:12















    0














    create a new hidden input field with a unique id and get the value of that field in js. for example



    In your PHP Page :



    <input type="hidden" id="finalAmount" value="<?php echo $finalAmount ?>">



    In Your js code :



    var finalAmount = $('#finalAmount').val();


    Hopefully, you will get the amount you are looking for :)






    share|improve this answer




























      0














      create a new hidden input field with a unique id and get the value of that field in js. for example



      In your PHP Page :



      <input type="hidden" id="finalAmount" value="<?php echo $finalAmount ?>">



      In Your js code :



      var finalAmount = $('#finalAmount').val();


      Hopefully, you will get the amount you are looking for :)






      share|improve this answer


























        0












        0








        0







        create a new hidden input field with a unique id and get the value of that field in js. for example



        In your PHP Page :



        <input type="hidden" id="finalAmount" value="<?php echo $finalAmount ?>">



        In Your js code :



        var finalAmount = $('#finalAmount').val();


        Hopefully, you will get the amount you are looking for :)






        share|improve this answer













        create a new hidden input field with a unique id and get the value of that field in js. for example



        In your PHP Page :



        <input type="hidden" id="finalAmount" value="<?php echo $finalAmount ?>">



        In Your js code :



        var finalAmount = $('#finalAmount').val();


        Hopefully, you will get the amount you are looking for :)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 12:30









        Ravi RoshanRavi Roshan

        527310




        527310






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392680%2fhow-can-i-pass-php-variables-value-to-client-code-for-paypal%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            How to fix TextFormField cause rebuild widget in Flutter