How to ammend paypal smart button to include vat (tax) - javascript

I have a Paypal generated smart button that I want to amend so that vat is broken down correctly on the paypal receipt.
I've read the paypal pages that say it cant be done in the paypal interface but have to be called through on the api.
The code I current have is...
<div id="paypal-button-container"></div>
<script src="https://www.paypal.com/sdk/js?client-id=xxxzzz&currency=GBP" data-sdk-integration-source="button-factory"></script>
<script>
paypal.Buttons({
style: {
shape: 'rect',
color: 'gold',
layout: 'vertical',
label: 'paypal',
},
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
description: "my amazing rocketpack",
amount: {
value: '2.99'
}
}]
});
}, onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
alert('Shop transaction completed by ' + details.payer.name.given_name + '!');
});
}
}).render('#paypal-button-container');
</script>
Looking at the paypal docs for purchase units it suggests I need to turn on amount.breakdown and specify item_total and tax_total.
But I cant seem to get it right (it either breaks the button so it doesn't display at all or the receipt is still just gross amount) and can find no examples of the code

Solved, in case anyone else wants to know, the below is how you initiate amount.breakdown (there is no dot)
purchase_units: [{
description: "my item",
amount: {
value: '29.99',
currency_code: 'GBP',
breakdown: {
item_total: {
currency_code: 'GBP',
value: '24.99'
},
tax_total: {
currency_code: 'GBP',
value: '5.00'
},
}
}
}]

Related

Paypal api status pending in payment capture

Everything is fine in the PayPal sandbox but in the live environment, there is an issue with transactions.
My js :
<script src="https://www.paypal.com/sdk/js?client-id=key&currency=GBP"></script>
createOrder: (data, actions) => {
console.log("create order");
return actions.order.create({
purchase_units: [{
amount: {
value: 10.00
}
}]
});
},
onApprove: (data, actions) => {
console.log("on approve");
return actions.order.capture().then(function(orderData) {
const transaction = orderData.purchase_units[0].payments.captures[0];
alert(${transaction.status}: ${transaction.id});
});
}
}).render('#paypal-button-container');
This is my API response issue:
"payments": {
"captures": [
{
"amount": {
"currency_code": "GBP",
"value": "1.00"
},
"seller_protection": {
"dispute_categories": [
"ITEM_NOT_RECEIVED",
"UNAUTHORIZED_TRANSACTION"
],
"status": "ELIGIBLE"
},
"status": "PENDING",
"status_details": {
"reason": "PENDING_REVIEW"
}
}
]
},
the amount is received. but it's going to hold because of this status pending. but the PayPal team provides this as the solution " As for pending payments that would be a setting on your shopping cart, the setting would need to be set as 'sale' instead of 'Authorisation' " how do set this?
the setting would need to be set as 'sale' instead of 'Authorisation' "
This is not your issue, this is already a capture. The reason it is pending is given as "pending review", which basically means what it says. PayPal will review and either approve or deny the transaction, usually within 24 hours.

PayPal Checkout Braintree Line Items

I'm trying to use Braintree with the PayPal Checkout SDK (sandbox environment) however everytime I try and add in line items, I get a 'create_order_error' 'PayPal error undefined: could not initialise paypal flow'. This is only when I add in the lineItems option:
...
return paypalCheckoutInstance.createPayment({
flow: 'checkout',
amount: '100',
currency: 'USD',
intent: 'capture',
displayName: 'Display Test',
landingPageType: 'login',
lineItems: [
{
quantity: '1',
unitAmount: '50.00',
name: 'Test Name',
kind: 'debit'
},
{
quanity: '1',
unitAmount: '50.00',
name: 'Test Name 2',
kind: 'debit'
}
],
enableShippingAddress: true,
...
If I remove the lineItems property or set
...
lineItems: []
...
I get no error, so presumably its the way I'm formatting the items.
Can anyone assist?
Thank you!
Misspelt 'quantity' in line item 2 as pointed out by #Reynadan in comments. Silly mistake, don't know how I didn't spot it! Many thanks to all.

How to retrieve the seller_receivable_breakdown values?

I need help with finding a way to get the seller_receivable_breakdown values (gross_amount, paypal_fee, net_amount) from Paypal v2 Api in the sandbox
Here is my code:
onApprove: function (data, actions) {
return actions.order.capture().then(function (details) {
alert('paypal fees: ' + details.purchase_units[0].payments.captures[0].seller_receivable_breakdown.paypal_fee.value);
});
},
I followed the documentation:
https://developer.paypal.com/docs/api/payments/v2#captures_get
And I managed to get other fields such as:
details.purchase_units[0].payments.captures[0].status
details.purchase_units[0].payments.captures[0].id
but I couldn't get the seller_receivable_breakdown values. any tips would be greatly appreciated
From the shared log:
purchase_units: Array(1) 0: amount: {value: "100.99", currency_code: "SEK"} payee: {email_address: "blablabla#gmail.com", merchant_id: "D8AOYHUX2L75G"} payments: captures: Array(1) 0: amount: {value: "100.99", currency_code: "SEK"} create_time: "2020-11-24T08:56:04Z" final_capture: true id: "1VE71745JL288960Y" links: (3) [{…}, {…}, {…}] seller_protection: {status: "NOT_ELIGIBLE"} status: "COMPLETED" update_time: "2020-11-24T08:56:04Z"
Note payee: {email_address: "blablabla#gmail.com"
There is no breakdown because no PayPal transaction exists yet, this order is completed but was sent to a payee email with no account, and so the payment has not (yet) been accepted.
If the payment is not accepted within 30 days, it will be returned to the sender.

Why will PayPal Smart buttons not recognise my items array?

I have integrated the PayPal smart buttons into my JavaScript shopping cart. I am trying to allow PayPal to tell the user the items they had in the cart at checkout. For example;
I know that to do this I need to use the following code:
<script>
paypal.Buttons({
createOrder: function(data, actions) {
// This function sets up the details of the transaction, including the amount and line item details.
return actions.order.create({
"purchase_units": [{
"description": "Stuff",
"amount": {
"value": "20.00",
"currency_code": "USD",
"breakdown": {
"item_total": {
"currency_code": "USD",
"value": "20.00"
},
}
},
"items": [
{
"unit_amount": {
"currency_code": "USD",
"value": "10.00"
},
"quantity": "1",
"name": "Item 1",
},
{
"unit_amount": {
"currency_code": "USD",
"value": "10.00"
},
"quantity": "1",
"name": "Item 2",
},
],
}
]
});
},
onApprove: function(data, actions) {
// This function captures the funds from the transaction.
return actions.order.capture().then(function(details) {
// This function shows a transaction success message to your buyer.
window.location.href = "orderConfirmed.php"
clearCart()
});
}
}).render('#paypal-button-container');
//This function displays Smart Payment Buttons on your web page.
</script>
In this example, there are 2 items in the dropdown tab: Item 1 and Item 2 but I need these to represent what the user has in their cart. I got an answer on her than said I needed to create amn array that held the cart item name, price and quantity.
I came up with this code, what I am trying to do is for every item in the cart, I want to return the product name, product price and product quantity. I came up with the code below:
function arrayOfItems() {
cart.forEach((cartItem, index) => {
let currency = cartItem.price;
let quantity = cartItem.quantity;
let itemName = cartItem.name;
let items = [{"unit_amount": {"currency_code": "USD","value": currency},"quantity": quantity,"name": itemName,}];
return items;
});
}
But when I run the new PayPal script like so:
<script src="cart.js"></script>
<script>
paypal.Buttons({
createOrder: function(data, actions) {
// This function sets up the details of the transaction, including the amount and line item details.
return actions.order.create({
purchase_units: [{
amount: {
value: countCartTotal()
},
items: [
{
arrayOfItems()
},
],
}
]
});
},
onApprove: function(data, actions) {
// This function captures the funds from the transaction.
return actions.order.capture().then(function(details) {
// This function shows a transaction success message to your buyer.
window.location.href = "orderConfirmed.php"
clearCart()
});
}
}).render('#paypal-button-container');
//This function displays Smart Payment Buttons on your web page.
</script>
The PayPal buttons stop working!
Update
After making the changes the code now looks like this:
<script>
paypal.Buttons({
createOrder: function(data, actions) {
// This function sets up the details of the transaction, including the amount and line item details.
return actions.order.create({
"purchase_units": [{
"amount": {
"value": countCartTotal(),
"currency_code": "USD",
"breakdown": {
"item_total": {
"currency_code": "USD",
"value": countCartTotal()
},
},
"items": arrayOfItems()
}
]
});
},
onApprove: function(data, actions) {
// This function captures the funds from the transaction.
return actions.order.capture().then(function(details) {
// This function shows a transaction success message to your buyer.
window.location.href = "orderConfirmed.php"
clearCart()
});
}
}).render('#paypal-button-container');
//This function displays Smart Payment Buttons on your web page.
</script>
And is not producing any errors in my IDE, however when I run the code the JavaScript Console gives me this error:
You don't seem to be including the required breakdown. This may seem redundant, but it is required to have a breakdown section.
"breakdown": {
"item_total": {
"value": countCartTotal()
},
}
Also it looks like you are generating an items array, so you need to use it like so:
amount: {
value: countCartTotal()
},
items: arrayOfItems(),
All of the currency_code fields also seem to be required, and not optional when passing line item information, so you will need to add those back in.
That's three issues you need to fix.
If you still need help, post a runtime example of what everything is evaluating to, i.e. the output of the functions so we can tell you what's wrong

Paypal integration issue in titanium

I have to implement Paypal into my application. Here I have added some products into the cart. If I click the cart, I have to pay the amount using Paypal.
When clicking the Paypal button, I am getting the following error:
The amounts specified for item price,tax,and shipping do not add up to the total amount.
Why am getting this error?
I am using the following code:
$.paypalWindow.addEventListener('open', function(){
var Paypal = require('ti.paypal');
var u = Ti.Android != undefined ? 'dp' : 0;
var status = Ti.UI.createLabel({ top: 20 + u, height: 50 + u, color: '#333', text: 'Loading, please wait...' });
$.paypalWindow.add(status); var price = totalamount;
var invoiceitemslist = JSON.stringify(data);
var button; function addButtonToWindow() {
if (button) { $.paypalWindow.remove(button); button = null; }
button = Paypal.createPaypalButton({ width: 194 + u, height: 37 + u, buttonStyle: Paypal.BUTTON_194x37, top: 20 + u,
language: 'en_US',
appID: 'APP-80W284485P519543T',
paypalEnvironment: Paypal.PAYPAL_ENV_SANDBOX,
feePaidByReceiver: false,
enableShipping: false,
payment: {
paymentType: Paypal.PAYMENT_TYPE_BUSINESS,
subtotal: price,
tax: 0.00,
shipping: 0.00,
currency: 'USD',
recipient: 'thaibusiness#gmail.com',
customID: 'anythingYouWant',
invoiceItems:
[{"name":"Bajaj 200 mm Ultima PT01 Personal Fan","totalPrice":2997,"itemPrice":999,"itemCount":3},
],
ipnUrl: 'http://www.appcelerator.com/',
merchantName: 'EYMOBINS Insurance',
memo: 'For the insurance with EYMOBINS!'
}
});
button.addEventListener('paymentCancelled', function (e) {
alert('Payment cancelled. Please try again!');
addButtonToWindow();
});
button.addEventListener('paymentSuccess', function (e) {
alert('Payment successfull. Please get your Policy No.!'+" "+e.transactionID);
$.paypalWindow.remove(button);
//addButtonToWindow();
});
button.addEventListener('paymentError', function (e) {
alert('Payment Error. Please try again!');
addButtonToWindow();
});
button.addEventListener('buttonDisplayed', function () {
$.paypalWindow.remove(status);
//alert('Please pay '+Ti.App.totalcost+'$ with Paypal!')
});
button.addEventListener('buttonError', function () {
});
$.paypalWindow.add(button);
}
addButtonToWindow();
});
$.paypalWindow.open();
Please check the code and give me an idea to resolve the above issue.
EDIT:
Here I am facing one issue:
[{"name":"Bajaj 200 mm Ultima PT01 Personal Fan","totalPrice":999,"itemPrice":999,"itemCount":1},{"name":"Average2Excellent CBSE KG EVS MATHS ENG Educational CD ROMS","totalPrice":547,"itemPrice":547,"itemCount":1}]
Here the totalamount is 1546. Here I have printed the invoiceitems in the console, and I am getting the data like above.
So that I have given like:
invoiceItems:invoiceitems,
Like means am getting the issue (the amounts specified for item price, tax, and shipping do not add up to the total amount).
Same thing I have written the code like:
invoiceItems:[{"name":"Bajaj 200 mm Ultima PT01 Personal Fan","totalPrice":999,"itemPrice":999,"itemCount":1},{"name":"Average2Excellent CBSE KG EVS MATHS ENG Educational CD ROMS","totalPrice":547,"itemPrice":547,"itemCount":1}]
it's working perfectly.
It is not working dynamically when assigned the value. Can you please check the code and help?
EDIT:
If i have tried to run this code on android device, as am clicking the paypal button nothing happends. Why the login form is not open in the android device.
Why are you escaping the quotes in this code?
[{"name":"Bajaj 200 mm Ultima PT01 Personal Fan","totalPrice":2997,"itemPrice":999,"itemCount":3}
This is not valid JSON (or Javascript for that matter). Is this required by Paypal? I think it should look like this:
[{"name":"Bajaj 200 mm Ultima PT01 Personal Fan","totalPrice":2997,"itemPrice":999,"itemCount":3}
Edit:
I've had a closer look at your code sample. Can you confirm where totatamount is coming from? As the code sample stands it will be undefined, which would be consistent with the error message you have described. Can you confirm it is definitely the correct value IMMEDIATELY before you submit this data?
The ti.paypal spec has a nice code sample in the README file, which gives a good example of how to setup createPaypalButton. I would recommend looking at this if you haven't already.
button = Paypal.createPaypalButton({
// NOTE: height/width only determine the size of the view that the button is embedded in - the actual button size
// is determined by the buttonStyle property!
width: 194 + u, height: 37 + u,
buttonStyle: Paypal.BUTTON_194x37, // The style & size of the button
bottom: 50 + u,
language: 'en_US',
textStyle: Paypal.PAYPAL_TEXT_DONATE, // Causes the button's text to change from "Pay" to "Donate"
appID: '<<<YOUR APP ID HERE>>>', // The appID issued by Paypal for your application; for testing, feel free to delete this property entirely.
paypalEnvironment: Paypal.PAYPAL_ENV_SANDBOX, // Sandbox, None or Live
feePaidByReceiver: false, // This will only be applied when the transaction type is Personal
enableShipping: false, // Whether or not to select/send shipping information
advancedPayment: { // The payment itself
payments: [
{
isPrimary: true, // Mark this as the primary vendor; this marks this as a chain payment.
merchantName: 'Primary Vendor',
paymentType: Paypal.PAYMENT_TYPE_SERVICE, // The type of payment
paymentSubtype: Paypal.PAYMENT_SUBTYPE_DONATIONS, // The subtype of the payment; you must be authorized for this by Paypal!
subtotal: 13, // The total cost of the order, excluding tax and shipping
tax: 0,
shipping: 0,
recipient: '<<<YOUR RECIPIENT HERE>>>',
customID: 'anythingYouWant',
invoiceItems: [
{ name: 'Shoes', totalPrice: 8, itemPrice: 2, itemCount: 4 },
{ name: 'Hats', totalPrice: 2, itemPrice: 0.5, itemCount: 4 },
{ name: 'Coats', totalPrice: 3, itemPrice: 1, itemCount: 3 }
]
},
{
merchantName: 'Vendor 1',
paymentType: Paypal.PAYMENT_TYPE_SERVICE, // The type of payment
paymentSubtype: Paypal.PAYMENT_SUBTYPE_DONATIONS, // The subtype of the payment; you must be authorized for this by Paypal!
subtotal: 10, // The total cost of the order, excluding tax and shipping
tax: 0,
shipping: 0,
recipient: '<<<YOUR RECIPIENT HERE>>>',
customID: 'anythingYouWant',
invoiceItems: [
{ name: 'Shoes', totalPrice: 8, itemPrice: 2, itemCount: 4 },
{ name: 'Hats', totalPrice: 2, itemPrice: 0.5, itemCount: 4 }
]
},
{
merchantName: 'Vendor 2',
paymentType: Paypal.PAYMENT_TYPE_SERVICE, // The type of payment
paymentSubtype: Paypal.PAYMENT_SUBTYPE_DONATIONS, // The subtype of the payment; you must be authorized for this by Paypal!
subtotal: 3, // The total cost of the order, excluding tax and shipping
tax: 0,
shipping: 0,
recipient: '<<<YOUR RECIPIENT HERE>>>',
customID: 'anythingYouWant',
invoiceItems: [
{ name: 'Coats', totalPrice: 3, itemPrice: 1, itemCount: 3 }
]
}
],
ipnUrl: 'http://www.appcelerator.com/',
currency: 'USD',
memo: 'For the orphans and widows in the world!'
}
});

Categories