Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
});
expect(transaction.FromCollectiveId).to.equal(xdamman.CollectiveId);
expect(transaction.CollectiveId).to.equal(collective.id);
expect(transaction.currency).to.equal(collective.currency);
expect(transaction.hostFeeInHostCurrency).to.equal(0);
expect(transaction.platformFeeInHostCurrency).to.equal(0.05 * order.totalAmount);
expect(transaction.data.charge.currency).to.equal(collective.currency.toLowerCase());
expect(transaction.data.charge.status).to.equal('succeeded');
expect(transaction.data.balanceTransaction.net - transaction.hostFeeInHostCurrency).to.equal(transaction.netAmountInCollectiveCurrency);
// make sure the payment has been recorded in the connected Stripe Account of the host
const hostMember = await models.Member.findOne({ where: { CollectiveId: collective.id, role: 'HOST' } });
const hostStripeAccount = await models.ConnectedAccount.findOne({
where: { service: 'stripe', CollectiveId: hostMember.MemberCollectiveId }
});
const charge = await Stripe(hostStripeAccount.token).charges.retrieve(transaction.data.charge.id);
expect(charge.source.last4).to.equal('4242');
});
"stripe/refund/create"(paymentMethod, amount, reason = "requested_by_customer") {
check(amount, Number);
check(reason, String);
// Call both check and validate because by calling `clean`, the audit pkg
// thinks that we haven't checked paymentMethod arg
check(paymentMethod, Object);
PaymentMethodArgument.validate(PaymentMethodArgument.clean(paymentMethod));
let result;
try {
const stripeKey = utils.getStripeApi(paymentMethod.paymentPackageId);
const stripe = stripeNpm(stripeKey);
const refundPromise = stripe.refunds.create({ charge: paymentMethod.transactionId, amount: formatForStripe(amount) });
const refundResult = Promise.await(refundPromise);
Logger.debug(refundResult);
if (refundResult && refundResult.object === "refund") {
result = {
saved: true,
response: refundResult
};
} else {
result = {
saved: false,
response: refundResult
};
Logger.warn("Stripe call succeeded but refund not issued");
}
} catch (error) {
export default function feathersStripeWebhooks(handlers, config = {}) {
const options = Object.assign(
{
// Set to false to disable event fetching from Stripe
// https://stripe.com/docs/webhooks#verifying-events
verifyEvents: true,
},
config
);
debug(`Creating feathers-stripe-webhooks service with options: ${JSON.stringify(options)}`);
checkOpts(options);
const api = options.secret ? stripe(options.secret) : null;
return service(api, handlers, options);
}
import Stripe from 'stripe';
import express from 'express';
import bodyParser from 'body-parser';
const stripe = new Stripe(process.env.STRIPE_API_KEY!);
/**
* You'll need to make sure this is externally accessible. ngrok (https://ngrok.com/)
* makes this really easy.s
*
* To run this file, just provide your Secret API Key and Webhook Secret, like so:
* STRIPE_API_KEY=sk_test_XXX WEBHOOK_SECRET=whsec_XXX node express.js
*/
const webhookSecret: string = process.env.WEBHOOK_SECRET!;
const app: express.Application = express();
// Only use the raw body parser for webhooks
app.use(
(
req: express.Request,
const customerAccount = Accounts.findOne({ _id: cart.accountId });
if (customerAccount) {
const defaultEmail = (customerAccount.emails || []).find((email) => email.provides === "default");
if (defaultEmail) {
customerEmail = defaultEmail.address;
}
}
}
if (!customerEmail) {
throw new ReactionError("invalid-parameter", "No email associated with the cart");
}
// Initialize stripe api lib
const stripeApiKey = stripePkg.settings.api_key;
const stripe = stripeNpm(stripeApiKey);
// get array of shopIds that exist in this cart
const shopIds = cart.items.reduce((uniqueShopIds, item) => {
if (uniqueShopIds.indexOf(item.shopId) === -1) {
uniqueShopIds.push(item.shopId);
}
return uniqueShopIds;
}, []);
const transactionsByShopId = {};
// TODO: If there is only one transactionsByShopId and the shopId is primaryShopId -
// Create a standard charge and bypass creating a customer for this charge
const primaryShop = Shops.findOne({ _id: primaryShopId });
const { currency } = primaryShop;
function stripeCaptureCharge(paymentMethod) {
let result;
const captureDetails = {
amount: formatForStripe(paymentMethod.amount)
};
const stripeKey = utils.getStripeApi(paymentMethod.paymentPackageId);
const stripe = stripeNpm(stripeKey);
try {
const capturePromise = stripe.charges.capture(paymentMethod.transactionId, captureDetails);
const captureResult = Promise.await(capturePromise);
if (captureResult.status === "succeeded") {
result = {
saved: true,
response: captureResult
};
} else {
result = {
saved: false,
response: captureResult
};
}
/*
* All calls to stripe are meant to go through this gateway
*/
import { get } from 'lodash';
import Stripe from 'stripe';
import config from 'config';
import debugLib from 'debug';
const debug = debugLib('stripe');
export const appStripe = Stripe(config.stripe.secret);
/**
* Create stripe customer
* Returns a customerId (string)
*/
export const createCustomer = (stripeAccount, token, options = {}) => {
const collective = options.collective || {};
const payload = {
source: token,
description: `https://opencollective.com/${collective.slug}`,
email: options.email || '',
};
return appStripe.customers.create(payload, {
stripe_account: stripeAccount && stripeAccount.username,
export default function getStripeInstance(context, stripeApiKey) {
const stripe = stripeNpm(stripeApiKey);
stripe.setAppInfo({
...APP_INFO,
version: context.appVersion
});
return stripe;
}
export default async function stripeCreateRefund(context, paymentMethod, amount, reason) {
let result;
try {
const stripeKey = await getStripeApi(context, paymentMethod.paymentPluginName, paymentMethod.shopId);
const stripe = stripeNpm(stripeKey);
const refundResult = await stripe.refunds.create({ charge: paymentMethod.transactionId, amount: formatForStripe(amount), reason });
Logger.debug(refundResult);
if (refundResult && refundResult.object === "refund") {
result = {
saved: true,
response: refundResult
};
} else {
result = {
saved: false,
response: refundResult
};
Logger.warn("Stripe call succeeded but refund not issued");
}
} catch (error) {
Logger.error(error);
export const subscribeMutation = async (_, { stripeToken }, context) => {
if (!context.req.user) throw new Error('Not authorized');
const { stripePlanName, stripeSecretKey } = context.req.settings;
const stripe = stripePackage(stripeSecretKey);
const { id, email } = context.req.user;
let { stripeCustomerId } = context.req.user;
if (!stripeCustomerId) {
const customer = await stripe.customers.create({ email });
stripeCustomerId = customer.id;
await User.update({ stripe_customer_id: stripeCustomerId }, { where: { id } });
}
await stripe.customers.createSource(stripeCustomerId, { source: stripeToken });
const subscriptions = await stripe.subscriptions.list({ customer: stripeCustomerId });
if (!subscriptions.data.length) {
await stripe.subscriptions.create({ customer: stripeCustomerId, items: [{ plan: stripePlanName }] });
context.req.user.update({ isSubscribed: true });