I am trying to integrate PayPal server-side payment on a website with a new v2 since v1 is deprecated. Since I had to include quite a lot of code from their git, i will post here everything for a working example
In v1 i could do it really easily:
app.get('/create', function (req, res) {
//build PayPal payment request
let payReq = JSON.stringify({
'intent': 'sale',
'redirect_urls': {
'return_url': 'http://localhost:8081/process',
'cancel_url': 'http://localhost:8081/cancel'
},
'payer': {
'payment_method': 'paypal'
},
'transactions': [{
'amount': {
'total': '7.47',
'currency': 'USD'
},
'description': 'This is the payment transaction description.'
}]
});
paypal.payment.create(payReq, function (error, payment) {
if (error) {
console.error(error);
} else {
//capture HATEOAS links
let links = {};
payment.links.forEach(function (linkObj) {
links[linkObj.rel] = {
'href': linkObj.href,
'method': linkObj.method
};
})
//if redirect url present, redirect user
if (links.hasOwnProperty('approval_url')) {
res.redirect(links['approval_url'].href);
} else {
console.error('no redirect URI present');
}
}
});
});
app.get('/process', function (req, res) {
let paymentId = req.query.paymentId;
let payerId = {'payer_id': req.query.PayerID};
paypal.payment.execute(paymentId, payerId, function (error, payment) {
if (error) {
console.error(error);
} else {
if (payment.state == 'approved') {
const payerCountry = payment.payer.payer_info.country_code;
const total = payment.transactions[0].amount.total;
const currency = payment.transactions[0].amount.currency;
savePayment(payerCountry, total, currency);
res.send('payment completed successfully ' + cnt++);
} else {
res.send('payment not successful');
}
}
});
});
The create endpoint basically creates the order, the paypal API returns hateos links and the controller says the browser should redirect to that link. Once redirected , if user approves payment on paypal site he is redirected to on of
'redirect_urls': {
'return_url': 'http://localhost:8081/process',
'cancel_url': 'http://localhost:8081/cancel'
},
process endpoints retrieves PAYMENT ID and PAYER ID from query and captures the order - allowing me to do whatever i want to do ( e.g save payment in db ) in callback.
Now v2 seems like huge mess:
Following devs guide i have created
Paypal button (copy paste ):
import React, {useEffect} from "react";
import {PayPalButtons, PayPalScriptProvider, usePayPalScriptReducer} from "#paypal/react-paypal-js";
// This values are the props in the UI
const amount = "2";
const currency = "USD";
const style = {"layout": "vertical"};
const ButtonWrapper = ({currency, showSpinner}) => {
// usePayPalScriptReducer can be use only inside children of PayPalScriptProviders
// This is the main reason to wrap the PayPalButtons in a new component
const [{options, isPending}, dispatch] = usePayPalScriptReducer();
useEffect(() => {
dispatch({
type: "resetOptions",
value: {
...options,
currency: currency,
},
});
}, [currency, showSpinner]);
const createOrder = (data, actions) => {
console.log("create")
return fetch('http://localhost:8081/create', {
method: 'post'
}).then(function (res) {
return res.json();
}).then(function (orderData) {
console.log(orderData);
window.location = orderData.redirect;
});
}
// Call your server to finalize the transaction
const onApprove = (data, actions) => {
console.log("approve")
return fetch('/process', {
method: 'post'
}).then(function (res) {
return res.json();
}).then(function (orderData) {
// Three cases to handle:
// (1) Recoverable INSTRUMENT_DECLINED -> call actions.restart()
// (2) Other non-recoverable errors -> Show a failure message
// (3) Successful transaction -> Show confirmation or thank you
// This example reads a v2/checkout/orders capture response, propagated from the server
// You could use a different API or structure for your 'orderData'
var errorDetail = Array.isArray(orderData.details) && orderData.details[0];
if (errorDetail && errorDetail.issue === 'INSTRUMENT_DECLINED') {
return actions.restart(); // Recoverable state, per:
// https://developer.paypal.com/docs/checkout/integration-features/funding-failure/
}
if (errorDetail) {
var msg = 'Sorry, your transaction could not be processed.';
if (errorDetail.description) msg += '\n\n' + errorDetail.description;
if (orderData.debug_id) msg += ' (' + orderData.debug_id + ')';
return alert(msg); // Show a failure message (try to avoid alerts in production environments)
}
// Successful capture! For demo purposes:
console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
var transaction = orderData.purchase_units[0].payments.captures[0];
alert('Transaction ' + transaction.status + ': ' + transaction.id + '\n\nSee console for all available details');
});
}
return (<>
{(showSpinner && isPending) && <div className="spinner"/>}
<PayPalButtons
style={style}
disabled={false}
forceReRender={[amount, currency, style]}
fundingSource={undefined}
createOrder={(data, actions) => createOrder(data, actions)}
onApprove={(data, actions) => onApprove(data, actions)}
/>
</>
);
}
export default function PayPalButton() {
return (
<div style={{ maxWidth: "750px", minHeight: "200px" }}>
<PayPalScriptProvider
options={{
"client-id": "test",
components: "buttons",
currency: "USD"
}}
>
<ButtonWrapper
currency={currency}
showSpinner={false}
/>
</PayPalScriptProvider>
</div>
);
}
And then followed the flow from paypal's github example
created their HttpClient
const checkoutNodeJssdk = require('#paypal/checkout-server-sdk');
/**
* Returns PayPal HTTP client instance with environment which has access
* credentials context. This can be used invoke PayPal API's provided the
* credentials have the access to do so.
*/
function client() {
return new checkoutNodeJssdk.core.PayPalHttpClient(environment());
}
/**
* Setting up and Returns PayPal SDK environment with PayPal Access credentials.
* For demo purpose, we are using SandboxEnvironment. In production this will be
* LiveEnvironment.
*/
function environment() {
let clientId = process.env.PAYPAL_CLIENT_ID || '<clientId>';
let clientSecret = process.env.PAYPAL_CLIENT_SECRET || '<secret>';
if (process.env.NODE_ENV === 'production') {
return new checkoutNodeJssdk.core.LiveEnvironment(clientId, clientSecret);
}
return new checkoutNodeJssdk.core.SandboxEnvironment(clientId, clientSecret);
}
async function prettyPrint(jsonData, pre=""){
let pretty = "";
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
}
for (let key in jsonData){
if (jsonData.hasOwnProperty(key)){
if (isNaN(key))
pretty += pre + capitalize(key) + ": ";
else
pretty += pre + (parseInt(key) + 1) + ": ";
if (typeof jsonData[key] === "object"){
pretty += "\n";
pretty += await prettyPrint(jsonData[key], pre + "\t");
}
else {
pretty += jsonData[key] + "\n";
}
}
}
return pretty;
}
module.exports = {client: client, prettyPrint:prettyPrint};
created their CreateOrder:
/**
* PayPal SDK dependency
*/
const checkoutNodeJssdk = require('#paypal/checkout-server-sdk');
/**
* PayPal HTTP client dependency
*/
const payPalClient = require('./PayPalClient');
/**
* Setting up the JSON request body for creating the Order. The Intent in the
* request body should be set as "CAPTURE" for capture intent flow.
*
*/
function buildRequestBody() {
return {
"intent": "CAPTURE",
"application_context": {
"return_url": "http://localhost:8081/process",
"cancel_url": "https://www.example.com",
"locale": "en-US",
"landing_page": "BILLING",
"user_action": "CONTINUE"
},
"purchase_units": [
{
"soft_descriptor": "Donation",
"amount": {
"currency_code": "USD",
"value": "220.00"
}
}
]
};
}
/**
* This is the sample function which can be sued to create an order. It uses the
* JSON body returned by buildRequestBody() to create an new Order.
*/
async function createOrder(debug=false) {
try {
const request = new checkoutNodeJssdk.orders.OrdersCreateRequest();
request.headers["prefer"] = "return=representation";
request.requestBody(buildRequestBody());
const response = await payPalClient.client().execute(request);
if (debug){
console.log("Status Code: " + response.statusCode);
console.log("Status: " + response.result.status);
console.log("Order ID: " + response.result.id);
console.log("Intent: " + response.result.intent);
console.log("Links: ");
response.result.links.forEach((item, index) => {
let rel = item.rel;
let href = item.href;
let method = item.method;
let message = `\t${rel}: ${href}\tCall Type: ${method}`;
console.log(message);
});
console.log(`Gross Amount: ${response.result.purchase_units[0].amount.currency_code} ${response.result.purchase_units[0].amount.value}`);
// To toggle print the whole body comment/uncomment the below line
console.log(JSON.stringify(response.result, null, 4));
}
return response;
}
catch (e) {
console.log(e)
}
}
/**
* This is the driver function which invokes the createOrder function to create
* an sample order.
*/
if (require.main === module){
(async() => await createOrder(true))();
}
/**
* Exports the Create Order function. If needed this can be invoked from the
* order modules to execute the end to flow like create order, retrieve, capture
* and refund(Optional)
*/
module.exports = {createOrder:createOrder};
And endpoints:
const createUsersOrder = async (res) => {
try {
let response = await createOrder();
console.log("Creating Order...");
let orderId = "";
if (response.statusCode === 201){
console.log("Created Successfully");
orderId = response.result.id;
console.log("Links:");
response.result.links.forEach((item, index) => {
let rel = item.rel;
let href = item.href;
let method = item.method;
let message = `\t${rel}: ${href}\tCall Type: ${method}`;
console.log(message);
});
let links = {};
response.result.links.forEach(function (linkObj) {
links[linkObj.rel] = {
'href': linkObj.href,
'method': linkObj.method
};
})
//if redirect url present, redirect user
if (links.hasOwnProperty('approve')) {
var returnObj = {redirect : links['approve'].href}
console.log("Returning " + returnObj)
res.send(returnObj);
} else {
console.error('no redirect URI present');
}
}
console.log("Copy approve link and paste it in browser. Login with buyer account and follow the instructions.\nOnce approved hit enter...");
return
} catch (error) {
console.log('There was an error: ', error);
}
};
app.post("/create", function(req,res) {
createUsersOrder(res);
})
Here, this is called when button is clicked, as "createOrder" method is called. I create order just like in the v1 code, and then redirect browser to the url. However when the user approves transaction on paypal, and thus is being redirected to one of
"application_context": {
"return_url": "http://localhost:8081/process",
"cancel_url": "http://localhost:8081/cancel",
"locale": "en-US",
"landing_page": "BILLING",
"user_action": "CONTINUE"
},
return url ( /process route for success ), the request DOES NOT contain payment_id, only PAYER_ID. But the payment_id ( or order_id in v2 ) is needed to capture the order.
Since i have found literally zero blogs, tutorials, guide for v2 ( only millions for v1) i am confused where to get the order id. Do i really need to save it in DB after i create the order? OR is there any other trick?
Also, the button contains onApprove method, but after creating order, the browser is redirected to paypal, and the paypal redirects user to http://localhost:8081/process endpoint - thus the onApprove method is never invoked and useless (?).
This whole flow of v2 is really confusing, is there something i am missing?
Thanks for help
With your v2 code, do not use any redirects. At all. Your button should call 2 endpoints on your server. These two endpoints should (respectively) do the API operations of creating and capturing the order, and return the JSON result in each case (the capture route can do any server-side operations like storing the transaction result in the database before forwarding the JSON result to the client caller, since the client needs to handle any capture error situations as well as showing a success message). You can find a full stack node.js example in the PayPal integration guide, but it's fine to keep your #paypal/react-paypal-js code pretty much as-is for the front end.
This is the scenario where I am storing several attributes into the database. Now, while I fetch the response I don't want the exact attribute name what's already stored in Db. For eg, below is my code. Here, I have stored fields as 'ca_xyz'.Now while fetching the response I want only only 'xyz' as fieldname. Can anyone help me?
// --------adding customer group--------
const addCustomerGroup = async(req,res,next) => {
try{
console.log(req.body)
let data = await Group.create({
cg_name:req.body.name,
cg_description:req.body.description,
cg_is_active: req.body.is_ctive,
cg_created_by: req.body.created_by,
cg_modified_by: req.body.modified_by
})
var newData = {};
for (const [key, value] of Object.entries(data)) {
newData[key.substr("cg_".length)] = value;
}
console.log(newData)
res.send({
status:"success",
message:"Added customer group"
})
}
catch(error){
console.log(error)
res.send({
status:"failed",
message:"An error occurred"
})
}
}
I am trying to send a push notification via Firebase cloud messaging. I am using Firebase admin sdk to send push notification in fcm . I am using nodejs
When I am trying to send a push msg , ...
I am getting this error
{
code: 'messaging/invalid-payload',
message: 'data must be a non-null object' },
codePrefix: 'messaging'
}
My code :
const admin = require('firebase-admin');
const serviceAccount = require(`${__dirname}/fet_firebase.json`);
function sendPushNot(to, body, sendId, type) {
const registrationToken = to;
const notification = {};
let message = { };
const pbody = { body };
if (type === 'User') {
pbody.userId = sendId;
notification.userId = sendId;
notification.title = 'New user Follwed';
}
if (type === 'Post') {
pbody.postId = sendId;
notification.postId = sendId;
notification.title = 'Post Liked';
}
if (type === 'Room') {
pbody.roomId = sendId;
notification.roomId = sendId;
notification.title = 'New Chat messsage';
}
message = {
data: JSON.stringify(pbody),
token: registrationToken,
notification
};
console.log('messgae',message);
admin.messaging().send(message)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent cloud message:', response);
})
.catch((error) => {
console.log('Error sending cloud message:', error);
});
}
I thought that body is null
But the console output of console.log('messgae',message); is ::
{
data:
'{"body":"pankaj Liked Your Post","postId":"5ed1055ddf0efd2a42f6a28a"}',
token:
'f2umP-jfQyeM1suN77zz7-:APA91bHKzfqfRnmuBom2PIDB8cCPwZtq28JCWLSi1OMPO55JRzyhFZJpTkkNyDu_StTYID-scu-grejaxxn3d4iR6Xidz9-JCk_h-bRsdGHe8nzMrIVsc8vZDFgayiFgJrJ53DaDzb9b',
notification: { postId: 5ed1055ddf0efd2a42f6a28a, title: 'Post Liked'
}
}
So the body is not null
But I am getting data must be a non-null object' error ..
Why?
I fixed this by wrapping the stringified object with curly braces
data : { data: JSON.stringify(object) } // correct
data : JSON.stringify(object) // will result to the described error.
Data must be a non-null object. Above code sample is passing a string. Just remove the JSON.stringify() part.
I'm trying to transfer BTC and BCH between accounts. Looking through the documentation I have found the following:
https://developers.coinbase.com/api/v2#transfer-money-between-accounts
First off I believe there is an error in their example, as the "currency" property is mandatory, it gives an error when not providing it.
But after adding the currency, every request I make returns with "Not found".
I'm using the node client, but I think the problem is higher up.
This is the request I'm doing:
const Client = require('coinbase').Client;
let client = new Client({'apiKey': 'xxxx', 'apiSecret': 'xxx'});
client.getAccounts({}, function(err, accounts) {
let btc_account = null;
let bch_account = null;
for(account of accounts) {
if(account.currency == "BTC") {
btc_account = account;
} else if(account.currency == "BCH") {
bch_account = account;
}
}
var options = {'to': bch_account.id, 'amount': btc_account.balance.amount, 'currency': "BTC"};
btc_account.transferMoney(options, function(err, tx) {
if(err) {
console.log("ERROR during transfer", err);
return
}
console.log(tx);
});
});
Doing this returns me with:
ERROR during transfer NotFound: Not found
After doing some debugging, I found it tries to do the request with the following options:
url: 'https://api.coinbase.com/v2/accounts/xxx/transactions'
body: '{"to":"xxx","amount":"0.00072256","currency":"BTC","type":"transfer"}'
(obfuscated the actual account.id's)
And the actual response from their API is:
{"errors":[{"id":"not_found","message":"Not found"}]}
Can anybody tell me what I'm doing wrong here?
I'm trying to PUT in some data to a small node server I've written.
The code on the server side is as follows:
router.route('/returnLockID').put(function(req, res){
mongoOp.findOne({
name: req.body.name
}, function(err, user) { if(err) {
response = {"error" : true,"message" : "Error fetching data"};
} else {
// we got data from Mongo.
// change it accordingly.
if(req.body.LockID !== undefined) {
// case where ID needs to be updated.
user.LockID = req.body.LockID;
}
// save the data
user.save(function(err){
if(err) {
response = {"error" : true,"message" : "Error updating data"};
} else {
response = {"error" : false,"message" : "Data is updated for "+req.body.name};
}
res.json(response);
})
}
});
})
I get the response:
{
"error": false,
"message": "Data is updated for nv942"
}
However, the data isn't updated. Can anyone see where I'm going wrong with the saving?
It all goes through fine when I PUT using postman, i can save, however, when I try PUT from iOS I get:
TypeError: Cannot read property 'save' of null
at /Users/NikhilVedi/Documents/FYP/Server/lockserver/routes/users.js:92:13
at Query.<anonymous> (/Users/NikhilVedi/Documents/FYP/Server/lockserver/node_modules/mongoose/lib/model.js:3407:16)
at /Users/NikhilVedi/Documents/FYP/Server/lockserver/node_modules/kareem/index.js:259:21
at /Users/NikhilVedi/Documents/FYP/Server/lockserver/node_modules/kareem/index.js:127:16
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
The swift code is:
#IBAction func setup(_ sender: Any) {
if (UserDefaults.standard.value(forKey: "userIP") == nil)
{
//make this a message box and stop the program crashing by assigning user defaults a value
UserDefaults.standard.set("localhost", forKey: "userIP")
print("Local host programatically set");
}
let u = UserDefaults.standard.value(forKey: "userIP")!
let name = UserDefaults.standard.value(forKey: "email")!
var request = URLRequest(url: URL(string: "http://\(u):3000/users/returnLockID")!)
request.httpMethod = "PUT"
let postString = "LockID=\(LockID.text!)name=\(name)"
print(postString)
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
let responseString = String(data: data, encoding: .utf8)
// print("responseString = \(responseString)")
if let data = responseString?.data(using: String.Encoding.utf8) {
let resString = JSON(data: data)
if resString["success"].stringValue == "true"
{
print("works");
}
else if resString["success"].stringValue == "false"
{
print("failed")
print(resString["message"].stringValue)
//dismiss window and set bool to true
UserDefaults.standard.set(true, forKey: "LockIDPresent")
self.dismiss(animated: true, completion: nil);
}
}
}
task.resume()
}
Thanks in advance!
One error I see, but not likely the fix to your issue is that you should be doing res.json(response); in the first if statement clause.
Another thing to notice is that you are calling save regardless of whether or not a req.body.LockID value is provided, so in cases where it isn't the user is saved without any modification. You might want to print out the value or req.body.LockID, as that might be the reason that the user email is not being updated.