PayPal Smart Buttons Custom amount - paypal

i'm having a bit of an issue with some code regarding the paypal smart button i'm no code expert i dabble a bit but could do with some help.
I have the button set up and all is working but i would like to make it so the customer can enter the amount they wish to pay but everything i have tried has failed so far.
for example if customer A owes £25 they can visit the pay section enter £25 and then click the nice new fancy paypal button.
Been looking for like 3 days now and hitting dead ends im no coder i know a bit but not enough to work out the issue.
<script src="https://www.paypal.com/sdk/js?client-id=xxxxxxx&currency=GBP&commit=true">
</script>
<div id="paypal-button-container"></div>
<script>
paypal.Buttons({
createOrder: function(data, actions) {
// Set up the transaction
return actions.order.create({
purchase_units: [{
amount: {
value: '10'
}
}]
});
}
}).render('#paypal-button-container')
</script>

You have to pass the amount dynamically to PayPal Create order function.
Added a input field and passed amount to function - in addition to this you can check for negative and non 0 amount as well by adding checks to input fields.
Try the below basic code , i hope that this will solve the issue
<script src="https://www.paypal.com/sdk/js?client-id=xxxxxxx&currency=GBP&commit=true">
</script>
<label>Enter amount : </label><input name="amount" type="text" id="amount" />
<!-- Set up a container element for the button -->
<div id="paypal-button-container"></div>
<script>
paypal.Buttons({
createOrder: function(data, actions) {
var amt = document.getElementById("amount").value;
// Set up the transaction
return actions.order.create({
purchase_units: [{
amount: {
value: amt
}
}]
});
}
}).render('#paypal-button-container')
</script>

Building on the example by PayPal_vidya, I used the following code that worked with the latest PayPal smart button code:
<div id="paypal-button-container">
<label>Enter amount : </label><input name="amount" type="text" id="amount" />
</div>
<script src="https://www.paypal.com/sdk/js?client-id=sb&currency=USD" data-sdk-integration-source="button-factory"></script>
<script>
paypal.Buttons({
style: {
shape: 'rect',
color: 'gold',
layout: 'vertical',
label: 'paypal',
},
createOrder: function(data, actions) {
amt = document.getElementById("amount").value;
return actions.order.create({
purchase_units: [{
amount: {
value: amt
}
}]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
alert('Transaction completed by ' + details.payer.name.given_name + '!');
});
}
}).render('#paypal-button-container');
</script>

Related

Venmo not displaying with PayPal JS SDK Smart Buttons

I'm trying to implement PayPal and Venmo on my scratch Laravel project. The result should be like this:
However when I integrated it, it only shows these buttons:
Is there any way to display that Venmo button?
Here's the code, &enable-funding=venmo is included
<div class="container">
<div class="row">
<div class="col-md-8">
</div>
<div class="col-md-4">
<div id="smart-button-container" style="margin-top: 5em;">
<div >
<div id="paypal-button-container"></div>
</div>
</div>
</div>
</div>
</div>
<script src="https://www.paypal.com/sdk/js?client-id={{env('APP_CLIENT_ID')}}&enable-funding=venmo&currency=USD" data-sdk-integration-source="button-factory"></script>
<script>
function initPayPalButton() {
paypal.Buttons({
style: {
shape: 'rect',
color: 'gold',
layout: 'vertical',
label: 'paypal',
},
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [
{
"amount":
{
"currency_code":"USD",
"value":1
}
}]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(orderData) {
console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
const element = document.getElementById('paypal-button-container');
element.innerHTML = '';
element.innerHTML = '<h3>Thank you for your payment!</h3>';
});
},
onError: function(err) {
console.log(err);
}
}).render('#paypal-button-container');
}
initPayPalButton();
</script>
Reference: https://www.paypal.com/merchantapps/appcenter/acceptpayments/checkout
&enable-funding=venmo&currency=USD
You already are doing the right things with that on the SDK line.
However, Venmo will only appear to a US IP; for sandbox mode, you can simulate what a US buyer will see with &buyer-country=US
(Don't add buyer-country with a live client ID, as the buttons will not load -- only works for sandbox)

Transitioning from the old PayPal button to the PayPal Javascript SDK button

I have used the plain paypal button like this with no problems:
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="xxx#xxx.net">
<input type="hidden" name="item_name" value="Website Donation">
<input type="hidden" name="item_number" value="Donation">
<input type="hidden" name="amount" value="">
<input type="submit" value="Send Donation via PayPal" ><br></form>
and moved to this style to get Venmo and Credit card options:
<div id="smart-button-container">
<div style="text-align: center;">
<div id="paypal-button-container"></div>
</div>
</div>
<script src="https://www.paypal.com/sdk/js?client-id=secret&enable-funding=venmo&currency=USD" data-sdk-integration-source="button-factory"></script>
<script>
function initPayPalButton() {
paypal.Buttons({
style: {
shape: 'pill',
color: 'gold',
layout: 'vertical',
label: 'paypal',
},
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{"description":"Site Donation","amount":{"currency_code":"USD","value":1.99}}]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(orderData) {
// Full available details
console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
// Show a success message within this page, e.g.
const element = document.getElementById('paypal-button-container');
element.innerHTML = '';
element.innerHTML = '<h3>Thank you for your payment!</h3>';
// Or go to another URL: actions.redirect('thank_you.html');
});
},
onError: function(err) {
console.log(err);
}
}).render('#paypal-button-container');
}
initPayPalButton();
</script>
I have a IPN page to receive the order information from the original button and it checks the item_name and item_number field from that.
My questions is, will the description (Website Donation) from the createOrder section translate to those same fields or will the description be a new field (what is it called)?
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{"description":"Site Donation","amount":{"currency_code":"USD","value":1.99}}]
});
},
In one of your sandbox Business accounts, set a sandbox IPN listener URL.
Create a REST app for that particular sandbox business account, and copy its client ID. Use that client ID to create a sandbox test payment, using a sandbox Personal account to approve the payment.
Log the data from the sandbox IPN you receive. It will have the available fields for that transaction.
I guess I could have tested it, but anyhow. I created a 1.99 butt and had someone use it. The "description" used in the new button came through with the field name of &transaction_subject=Site Donation&.

How to make Paypal button invoke "live" functionality?

Following the code from https://www.paypal.com/buttons, I follow the "Smart Buttons" link, generate and copy the code, so I add the following to our payments page (with our actual clientID instead of the XXXXXX....):
<div id="smart-button-container">
<div style="text-align: center;">
<div id="paypal-button-container"></div>
</div>
</div>
<script src="https://www.paypal.com/sdk/js?client-id=XXXXXXXXXXXXXXXXX&currency=USD" data-sdk-integration-source="button-factory"></script>
<script>
function initPayPalButton() {
paypal.Buttons({
style: {
shape: 'rect',
color: 'gold',
layout: 'vertical',
label: 'paypal',
},
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{"description":"Subscription to our service","amount":{"currency_code":"USD","value":10}}]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
alert('Transaction completed by ' + details.payer.name.given_name + '!');
});
},
onError: function(err) {
console.log(err);
}
}).render('#paypal-button-container');
}
initPayPalButton();
</script>
The button seems to work, in that it opens the Paypal payment window; but the payment does not seem to take effect.
I then notice what I believe would be the reason: the URL of the Paypal payment window reads https://www.sandbox.paypal.com/checkoutnow .....
Question: how do I set up the code to enable the "live" functionality?
You are using a Sandbox Client ID.
Obtain your Live Client ID from the "Live" tab of https://www.paypal.com/signin?intent=developer&returnUri=https%3A%2F%2Fdeveloper.paypal.com%2Fdeveloper%2Fapplications

Rendering Multiple PayPal Payment Buttons on a Page

I'm using PayPal JS SDK payment buttons where all you do is copy the code into your HTML site and the PayPal buttons appear to initiate a checkout.
This works for only one button on the page, but I have 3 subscriptions a user can choose from (daily, weekly, monthly)
If I copy the code into my daily div area it works correctly, but then if I copy the code into the weekly div it wont appear and only the daily div PayPal button appears. But if I remove the code from the daily div the button will appear under the weekly div.
It seems I can only use button code once?
I tried modifying the code by changing the id name and function names but still no luck.
Is there a way to have multiple PayPal on my page?
Here is the generated code from PayPal:
<div id="smart-button-container">
<div style="text-align: center;">
<div id="paypal-button-container"></div>
</div>
</div>
<script src="https://www.paypal.com/sdk/js?client-id=test&currency=AUD" data-sdk-integration-source="button-factory"></script>
<script>
function initPayPalButton() {
paypal.Buttons({
style: {
shape: 'rect',
color: 'gold',
layout: 'vertical',
label: 'paypal',
},
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{"description":"daily","amount":{"currency_code":"AUD","value":1}}]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
alert('Transaction completed by ' + details.payer.name.given_name + '!');
});
},
onError: function(err) {
console.log(err);
}
}).render('#paypal-button-container');
}
initPayPalButton();
</script>
<br><br><br>
<p> gefwdsa</p>
<div id="smart-button-container">
<div style="text-align: center;">
<div id="paypal-button-container"></div>
</div>
</div>
<script src="https://www.paypal.com/sdk/js?client-id=sb&currency=AUD" data-sdk-integration-source="button-factory"></script>
<script>
function initPayPalButton() {
paypal.Buttons({
style: {
shape: 'rect',
color: 'gold',
layout: 'vertical',
label: 'paypal',
},
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{"description":"weekly sub","amount":{"currency_code":"AUD","value":5}}]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
alert('Transaction completed by ' + details.payer.name.given_name + '!');
});
},
onError: function(err) {
console.log(err);
}
}).render('#paypal-button-container');
}
initPayPalButton();
</script>
This only makes the first button appear
The SDK script,
<script src="https://www.paypal.com/sdk/js?client-id=sb&currency=AUD" data-sdk-integration-source="button-factory"></script>
Should only be loaded exactly once per page, above all the buttons.
Additionally, each button must render to a div with a unique HTML id, so this line:
<div id="paypal-button-container"></div>
And this line:
}).render('#paypal-button-container');
Both need some corresponding different/additional string suffix for each button, e.g. paypal-button-container-1 , -2 for the next buttons' etc.
Also client-id of test or sb is sandbox mode, you need a live client-id from the 'Live' section of an app in https://www.paypal.com/signin?intent=developer&returnUri=https%3A%2F%2Fdeveloper.paypal.com%2Fdeveloper%2Fapplications

paypal IPN and paynow button

I have a paypal button with the code in a file name paypaltest.php:
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="cmd" value="_s-xclick">
<!--<input type="hidden" name="custom" value="<?php //echo $id ?>">-->
<input type="image" src="https://www.sandbox.paypal.com/en_US/i/btn/btn_paynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.sandbox.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
I also have a paypal ipn php file name listener.php in the same location on my web hosting server directory.
I want to know how would i link the two to know if a transaction was Verified or not. Should i replace the sandbox url on the form with the listener.php file.
I can help you with single page PayPal pay now button
actually I use this one of my projects
<script src="https://www.paypal.com/sdk/js?client-id=XXXXXXXXXXXXXXXXX">
</script>
<div id="paypal-button-container"> </div>
<script>
paypal.Buttons({
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: '1230'
}
}]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
alert('Transaction completed');
// Call your server to save the transaction
return fetch('codes/paypalapi.php?invo=123', {
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
orderID: data.orderID,
amount: data.amount
})
});
});
}
}).render('#paypal-button-container');
</script>