<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<div class="container">
<div class="row">
<h2>strip payment getway</h2>
</div>
</div>
import Stripe from 'stripe';
import { genHash } from '../utils/common.util';
const createStripeObject = () => {
return new Stripe(process.env.STRIPE_SECRET_KEY || "", {
apiVersion: '2020-08-27',
});
};
export const createStripePlan = async (payload: any) => {
const res = await createPlan(payload);
return res;
}
const createPlan = async (payload: any) => {
const stripe = createStripeObject();
return stripe.plans.create({
amount: parseFloat(payload.price) * 100,
interval: payload.type.toLowerCase(),
interval_count: payload.interval,
product: {
name: payload.name
},
currency: "USD"
})
}
const createCustomer = async (payload: any) => {
const stripe = createStripeObject();
return stripe.customers.create({
source: payload.stripeToken,
name: payload.name,
email: payload.email
});
};
// const createPaymentIntent = async (amount: number, stripeCustomerId: string, currency: string) => {
// const stripe = createStripeObject();
// const sourceResponse = await stripe.customers.createSource(
// stripeCustomerId,
// { source: 'tok_amex' }
// );
// return stripe.paymentIntents.create({
// amount: amount * 100,
// customer: stripeCustomerId,
// currency,
// confirm: true,
// payment_method: sourceResponse.id,
// });
// };
const createPaymentIntent = async (amount: number, stripeCustomerId: string, currency: string, stripeToken: any) => {
const stripe = createStripeObject();
const sourceResponse = await stripe.customers.createSource(
stripeCustomerId,
{ source: 'tok_visa' }
);
const token = await stripe.tokens.retrieve(
stripeToken
);
return stripe.charges.create({
amount: amount * 10,
currency,
source: token.id,
});
};
const createSubscription = async (stripeCustomerId: string, planId: string) => {
const stripe = createStripeObject();
return stripe.subscriptions.create({
customer: stripeCustomerId,
items: [
{ plan: planId },
],
});
};
const createClientDataBase = async (body: any, dbName: string) => {
return new Promise((resolve, reject) => {
var MongoClient = require('mongodb').MongoClient;
var url = process.env.MONGODB_URI || "mongodb://localhost:27017/";
MongoClient.connect(url, async function(err: any, db:any) {
if (err) {
db.close();
reject(err.MongoError);
}
try {
var dbo = db.db(dbName);
const collection = await dbo.createCollection("users");
const hashed = await genHash(body.password);
await collection.insert( { email: body.email, firstName: body.firstName, lastName: body.lastName, password: hashed, role: 1 });
console.log("Collection created!");
db.close();
resolve({message: "Database created!"})
} catch (err) {
db.close();
reject(err);
}
});
})
}
export {
createPlan,
createCustomer,
createPaymentIntent,
createSubscription,
createClientDataBase
};
@Post("/create")
public async save(@Body() request: { stripeToken: string, email: string, fullname: string, amount: number, currency: string, planId: string }): Promise<IResponse> {
try {
const { email, fullname, amount, currency, stripeToken, planId } = request;
const stripeCharges = request
// const validatedProfile = validatePayment({ email, fullname, amount, currency, stripeToken });
// if (validatedProfile.error) {
// throw new Error(validatedProfile.error.message)
// }
// const customerPayload = {
// email: request.email,
// name: request.fullname,
// stripe: request.stripeToken,
// };
// let stripCustomerId;
// const userDetails = await findOne(clientModel, { stripCustomerId: { $exists: true } });
// console.log(userDetails, "????????????????")
// // if (userDetails) {
// // stripCustomerId = userDetails.stripCustomerId;
// // }
// // else {
// const newCustomerResponse = await createCustomer(customerPayload);
// console.log(newCustomerResponse, ">>>>>>")
// // stripCustomerId = newCustomerResponse.id;
// // const saveResponse = await upsert(clientModel, {stripCustomerId:stripCustomerId} );
// // }
// // const stripeCharges = await createPaymentIntent(request.amount, newCustomerResponse.id, request.currency,request.stripeToken);
// // const userSubscription = await createSubscription(newCustomerResponse.id, planId);
// const stripeCharges = await createPaymentIntent(request.amount, newCustomerResponse.id, request.currency, request.stripeToken);
// const userSubscription = await createSubscription(newCustomerResponse.id, planId);
return {
data: stripeCharges,
// data: stripeCharges.next_action?.use_stripe_sdk,
error: '',
message: 'Payment completed successfully',
status: 200
}
}
catch (err: any) {
logger.error(`${this.req.ip} ${err.message}`)
return {
data: null,
error: err.message ? err.message : err,
message: '',
status: 400
}
}
}
}