Open scheying opened 7 years ago
I agree, this prevents using the client in cloud functions for example, it doesn't look like this package is being maintained.
Hey @sonaye I am also trying to get this up as a firebase function. Were you able to get this figured out?
@evberrypi I stopped working on it and ended up opting out from Bitpay for now, currently working on something custom. Try publishing the modified client -as described in my comment above- to npm, and see if it builds when you deploy, this should do it (please let me know).
Or, just use their rest api directly instead.
My attempt:
const functions = require('firebase-functions');
const bitauth = require('bitauth');
const bitpay = require('bitpay-rest');
exports.pay = functions.https.onRequest((req, res) => {
const { orderId } = req.body;
if (!orderId) {
const message = 'Order ID is missing';
console.error(400, message);
return res.status(400).send({ message });
}
const encPrivkey = 'foo';
const privkey = bitauth.decrypt('foo', encPrivkey);
const client = bitpay.createClient(privkey, {
config: {
apiHost: 'test.bitpay.com',
apiPort: 443,
forceSSL: true
}
});
client.on('error', err => {
const message = "Couldn't connect to Bitpay";
console.error(500, message, err);
return res.status(500).send({ message });
});
client.on('ready', () =>
client.as('pos').post(
'invoices',
{
currency: 'USD',
notificationEmail: 'foo@bar',
notificationURL: 'https://foo',
orderId,
price: 5.0,
redirectURL: 'https://foo'
},
(err, invoice) => {
if (err) {
const message = "Couldn't create invoice";
console.error(500, message, err);
return res.status(500).send({ message });
}
const response = {
// pay by sending btc to this address
btc: invoice.btcDue,
address: invoice.bitcoinAddress,
// or by scanning this qr
qr: `https://chart.googleapis.com/chart?cht=qr&chs=512&chl=${encodeURIComponent(
invoice.paymentUrls.BIP21
)}`,
// or by visiting this url
url: invoice.url,
// before this time
expires: invoice.expirationTime
};
console.log(200, response, invoice);
return res.status(200).send(response);
}
)
);
});
Hey @sonaye I was able to do it here was my process:
git+https://git@github.com/me/forkedRepo.git
and it worked locally, but still was not working with Cloud Functions.Thank you so much for your assistance
A couple of things to note:
firebase functions:config:set bitpay.key="thekey" bitpay.pass="password"
test.bitpay.com
and that you switch this to bitpay.com
when going live.Since there was a pull request that never got merged from last year on this issue, I'm going to assume that this is no longer maintained, I'll update the readme on bitpay-serverless
exports.bitPayment = functions.https.onRequest((req, res) => {
const bitpay = require('bitpay-serverless');
const pass = functions.config().bitpay.testpass; //or functions.config().bitpay.pass
const key = functions.config().bitpay.testkey; //or functions.config().bitpay.key
const privkey = bitauth.decrypt(pass, key)
console.log(privkey)
var client = bitpay.createClient(privkey, {
config: {
apiHost: 'test.bitpay.com', // if you are using production keys pass bitpay.com instead
apiPort: 443
}
});
client.on('error', function(err) {
// handle client errors here
console.log(err);
});
//Client will take a second to automatically load your tokens, after which it will emit this ready event
////You must wait for the ready event before issuing requests
client.on('ready', function(){
var data = {
price: 620,
currency: 'USD'
};
//NOTE: the .as('pos') is necessary for Point of Sale requests, use as('merchant') if you have a merchant token instead
client.as('pos').post('invoices', data, function(err, invoice) {
if (err){
// more error handling
console.log(err);
}else{
// success
console.log('invoice data', invoice);
}
});
});
// res.send("Done")
});
I think it should be possible to use the REST lib without creating a local config.json first. Especially since the config can already be provided as an argument to createClient()