Skip to content

Instantly share code, notes, and snippets.

@annibuliful
Last active August 31, 2017 13:49
Show Gist options
  • Save annibuliful/582ed33b677647fa663c4bda241f8649 to your computer and use it in GitHub Desktop.
Save annibuliful/582ed33b677647fa663c4bda241f8649 to your computer and use it in GitHub Desktop.
export default {
Mutation: {
buyProduct: async (root, params, options) => {
let today = new Date();
let dd = today.getDate();
let mm = today.getMonth() + 1;
let yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
let now = dd + '/' + mm + '/' + yyyy;
let returnData;
let add = await omise.charges.create({
'amount': params.Products.price + "00",
'currency': 'thb',
'card': params.tokenID
}, async (err, resp) => {
if (resp.paid) {
let add = await buyProductModel.insertMany({
userID: params.userID,
tokenID: params.tokenID,
productID: params.Products.productID,
productName: params.Products.productNames,
productPrice: params.Products.productPrices,
price: params.Products.price,
quantity: params.Products.quantitys,
transactionID: resp.transaction,
productImage: params.Products.productImage,
createdTime: now
}).then((data) => {
returnData = "success";
return "success";
}).catch((err) => {
console.log(err);
returnData = "failed";
});
return "failed"
} else {
returnData = "failed";
}
});
if (add.paid == true) {
return { created: true }
} else {
return { created: false }
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment