How to change total according to item quantity - node.js

I've got a paypal payment at my site. Looks like this:
app.post('/pay', (req, res) => {
console.log(req.body);
const create_payment_json = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "http://localhost:1234/success",
"cancel_url": "http://localhost:1234/cancel"
},
"transactions": [{
"item_list": {
"items": [{
"name": req.body.user_name,
"sku": "001",
"price": "25",
"currency": "USD",
"quantity": req.body.persons_count
}]
},
"amount": {
"currency": "USD",
"total": "25"
},
"description": req.body.user_name + " with email " + req.body.email + " just ordered " + req.body.persons_count + " places"
}]
};
paypal.payment.create(create_payment_json, function (error, payment) {
if (error) {
throw error;
} else {
res.send('on my way');
console.log(payment);
}
});
})
If I change total field in amount object (this is what I wanna do) I get a 400 response (bad request). How can I make a payment like this:
"amount":{
"total": req.body.persons_count * 2
}
Where req.body.persons_count variable is a variable I get from a post request from one of the forms I have earlier.
Fighting with that code showed, that both price and total values have to be equal however I want the price of a single item be different from the total amount I wanna get. Huge thanks!
By the way quantity value has to be equal to 1. In all other cases app crahes.

Total amount should be sum of items amount, shipping amount, tax and other fees.
In your case, total : Item price * Item quantity
sample,
"transactions": [
{
"amount": {
"currency": "USD",
"total": "20",
"details": {
"shipping": "1.20",
"tax": "1.30",
"subtotal": "17.50"
}
},
"description": "Payment description",
"invoice_number": "1231123524",
"custom": "custom data",
"item_list": {
"items": [
{
"name": "Ground Coffee 40 oz",
"currency": "USD",
"quantity": 1,
"price": "7.50"
},
{
"name": "Granola bars",
"currency": "USD",
"quantity": 5,
"price": "2"
}
]
}
}
]
You can also refer https://developer.paypal.com/docs/api/payments/v1/#payment_create

Related

Orders are not displayed at merchant console

I have developed an app in nodejs where I have integrated the Mastercard payment method and it was working fine till Friday. But now when I send money from MasterCard to the merchant account it gives the success response but there is nothing on the merchant console. here is my response
"paymentInfoList": [
{
"url": "https://test-gateway.mastercard.com/api/rest/version/57/merchant/merchant/order/order-1598868168410/transaction/transaction-1598868195864",
"mthd": "PUT",
"payload": {
"apiOperation": "CAPTURE",
"transaction": {
"amount": "1144",
"currency": "USD",
"reference": 1598868195864
}
},
"resbody": {
"authorizationResponse": {
"cardSecurityCodeError": "M",
"commercialCardIndicator": "1",
"date": "0831",
"financialNetworkCode": "MCC",
"posData": "1025104006600",
"posEntryMode": "812",
"processingCode": "000000",
"responseCode": "00",
"stan": "204684",
"time": "100313",
"transactionIdentifier": "0114GR"
},
"gatewayEntryPoint": "WEB_SERVICES_API",
"merchant": "GTB123456D00",
"order": {
"amount": 1144,
"certainty": "FINAL",
"chargeback": {
"amount": 0,
"currency": "USD"
},
"creationTime": "2020-08-31T10:03:13.631Z",
"currency": "USD",
"id": "order-1598868168410",
"lastUpdatedTime": "2020-08-31T10:03:33.156Z",
"merchantAmount": 1144,
"merchantCategoryCode": "5399",
"merchantCurrency": "USD",
"reference": "1598868168410",
"status": "CAPTURED",
"totalAuthorizedAmount": 1144,
"totalCapturedAmount": 1144,
"totalRefundedAmount": 0
},
"response": {
"acquirerCode": "00",
"acquirerMessage": "Approved",
"cardSecurityCode": {
"acquirerCode": "M",
"gatewayCode": "MATCH"
},
"gatewayCode": "APPROVED"
},
"result": "SUCCESS",
"sourceOfFunds": {
"provided": {
"card": {
"brand": "MASTERCARD",
"expiry": {
"month": "9",
"year": "21"
},
"fundingMethod": "CREDIT",
"issuer": "AFRILAND FIRST BANK",
"number": "512345xxxxxx0008",
"scheme": "MASTERCARD",
"storedOnFile": "NOT_STORED"
}
},
"type": "CARD"
},
"timeOfLastUpdate": "2020-08-31T10:03:33.156Z",
"timeOfRecord": "2020-08-31T10:03:33.064Z",
"transaction": {
"acquirer": {
"batch": 20200831,
"date": "0831",
"id": "GTB_S2I",
"merchantId": "mymerchant",
"settlementDate": "2020-08-31",
"timeZone": "+0100",
"transactionId": "0114GR"
},
"amount": 1144,
"authorizationCode": "010602",
"currency": "USD",
"id": "transaction-1598868195864",
"receipt": "024410204684",
"reference": "1598868195864",
"source": "INTERNET",
"stan": "205744",
"terminal": "GTBS2I04",
"type": "CAPTURE"
},
"version": "57"
}
}
]
now you can see the response is success but it doesnot display on merchant panel/console
It was the issue of the merchant panel/console. It didn't display the orders the same day but from the next day it was working fine and all the previous orders were there, and new orders were also showing on the console/panel So it was their server problem.

Inserting a value via a variable in json using Node.js

I have to write a specific value using a variable in json. Below is the code.
Instead of price, I need to write a variable that contains the price, can someone instruct how to do this.
app.post('/pay', (req, res) => {
const create_payment_json = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "http://localhost:3000/success",
"cancel_url": "http://localhost:3000/cancel"
},
"transactions": [{
"item_list": {
"items": [{
"name": "Red Sox Hat",
"sku": "001",
"price": "25.00",
"currency": "USD",
"quantity": 1
}]
},
"amount": {
"currency": "USD",
"total": "25.00"
},
"description": "Hat for the best team ever"
}]
};
You can use a variable for any value you want.
var thePrice = "25.00";
...
"amount": {
"currency": "USD",
"total": thePrice
},

Error: Response Status: 400 at PayPal Rest SDK

I created a platform with the help of NodeJS with some PayPal Payments, it all worked fine until now and I don't know why it's throwing me this error. 1 hour Ago I did a payment now it won't let me. I haven't modified any code or something like that.]
Here's the error
/Users/calinonaca/Desktop/PEDIGREE-NOTES copy/controllers/feed.js:863
throw error;
^
Error: Response Status : 400
at IncomingMessage. (/Users/calinonaca/Desktop/PEDIGREE-NOTES
copy/node_modules/paypal-rest-sdk/lib/client.js:130:23)
at IncomingMessage.emit (events.js:203:15)
at endReadableNT (_stream_readable.js:1145:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
And here's my function
exports.postPayment = (req,res,next) => {
const uploadedBy = req.body.noteOwner;
const finalprice = req.body.finalprice;
const notename = req.body.notename;
const loggedInUser = req.session.user._id;
const noteprice = req.body.noteprice;
const create_payment_json = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "http://localhost/successPayment/"+finalprice+"/"+notename+"/"+uploadedBy+"/"+noteprice+"/"+loggedInUser,
"cancel_url": "http://localhost/cancel"
},
"transactions": [{
"item_list": {
"items": [{
"name": "Note",
"sku": "001",
"price": finalprice,
"currency": "USD",
"quantity": 1
}]
},
"amount": {
"currency": "USD",
"total": finalprice
},
"description": "Note you ordered"
}]
};
paypal.payment.create(create_payment_json, function (error, payment) {
if (error) {
throw error;
} else {
for(let i = 0; i < payment.links.length; i++)
{
if(payment.links[i].rel === 'approval_url')
{
res.redirect(payment.links[i].href);
}
}
}
});
}
It all worked fine until now and I don't know what is wrong, can you please help me? Thank you!
i hope this will help, i had the same problem and i had to dig almost all possible links and resources, what i found is that you have to map EXACTLY SAME OBJECT AS HERE
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"transactions": [
{
"amount": {
"total": "30.11",
"currency": "USD",
"details": {
"subtotal": "30.00",
"tax": "0.07",
"shipping": "0.03",
"handling_fee": "1.00",
"shipping_discount": "-1.00",
"insurance": "0.01"
}
},
"description": "The payment transaction description.",
"custom": "EBAY_EMS_90048630024435",
"invoice_number": "48787589673",
"payment_options": {
"allowed_payment_method": "INSTANT_FUNDING_SOURCE"
},
"soft_descriptor": "ECHI5786786",
"item_list": {
"items": [
{
"name": "hat",
"description": "Brown hat.",
"quantity": "5",
"price": "3",
"tax": "0.01",
"sku": "1",
"currency": "USD"
},
{
"name": "handbag",
"description": "Black handbag.",
"quantity": "1",
"price": "15",
"tax": "0.02",
"sku": "product34",
"currency": "USD"
}
],
"shipping_address": {
"recipient_name": "Brian Robinson",
"line1": "4th Floor",
"line2": "Unit #34",
"city": "San Jose",
"country_code": "US",
"postal_code": "95131",
"phone": "011862212345678",
"state": "CA"
}
}
}
],
"note_to_payer": "Contact us for any questions on your order.",
"redirect_urls": {
"return_url": "https://example.com/return",
"cancel_url": "https://example.com/cancel"
}
Only after i copied and pasted this object i got a successful response with redirect urls. I hope this will help you. I will play around with object to see if there is anything to neglect.

getting variable amount in execute_payment_json

I want to integrate Paypal in my site for which I am using Paypal-rest-sdk.
I want the amount in execute_payment_json to be equal to the amount that the user inputs.
paypal community has a thread on it but its not answered.
Check it here
The code that i run after getting the value from user
app.post("/paypal-pay",(req,res) => {
console.log(req.body.amount);
const create_payment_json = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "http://localhost:3000/success",
"cancel_url": "http://localhost:3000/cancel"
},
"transactions": [{
"item_list": {
"items": [{
"name": "Donation",
"sku": "item",
"price": req.body.amount,
"currency": "USD",
"quantity": 1
}]
},
"amount": {
"currency": "USD",
"total": req.body.amount
},
"description": "The Donation that you are doing"
}]
};
paypal.payment.create(create_payment_json, function (error, payment) {
if (error) {
throw error;
} else {
console.log(payment);
payment.links.forEach(url => {
if(url.rel === "approval_url"){
res.redirect(url.href);
}
});
}
});
})
After success the above code redirects me to /success and in this i want the total to be equal to the value provided.
app.get("/success",(req,res)=>{
console.log(req.body);
const payerId = req.query.PayerID;
const paymentId = req.query.paymentId;
const execute_payment_json = {
"payer_id" : payerId,
"transactions": [{
"amount": {
"currency": "USD",
// I want this total to be equal to the payment provided by user.
"total": "1.00"
}
}]
}

NodeJS Nested group aggregation with MongoDB

I have a data set of payments that I want to group together to show on page in a report form. Below is the data:-
{
"_id": {
"$oid": "5bed91bfbbcf4d0014ec3ab1"
},
"type": "Card",
"reason": "Deposit",
"item": "Dress",
"amount": 100,
},
{
"_id": {
"$oid": "5bed91bfbbcf4d0014ec3ab2"
},
"type": "Cash",
"reason": "Deposit",
"item": "Dress",
"amount": 400,
},
{
"_id": {
"$oid": "5bed91bfbbcf4d0014ec3ab3"
},
"type": "Cash",
"reason": "Deposit",
"item": "Dress",
"amount": 100,
},
{
"_id": {
"$oid": "5bed91bfbbcf4d0014ec3ab4"
},
"type": "Cash",
"reason": "Full Balance",
"item": "Dress",
"amount": 200,
},
{
"_id": {
"$oid": "5bed91bfbbcf4d0014ec3ab5"
},
"type": "Card",
"reason": "Full Balance",
"item": "Dress",
"amount": 300,
},
{
"_id": {
"$oid": "5bed91bfbbcf4d0014ec3ab6"
},
"type": "Cash",
"reason": "Deposit",
"item": "Jacket",
"amount": 400,
},
{
"_id": {
"$oid": "5bed91bfbbcf4d0014ec3ab7"
},
"type": "Card",
"reason": "Deposit",
"item": "Jacket",
"amount": 150,
},
{
"_id": {
"$oid": "5bed91bfbbcf4d0014ec3ab8"
},
"type": "Cash",
"reason": "Full Balance",
"item": "Jacket",
"amount": 250,
},
{
"_id": {
"$oid": "5bed91bfbbcf4d0014ec3ab9"
},
"type": "Card",
"reason": "Full Balance",
"item": "Jacket",
"amount": 350,
}
I can group the data by 1 item without problem, so for example I can group and total the amounts by item (Dress, Jacket etc.)
I would like to group by all three fields so would get an output that looks like this and structured so I can display them on the Frontend.
{
Dress: {
reason: {
"Deposit" : {
type: {
"Cash": 500 //Combination of 2 records for example
"Card": 100
}
}
"Full Balance" : {
type: {
"Cash": 200
"Card": 300
}
}
}
}
Jacket: {
reason: {
"Deposit" : {
type: {
"Cash": 400
"Card": 150
}
}
"Full Balance" : {
type: {
"Cash": 250
"Card": 350
}
}
}
}
I attempted to start this but I am struggling to get the concept right to be able to produce the right data. Obviously this doesn't produce the right output.
$group: {
_id: {item: "$item"},
totalAmount: {$sum: "$amount"}
},
$group: {
_id: {item: "$_id.item"},
total: {
"$push": {
type: {type: "$type"},
amount: {$sum : "$amount"}
}
}
}
Any help is appreciated.

Resources