medusa-payment-stripe
Advanced tools
Comparing version 1.0.15 to 1.1.0
@@ -10,9 +10,13 @@ "use strict"; | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var carts = { | ||
emptyCart: { | ||
_id: _medusaTestUtils.IdMap.getId("emptyCart"), | ||
id: _medusaTestUtils.IdMap.getId("emptyCart"), | ||
items: [], | ||
region_id: _medusaTestUtils.IdMap.getId("testRegion"), | ||
customer_id: "test-customer", | ||
payment_sessions: [], | ||
shipping_options: [{ | ||
_id: _medusaTestUtils.IdMap.getId("freeShipping"), | ||
id: _medusaTestUtils.IdMap.getId("freeShipping"), | ||
profile_id: "default_profile", | ||
@@ -25,3 +29,3 @@ data: { | ||
frCart: { | ||
_id: _medusaTestUtils.IdMap.getId("fr-cart"), | ||
id: _medusaTestUtils.IdMap.getId("fr-cart"), | ||
email: "lebron@james.com", | ||
@@ -31,49 +35,44 @@ title: "test", | ||
items: [{ | ||
_id: _medusaTestUtils.IdMap.getId("line"), | ||
id: _medusaTestUtils.IdMap.getId("line"), | ||
title: "merge line", | ||
description: "This is a new line", | ||
thumbnail: "test-img-yeah.com/thumb", | ||
content: [{ | ||
unit_price: 8, | ||
variant: { | ||
_id: _medusaTestUtils.IdMap.getId("eur-8-us-10") | ||
}, | ||
product: { | ||
_id: _medusaTestUtils.IdMap.getId("product") | ||
}, | ||
quantity: 1 | ||
}, { | ||
unit_price: 10, | ||
variant: { | ||
_id: _medusaTestUtils.IdMap.getId("eur-10-us-12") | ||
}, | ||
product: { | ||
_id: _medusaTestUtils.IdMap.getId("product") | ||
}, | ||
quantity: 1 | ||
}], | ||
unit_price: 8, | ||
variant: { | ||
id: _medusaTestUtils.IdMap.getId("eur-8-us-10") | ||
}, | ||
product: { | ||
id: _medusaTestUtils.IdMap.getId("product") | ||
}, | ||
// { | ||
// unit_price: 10, | ||
// variant: { | ||
// id: IdMap.getId("eur-10-us-12"), | ||
// }, | ||
// product: { | ||
// id: IdMap.getId("product"), | ||
// }, | ||
// quantity: 1, | ||
// }, | ||
quantity: 10 | ||
}, { | ||
_id: _medusaTestUtils.IdMap.getId("existingLine"), | ||
}, _defineProperty({ | ||
id: _medusaTestUtils.IdMap.getId("existingLine"), | ||
title: "merge line", | ||
description: "This is a new line", | ||
thumbnail: "test-img-yeah.com/thumb", | ||
content: { | ||
unit_price: 10, | ||
variant: { | ||
_id: _medusaTestUtils.IdMap.getId("eur-10-us-12") | ||
}, | ||
product: { | ||
_id: _medusaTestUtils.IdMap.getId("product") | ||
}, | ||
quantity: 1 | ||
unit_price: 10, | ||
variant: { | ||
id: _medusaTestUtils.IdMap.getId("eur-10-us-12") | ||
}, | ||
quantity: 10 | ||
}], | ||
product: { | ||
id: _medusaTestUtils.IdMap.getId("product") | ||
}, | ||
quantity: 1 | ||
}, "quantity", 10)], | ||
shipping_methods: [{ | ||
_id: _medusaTestUtils.IdMap.getId("freeShipping"), | ||
id: _medusaTestUtils.IdMap.getId("freeShipping"), | ||
profile_id: "default_profile" | ||
}], | ||
shipping_options: [{ | ||
_id: _medusaTestUtils.IdMap.getId("freeShipping"), | ||
id: _medusaTestUtils.IdMap.getId("freeShipping"), | ||
profile_id: "default_profile" | ||
@@ -101,7 +100,7 @@ }], | ||
frCartNoStripeCustomer: { | ||
_id: _medusaTestUtils.IdMap.getId("fr-cart-no-customer"), | ||
id: _medusaTestUtils.IdMap.getId("fr-cart-no-customer"), | ||
title: "test", | ||
region_id: _medusaTestUtils.IdMap.getId("region-france"), | ||
items: [{ | ||
_id: _medusaTestUtils.IdMap.getId("line"), | ||
id: _medusaTestUtils.IdMap.getId("line"), | ||
title: "merge line", | ||
@@ -113,6 +112,6 @@ description: "This is a new line", | ||
variant: { | ||
_id: _medusaTestUtils.IdMap.getId("eur-8-us-10") | ||
id: _medusaTestUtils.IdMap.getId("eur-8-us-10") | ||
}, | ||
product: { | ||
_id: _medusaTestUtils.IdMap.getId("product") | ||
id: _medusaTestUtils.IdMap.getId("product") | ||
}, | ||
@@ -123,6 +122,6 @@ quantity: 1 | ||
variant: { | ||
_id: _medusaTestUtils.IdMap.getId("eur-10-us-12") | ||
id: _medusaTestUtils.IdMap.getId("eur-10-us-12") | ||
}, | ||
product: { | ||
_id: _medusaTestUtils.IdMap.getId("product") | ||
id: _medusaTestUtils.IdMap.getId("product") | ||
}, | ||
@@ -133,3 +132,3 @@ quantity: 1 | ||
}, { | ||
_id: _medusaTestUtils.IdMap.getId("existingLine"), | ||
id: _medusaTestUtils.IdMap.getId("existingLine"), | ||
title: "merge line", | ||
@@ -141,6 +140,6 @@ description: "This is a new line", | ||
variant: { | ||
_id: _medusaTestUtils.IdMap.getId("eur-10-us-12") | ||
id: _medusaTestUtils.IdMap.getId("eur-10-us-12") | ||
}, | ||
product: { | ||
_id: _medusaTestUtils.IdMap.getId("product") | ||
id: _medusaTestUtils.IdMap.getId("product") | ||
}, | ||
@@ -152,7 +151,7 @@ quantity: 1 | ||
shipping_methods: [{ | ||
_id: _medusaTestUtils.IdMap.getId("freeShipping"), | ||
id: _medusaTestUtils.IdMap.getId("freeShipping"), | ||
profile_id: "default_profile" | ||
}], | ||
shipping_options: [{ | ||
_id: _medusaTestUtils.IdMap.getId("freeShipping"), | ||
id: _medusaTestUtils.IdMap.getId("freeShipping"), | ||
profile_id: "default_profile" | ||
@@ -187,2 +186,6 @@ }], | ||
if (cartId === _medusaTestUtils.IdMap.getId("fr-cart-no-customer")) { | ||
return Promise.resolve(carts.frCartNoStripeCustomer); | ||
} | ||
if (cartId === _medusaTestUtils.IdMap.getId("emptyCart")) { | ||
@@ -189,0 +192,0 @@ return Promise.resolve(carts.emptyCart); |
@@ -59,3 +59,3 @@ "use strict"; | ||
_context.next = 23; | ||
return orderService.update(order._id, { | ||
return orderService.update(order.id, { | ||
payment_status: "captured" | ||
@@ -62,0 +62,0 @@ }); |
@@ -6,2 +6,10 @@ # Change Log | ||
# [1.1.0](https://github.com/medusajs/medusa/compare/medusa-payment-stripe@1.0.15...medusa-payment-stripe@1.1.0) (2021-01-26) | ||
**Note:** Version bump only for package medusa-payment-stripe | ||
## [1.0.15](https://github.com/medusajs/medusa/compare/medusa-payment-stripe@1.0.14...medusa-payment-stripe@1.0.15) (2020-12-17) | ||
@@ -8,0 +16,0 @@ |
{ | ||
"name": "medusa-payment-stripe", | ||
"version": "1.0.15", | ||
"version": "1.1.0", | ||
"description": "Stripe Payment provider for Meduas Commerce", | ||
@@ -18,2 +18,3 @@ "main": "index.js", | ||
"@babel/plugin-proposal-class-properties": "^7.7.4", | ||
"@babel/plugin-proposal-optional-chaining": "^7.12.7", | ||
"@babel/plugin-transform-classes": "^7.9.5", | ||
@@ -29,3 +30,3 @@ "@babel/plugin-transform-instanceof": "^7.8.3", | ||
"jest": "^25.5.2", | ||
"medusa-test-utils": "^1.0.13" | ||
"medusa-test-utils": "^1.1.0" | ||
}, | ||
@@ -44,6 +45,6 @@ "scripts": { | ||
"express": "^4.17.1", | ||
"medusa-core-utils": "^1.0.11", | ||
"medusa-core-utils": "^1.1.0", | ||
"stripe": "^8.50.0" | ||
}, | ||
"gitHead": "cb0fc0d47561df79fbef3bd3091ab9469d3e31c8" | ||
"gitHead": "ff49d2332252e7f0e03312e8c9123ed756217807" | ||
} |
@@ -21,3 +21,3 @@ "use strict"; | ||
return Promise.resolve({ | ||
id: "pi" | ||
id: "pi_no" | ||
}); | ||
@@ -24,0 +24,0 @@ } |
@@ -20,2 +20,6 @@ "use strict"; | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
@@ -90,6 +94,24 @@ | ||
_this = _super.call(this); | ||
/** | ||
* Required Stripe options: | ||
* { | ||
* api_key: "stripe_secret_key", REQUIRED | ||
* webhook_secret: "stripe_webhook_secret", REQUIRED | ||
* // Use this flag to capture payment immediately (default is false) | ||
* capture: true | ||
* } | ||
*/ | ||
_this.options_ = options; | ||
/** @private @const {Stripe} */ | ||
_this.stripe_ = (0, _stripe["default"])(options.api_key); | ||
/** @private @const {CustomerService} */ | ||
_this.customerService_ = customerService; | ||
/** @private @const {RegionService} */ | ||
_this.regionService_ = regionService; | ||
/** @private @const {TotalsService} */ | ||
_this.totalsService_ = totalsService; | ||
@@ -99,4 +121,5 @@ return _this; | ||
/** | ||
* Status for Stripe PaymentIntent. | ||
* @param {Object} paymentData - payment method data from cart | ||
* Fetches Stripe payment intent. Check its status and returns the | ||
* corresponding Medusa status. | ||
* @param {object} paymentData - payment method data from cart | ||
* @returns {string} the status of the payment intent | ||
@@ -123,3 +146,3 @@ */ | ||
paymentIntent = _context.sent; | ||
status = "initial"; | ||
status = "pending"; | ||
@@ -134,10 +157,22 @@ if (!(paymentIntent.status === "requires_payment_method")) { | ||
case 7: | ||
if (paymentIntent.status === "requires_capture") { | ||
status = "authorized"; | ||
if (!(paymentIntent.status === "requires_confirmation")) { | ||
_context.next = 9; | ||
break; | ||
} | ||
if (paymentIntent.status === "succeeded") { | ||
status = "succeeded"; | ||
return _context.abrupt("return", status); | ||
case 9: | ||
if (!(paymentIntent.status === "processing")) { | ||
_context.next = 11; | ||
break; | ||
} | ||
return _context.abrupt("return", status); | ||
case 11: | ||
if (paymentIntent.status === "requires_action") { | ||
status = "requires_more"; | ||
} | ||
if (paymentIntent.status === "canceled") { | ||
@@ -147,5 +182,13 @@ status = "canceled"; | ||
if (paymentIntent.status === "requires_capture") { | ||
status = "authorized"; | ||
} | ||
if (paymentIntent.status === "succeeded") { | ||
status = "authorized"; | ||
} | ||
return _context.abrupt("return", status); | ||
case 11: | ||
case 16: | ||
case "end": | ||
@@ -164,2 +207,8 @@ return _context.stop(); | ||
}() | ||
/** | ||
* Fetches a customers saved payment methods if registered in Stripe. | ||
* @param {object} customer - customer to fetch saved cards for | ||
* @returns {Promise<Array<object>>} saved payments methods | ||
*/ | ||
}, { | ||
@@ -208,2 +257,8 @@ key: "retrieveSavedMethods", | ||
}() | ||
/** | ||
* Fetches a Stripe customer | ||
* @param {string} customerId - Stripe customer id | ||
* @returns {Promise<object>} Stripe customer | ||
*/ | ||
}, { | ||
@@ -242,3 +297,8 @@ key: "retrieveCustomer", | ||
return retrieveCustomer; | ||
}() // customer metadata | ||
}() | ||
/** | ||
* Creates a Stripe customer using a Medusa customer. | ||
* @param {object} customer - Customer data from Medusa | ||
* @returns {Promise<object>} Stripe customer | ||
*/ | ||
@@ -264,14 +324,24 @@ }, { | ||
stripeCustomer = _context4.sent; | ||
_context4.next = 6; | ||
return this.customerService_.setMetadata(customer._id, "stripe_id", stripeCustomer.id); | ||
case 6: | ||
if (!customer.id) { | ||
_context4.next = 7; | ||
break; | ||
} | ||
_context4.next = 7; | ||
return this.customerService_.update(customer.id, { | ||
metadata: { | ||
stripe_id: stripeCustomer.id | ||
} | ||
}); | ||
case 7: | ||
return _context4.abrupt("return", stripeCustomer); | ||
case 9: | ||
_context4.prev = 9; | ||
case 10: | ||
_context4.prev = 10; | ||
_context4.t0 = _context4["catch"](0); | ||
throw _context4.t0; | ||
case 12: | ||
case 13: | ||
case "end": | ||
@@ -281,3 +351,3 @@ return _context4.stop(); | ||
} | ||
}, _callee4, this, [[0, 9]]); | ||
}, _callee4, this, [[0, 10]]); | ||
})); | ||
@@ -292,6 +362,6 @@ | ||
/** | ||
* Creates Stripe PaymentIntent. | ||
* @param {string} cart - the cart to create a payment for | ||
* @param {number} amount - the amount to create a payment for | ||
* @returns {string} id of payment intent | ||
* Creates a Stripe payment intent. | ||
* If customer is not registered in Stripe, we do so. | ||
* @param {object} cart - cart to create a payment for | ||
* @returns {object} Stripe payment intent | ||
*/ | ||
@@ -305,3 +375,3 @@ | ||
regeneratorRuntime.mark(function _callee5(cart) { | ||
var customer_id, region_id, _ref2, currency_code, stripeCustomerId, _ref3, id, customer, _ref4, _id, amount, paymentIntent; | ||
var customer_id, region_id, email, _ref2, currency_code, amount, intentRequest, _customer$metadata, customer, stripeCustomer, _stripeCustomer, paymentIntent; | ||
@@ -312,3 +382,3 @@ return regeneratorRuntime.wrap(function _callee5$(_context5) { | ||
case 0: | ||
customer_id = cart.customer_id, region_id = cart.region_id; | ||
customer_id = cart.customer_id, region_id = cart.region_id, email = cart.email; | ||
_context5.next = 3; | ||
@@ -320,74 +390,71 @@ return this.regionService_.retrieve(region_id); | ||
currency_code = _ref2.currency_code; | ||
_context5.next = 7; | ||
return this.totalsService_.getTotal(cart); | ||
if (customer_id) { | ||
_context5.next = 13; | ||
case 7: | ||
amount = _context5.sent; | ||
intentRequest = { | ||
amount: amount, | ||
currency: currency_code, | ||
setup_future_usage: "on_session", | ||
capture_method: this.options_.capture ? "automatic" : "manual", | ||
metadata: { | ||
cart_id: "".concat(cart.id) | ||
} | ||
}; | ||
if (!customer_id) { | ||
_context5.next = 23; | ||
break; | ||
} | ||
_context5.next = 8; | ||
return this.stripe_.customers.create({ | ||
email: cart.email | ||
}); | ||
case 8: | ||
_ref3 = _context5.sent; | ||
id = _ref3.id; | ||
stripeCustomerId = id; | ||
_context5.next = 26; | ||
break; | ||
case 13: | ||
_context5.next = 15; | ||
_context5.next = 12; | ||
return this.customerService_.retrieve(customer_id); | ||
case 15: | ||
case 12: | ||
customer = _context5.sent; | ||
if (customer.metadata && customer.metadata.stripe_id) { | ||
_context5.next = 25; | ||
if (!((_customer$metadata = customer.metadata) !== null && _customer$metadata !== void 0 && _customer$metadata.stripe_id)) { | ||
_context5.next = 17; | ||
break; | ||
} | ||
intentRequest.customer = customer.metadata.stripe_id; | ||
_context5.next = 21; | ||
break; | ||
case 17: | ||
_context5.next = 19; | ||
return this.stripe_.customers.create({ | ||
email: customer.email | ||
return this.createCustomer({ | ||
email: email, | ||
id: customer_id | ||
}); | ||
case 19: | ||
_ref4 = _context5.sent; | ||
_id = _ref4.id; | ||
_context5.next = 23; | ||
return this.customerService_.setMetadata(customer._id, "stripe_id", _id); | ||
stripeCustomer = _context5.sent; | ||
intentRequest.customer = stripeCustomer.id; | ||
case 23: | ||
_context5.next = 26; | ||
case 21: | ||
_context5.next = 27; | ||
break; | ||
case 23: | ||
_context5.next = 25; | ||
return this.createCustomer({ | ||
email: email | ||
}); | ||
case 25: | ||
stripeCustomerId = customer.metadata.stripe_id; | ||
_stripeCustomer = _context5.sent; | ||
intentRequest.customer = _stripeCustomer.id; | ||
case 26: | ||
_context5.next = 28; | ||
return this.totalsService_.getTotal(cart); | ||
case 27: | ||
_context5.next = 29; | ||
return this.stripe_.paymentIntents.create(intentRequest); | ||
case 28: | ||
amount = _context5.sent; | ||
_context5.next = 31; | ||
return this.stripe_.paymentIntents.create({ | ||
customer: stripeCustomerId, | ||
amount: parseInt(amount * 100), | ||
// Stripe amount is in cents | ||
currency: currency_code, | ||
setup_future_usage: "on_session", | ||
capture_method: "manual", | ||
metadata: { | ||
cart_id: "".concat(cart._id) | ||
} | ||
}); | ||
case 31: | ||
case 29: | ||
paymentIntent = _context5.sent; | ||
return _context5.abrupt("return", paymentIntent); | ||
case 33: | ||
case 31: | ||
case "end": | ||
@@ -407,5 +474,5 @@ return _context5.stop(); | ||
/** | ||
* Retrieves Stripe PaymentIntent. | ||
* Retrieves Stripe payment intent. | ||
* @param {object} data - the data of the payment to retrieve | ||
* @returns {Object} Stripe PaymentIntent | ||
* @returns {Promise<object>} Stripe payment intent | ||
*/ | ||
@@ -446,15 +513,13 @@ | ||
/** | ||
* Updates Stripe PaymentIntent. | ||
* @param {object} data - The payment session data. | ||
* @param {Object} cart - the current cart value | ||
* @returns {Object} Stripe PaymentIntent | ||
* Gets a Stripe payment intent and returns it. | ||
* @param {object} sessionData - the data of the payment to retrieve | ||
* @returns {Promise<object>} Stripe payment intent | ||
*/ | ||
}, { | ||
key: "updatePayment", | ||
key: "getPaymentData", | ||
value: function () { | ||
var _updatePayment = _asyncToGenerator( | ||
var _getPaymentData = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
regeneratorRuntime.mark(function _callee7(data, cart) { | ||
var id, amount; | ||
regeneratorRuntime.mark(function _callee7(sessionData) { | ||
return regeneratorRuntime.wrap(function _callee7$(_context7) { | ||
@@ -465,18 +530,10 @@ while (1) { | ||
_context7.prev = 0; | ||
id = data.id; | ||
_context7.next = 4; | ||
return this.totalsService_.getTotal(cart); | ||
return _context7.abrupt("return", this.stripe_.paymentIntents.retrieve(sessionData.data.id)); | ||
case 4: | ||
amount = _context7.sent; | ||
return _context7.abrupt("return", this.stripe_.paymentIntents.update(id, { | ||
amount: parseInt(amount * 100) | ||
})); | ||
case 8: | ||
_context7.prev = 8; | ||
_context7.prev = 4; | ||
_context7.t0 = _context7["catch"](0); | ||
throw _context7.t0; | ||
case 11: | ||
case 7: | ||
case "end": | ||
@@ -486,6 +543,155 @@ return _context7.stop(); | ||
} | ||
}, _callee7, this, [[0, 8]]); | ||
}, _callee7, this, [[0, 4]]); | ||
})); | ||
function updatePayment(_x7, _x8) { | ||
function getPaymentData(_x7) { | ||
return _getPaymentData.apply(this, arguments); | ||
} | ||
return getPaymentData; | ||
}() | ||
/** | ||
* Authorizes Stripe payment intent by simply returning | ||
* the status for the payment intent in use. | ||
* @param {object} sessionData - payment session data | ||
* @param {object} context - properties relevant to current context | ||
* @returns {Promise<{ status: string, data: object }>} result with data and status | ||
*/ | ||
}, { | ||
key: "authorizePayment", | ||
value: function () { | ||
var _authorizePayment = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
regeneratorRuntime.mark(function _callee8(sessionData) { | ||
var context, | ||
stat, | ||
_args8 = arguments; | ||
return regeneratorRuntime.wrap(function _callee8$(_context8) { | ||
while (1) { | ||
switch (_context8.prev = _context8.next) { | ||
case 0: | ||
context = _args8.length > 1 && _args8[1] !== undefined ? _args8[1] : {}; | ||
_context8.next = 3; | ||
return this.getStatus(sessionData.data); | ||
case 3: | ||
stat = _context8.sent; | ||
_context8.prev = 4; | ||
return _context8.abrupt("return", { | ||
data: sessionData.data, | ||
status: stat | ||
}); | ||
case 8: | ||
_context8.prev = 8; | ||
_context8.t0 = _context8["catch"](4); | ||
throw _context8.t0; | ||
case 11: | ||
case "end": | ||
return _context8.stop(); | ||
} | ||
} | ||
}, _callee8, this, [[4, 8]]); | ||
})); | ||
function authorizePayment(_x8) { | ||
return _authorizePayment.apply(this, arguments); | ||
} | ||
return authorizePayment; | ||
}() | ||
}, { | ||
key: "updatePaymentData", | ||
value: function () { | ||
var _updatePaymentData = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
regeneratorRuntime.mark(function _callee9(sessionData, update) { | ||
return regeneratorRuntime.wrap(function _callee9$(_context9) { | ||
while (1) { | ||
switch (_context9.prev = _context9.next) { | ||
case 0: | ||
_context9.prev = 0; | ||
return _context9.abrupt("return", this.stripe_.paymentIntents.update(sessionData.id, _objectSpread({}, update.data))); | ||
case 4: | ||
_context9.prev = 4; | ||
_context9.t0 = _context9["catch"](0); | ||
throw _context9.t0; | ||
case 7: | ||
case "end": | ||
return _context9.stop(); | ||
} | ||
} | ||
}, _callee9, this, [[0, 4]]); | ||
})); | ||
function updatePaymentData(_x9, _x10) { | ||
return _updatePaymentData.apply(this, arguments); | ||
} | ||
return updatePaymentData; | ||
}() | ||
/** | ||
* Updates Stripe payment intent. | ||
* @param {object} sessionData - payment session data. | ||
* @param {object} update - objec to update intent with | ||
* @returns {object} Stripe payment intent | ||
*/ | ||
}, { | ||
key: "updatePayment", | ||
value: function () { | ||
var _updatePayment = _asyncToGenerator( | ||
/*#__PURE__*/ | ||
regeneratorRuntime.mark(function _callee10(sessionData, cart) { | ||
var _cart$customer, _cart$customer$metada, stripeId; | ||
return regeneratorRuntime.wrap(function _callee10$(_context10) { | ||
while (1) { | ||
switch (_context10.prev = _context10.next) { | ||
case 0: | ||
_context10.prev = 0; | ||
stripeId = ((_cart$customer = cart.customer) === null || _cart$customer === void 0 ? void 0 : (_cart$customer$metada = _cart$customer.metadata) === null || _cart$customer$metada === void 0 ? void 0 : _cart$customer$metada.stripe_id) || undefined; | ||
if (!(stripeId !== sessionData.customer)) { | ||
_context10.next = 6; | ||
break; | ||
} | ||
return _context10.abrupt("return", this.createPayment(cart)); | ||
case 6: | ||
if (!(cart.total && sessionData.amount === cart.total)) { | ||
_context10.next = 8; | ||
break; | ||
} | ||
return _context10.abrupt("return", sessionData); | ||
case 8: | ||
return _context10.abrupt("return", this.stripe_.paymentIntents.update(sessionData.id, { | ||
amount: cart.total | ||
})); | ||
case 9: | ||
_context10.next = 14; | ||
break; | ||
case 11: | ||
_context10.prev = 11; | ||
_context10.t0 = _context10["catch"](0); | ||
throw _context10.t0; | ||
case 14: | ||
case "end": | ||
return _context10.stop(); | ||
} | ||
} | ||
}, _callee10, this, [[0, 11]]); | ||
})); | ||
function updatePayment(_x11, _x12) { | ||
return _updatePayment.apply(this, arguments); | ||
@@ -501,11 +707,11 @@ } | ||
/*#__PURE__*/ | ||
regeneratorRuntime.mark(function _callee8(data) { | ||
regeneratorRuntime.mark(function _callee11(payment) { | ||
var id; | ||
return regeneratorRuntime.wrap(function _callee8$(_context8) { | ||
return regeneratorRuntime.wrap(function _callee11$(_context11) { | ||
while (1) { | ||
switch (_context8.prev = _context8.next) { | ||
switch (_context11.prev = _context11.next) { | ||
case 0: | ||
_context8.prev = 0; | ||
id = data.id; | ||
return _context8.abrupt("return", this.stripe_.paymentIntents.cancel(id)["catch"](function (err) { | ||
_context11.prev = 0; | ||
id = payment.data.id; | ||
return _context11.abrupt("return", this.stripe_.paymentIntents.cancel(id)["catch"](function (err) { | ||
if (err.statusCode === 400) { | ||
@@ -519,15 +725,15 @@ return; | ||
case 5: | ||
_context8.prev = 5; | ||
_context8.t0 = _context8["catch"](0); | ||
throw _context8.t0; | ||
_context11.prev = 5; | ||
_context11.t0 = _context11["catch"](0); | ||
throw _context11.t0; | ||
case 8: | ||
case "end": | ||
return _context8.stop(); | ||
return _context11.stop(); | ||
} | ||
} | ||
}, _callee8, this, [[0, 5]]); | ||
}, _callee11, this, [[0, 5]]); | ||
})); | ||
function deletePayment(_x9) { | ||
function deletePayment(_x13) { | ||
return _deletePayment.apply(this, arguments); | ||
@@ -539,6 +745,6 @@ } | ||
/** | ||
* Updates customer of Stripe PaymentIntent. | ||
* @param {string} cart - the cart to update payment intent for | ||
* @param {Object} data - the update object for the payment intent | ||
* @returns {Object} Stripe PaymentIntent | ||
* Updates customer of Stripe payment intent. | ||
* @param {string} paymentIntentId - id of payment intent to update | ||
* @param {string} customerId - id of new Stripe customer | ||
* @returns {object} Stripe payment intent | ||
*/ | ||
@@ -551,26 +757,26 @@ | ||
/*#__PURE__*/ | ||
regeneratorRuntime.mark(function _callee9(paymentIntent, id) { | ||
return regeneratorRuntime.wrap(function _callee9$(_context9) { | ||
regeneratorRuntime.mark(function _callee12(paymentIntentId, customerId) { | ||
return regeneratorRuntime.wrap(function _callee12$(_context12) { | ||
while (1) { | ||
switch (_context9.prev = _context9.next) { | ||
switch (_context12.prev = _context12.next) { | ||
case 0: | ||
_context9.prev = 0; | ||
return _context9.abrupt("return", this.stripe_.paymentIntents.update(paymentIntent, { | ||
customer: id | ||
_context12.prev = 0; | ||
return _context12.abrupt("return", this.stripe_.paymentIntents.update(paymentIntentId, { | ||
customer: customerId | ||
})); | ||
case 4: | ||
_context9.prev = 4; | ||
_context9.t0 = _context9["catch"](0); | ||
throw _context9.t0; | ||
_context12.prev = 4; | ||
_context12.t0 = _context12["catch"](0); | ||
throw _context12.t0; | ||
case 7: | ||
case "end": | ||
return _context9.stop(); | ||
return _context12.stop(); | ||
} | ||
} | ||
}, _callee9, this, [[0, 4]]); | ||
}, _callee12, this, [[0, 4]]); | ||
})); | ||
function updatePaymentIntentCustomer(_x10, _x11) { | ||
function updatePaymentIntentCustomer(_x14, _x15) { | ||
return _updatePaymentIntentCustomer.apply(this, arguments); | ||
@@ -582,5 +788,5 @@ } | ||
/** | ||
* Captures payment for Stripe PaymentIntent. | ||
* @param {Object} paymentData - payment method data from cart | ||
* @returns {Object} Stripe PaymentIntent | ||
* Captures payment for Stripe payment intent. | ||
* @param {object} paymentData - payment method data from cart | ||
* @returns {object} Stripe payment intent | ||
*/ | ||
@@ -593,26 +799,26 @@ | ||
/*#__PURE__*/ | ||
regeneratorRuntime.mark(function _callee10(paymentData) { | ||
regeneratorRuntime.mark(function _callee13(payment) { | ||
var id; | ||
return regeneratorRuntime.wrap(function _callee10$(_context10) { | ||
return regeneratorRuntime.wrap(function _callee13$(_context13) { | ||
while (1) { | ||
switch (_context10.prev = _context10.next) { | ||
switch (_context13.prev = _context13.next) { | ||
case 0: | ||
id = paymentData.id; | ||
_context10.prev = 1; | ||
return _context10.abrupt("return", this.stripe_.paymentIntents.capture(id)); | ||
id = payment.data.id; | ||
_context13.prev = 1; | ||
return _context13.abrupt("return", this.stripe_.paymentIntents.capture(id)); | ||
case 5: | ||
_context10.prev = 5; | ||
_context10.t0 = _context10["catch"](1); | ||
throw _context10.t0; | ||
_context13.prev = 5; | ||
_context13.t0 = _context13["catch"](1); | ||
throw _context13.t0; | ||
case 8: | ||
case "end": | ||
return _context10.stop(); | ||
return _context13.stop(); | ||
} | ||
} | ||
}, _callee10, this, [[1, 5]]); | ||
}, _callee13, this, [[1, 5]]); | ||
})); | ||
function capturePayment(_x12) { | ||
function capturePayment(_x16) { | ||
return _capturePayment.apply(this, arguments); | ||
@@ -624,5 +830,6 @@ } | ||
/** | ||
* Refunds payment for Stripe PaymentIntent. | ||
* @param {Object} paymentData - payment method data from cart | ||
* @returns {string} id of payment intent | ||
* Refunds payment for Stripe payment intent. | ||
* @param {object} paymentData - payment method data from cart | ||
* @param {number} amountToRefund - amount to refund | ||
* @returns {string} refunded payment intent | ||
*/ | ||
@@ -635,29 +842,33 @@ | ||
/*#__PURE__*/ | ||
regeneratorRuntime.mark(function _callee11(paymentData, amount) { | ||
regeneratorRuntime.mark(function _callee14(payment, amountToRefund) { | ||
var id; | ||
return regeneratorRuntime.wrap(function _callee11$(_context11) { | ||
return regeneratorRuntime.wrap(function _callee14$(_context14) { | ||
while (1) { | ||
switch (_context11.prev = _context11.next) { | ||
switch (_context14.prev = _context14.next) { | ||
case 0: | ||
id = paymentData.id; | ||
_context11.prev = 1; | ||
return _context11.abrupt("return", this.stripe_.refunds.create({ | ||
amount: parseInt(amount * 100), | ||
id = payment.data.id; | ||
_context14.prev = 1; | ||
_context14.next = 4; | ||
return this.stripe_.refunds.create({ | ||
amount: amountToRefund, | ||
payment_intent: id | ||
})); | ||
}); | ||
case 5: | ||
_context11.prev = 5; | ||
_context11.t0 = _context11["catch"](1); | ||
throw _context11.t0; | ||
case 4: | ||
return _context14.abrupt("return", payment.data); | ||
case 8: | ||
case 7: | ||
_context14.prev = 7; | ||
_context14.t0 = _context14["catch"](1); | ||
throw _context14.t0; | ||
case 10: | ||
case "end": | ||
return _context11.stop(); | ||
return _context14.stop(); | ||
} | ||
} | ||
}, _callee11, this, [[1, 5]]); | ||
}, _callee14, this, [[1, 7]]); | ||
})); | ||
function refundPayment(_x13, _x14) { | ||
function refundPayment(_x17, _x18) { | ||
return _refundPayment.apply(this, arguments); | ||
@@ -669,5 +880,5 @@ } | ||
/** | ||
* Cancels payment for Stripe PaymentIntent. | ||
* @param {Object} paymentData - payment method data from cart | ||
* @returns {string} id of payment intent | ||
* Cancels payment for Stripe payment intent. | ||
* @param {object} paymentData - payment method data from cart | ||
* @returns {object} canceled payment intent | ||
*/ | ||
@@ -680,40 +891,35 @@ | ||
/*#__PURE__*/ | ||
regeneratorRuntime.mark(function _callee12(paymentData) { | ||
var id, result; | ||
return regeneratorRuntime.wrap(function _callee12$(_context12) { | ||
regeneratorRuntime.mark(function _callee15(payment) { | ||
var id; | ||
return regeneratorRuntime.wrap(function _callee15$(_context15) { | ||
while (1) { | ||
switch (_context12.prev = _context12.next) { | ||
switch (_context15.prev = _context15.next) { | ||
case 0: | ||
id = paymentData.id; | ||
_context12.prev = 1; | ||
_context12.next = 4; | ||
return this.stripe_.paymentIntents.cancel(id); | ||
id = payment.data.id; | ||
_context15.prev = 1; | ||
return _context15.abrupt("return", this.stripe_.paymentIntents.cancel(id)); | ||
case 4: | ||
result = _context12.sent; | ||
return _context12.abrupt("return", result); | ||
case 5: | ||
_context15.prev = 5; | ||
_context15.t0 = _context15["catch"](1); | ||
case 8: | ||
_context12.prev = 8; | ||
_context12.t0 = _context12["catch"](1); | ||
if (!(_context12.t0.payment_intent.status === "canceled")) { | ||
_context12.next = 12; | ||
if (!(_context15.t0.payment_intent.status === "canceled")) { | ||
_context15.next = 9; | ||
break; | ||
} | ||
return _context12.abrupt("return", _context12.t0.payment_intent); | ||
return _context15.abrupt("return", _context15.t0.payment_intent); | ||
case 12: | ||
throw _context12.t0; | ||
case 9: | ||
throw _context15.t0; | ||
case 13: | ||
case 10: | ||
case "end": | ||
return _context12.stop(); | ||
return _context15.stop(); | ||
} | ||
} | ||
}, _callee12, this, [[1, 8]]); | ||
}, _callee15, this, [[1, 5]]); | ||
})); | ||
function cancelPayment(_x15) { | ||
function cancelPayment(_x19) { | ||
return _cancelPayment.apply(this, arguments); | ||
@@ -726,6 +932,6 @@ } | ||
* Constructs Stripe Webhook event | ||
* @param {Object} data - the data of the webhook request: req.body | ||
* @param {Object} signature - the Stripe signature on the event, that | ||
* @param {object} data - the data of the webhook request: req.body | ||
* @param {object} signature - the Stripe signature on the event, that | ||
* ensures integrity of the webhook event | ||
* @returns {Object} Stripe Webhook event | ||
* @returns {object} Stripe Webhook event | ||
*/ | ||
@@ -732,0 +938,0 @@ |
@@ -28,3 +28,3 @@ "use strict"; | ||
customerService = _ref.customerService, | ||
stripeProviderService = _ref.stripeProviderService, | ||
paymentProviderService = _ref.paymentProviderService, | ||
eventBusService = _ref.eventBusService; | ||
@@ -36,3 +36,3 @@ | ||
this.customerService_ = customerService; | ||
this.stripeProviderService_ = stripeProviderService; | ||
this.paymentProviderService_ = paymentProviderService; | ||
this.eventBus_ = eventBusService; | ||
@@ -71,4 +71,6 @@ this.eventBus_.subscribe("cart.customer_updated", | ||
/*#__PURE__*/ | ||
regeneratorRuntime.mark(function _callee2(cart) { | ||
var customer_id, payment_sessions, customer, stripeSession, paymentIntent, stripeCustomer, newPaymentIntent; | ||
regeneratorRuntime.mark(function _callee2(cartId) { | ||
var _cart$payment_session; | ||
var cart, session; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
@@ -78,6 +80,13 @@ while (1) { | ||
case 0: | ||
customer_id = cart.customer_id, payment_sessions = cart.payment_sessions; | ||
_context2.next = 2; | ||
return this.cartService_.retrieve(cartId, { | ||
select: ["subtotal", "tax_total", "shipping_total", "discount_total", "total"], | ||
relations: ["items", "billing_address", "shipping_address", "region", "region.payment_providers", "payment_sessions", "customer"] | ||
}); | ||
if (payment_sessions) { | ||
_context2.next = 3; | ||
case 2: | ||
cart = _context2.sent; | ||
if ((_cart$payment_session = cart.payment_sessions) !== null && _cart$payment_session !== void 0 && _cart$payment_session.length) { | ||
_context2.next = 5; | ||
break; | ||
@@ -88,87 +97,15 @@ } | ||
case 3: | ||
_context2.next = 5; | ||
return this.customerService_.retrieve(customer_id); | ||
case 5: | ||
customer = _context2.sent; | ||
stripeSession = payment_sessions.find(function (s) { | ||
return s.provider_id === "stripe"; | ||
session = cart.payment_sessions.find(function (ps) { | ||
return ps.provider_id === "stripe"; | ||
}); | ||
if (stripeSession) { | ||
_context2.next = 9; | ||
if (!session) { | ||
_context2.next = 8; | ||
break; | ||
} | ||
return _context2.abrupt("return", Promise.resolve()); | ||
return _context2.abrupt("return", this.paymentProviderService_.updateSession(session, cart)); | ||
case 9: | ||
_context2.next = 11; | ||
return this.stripeProviderService_.retrievePayment(stripeSession.data); | ||
case 11: | ||
paymentIntent = _context2.sent; | ||
if (!(customer.metadata && customer.metadata.stripe_id)) { | ||
_context2.next = 16; | ||
break; | ||
} | ||
_context2.next = 15; | ||
return this.stripeProviderService_.retrieveCustomer(customer.metadata.stripe_id); | ||
case 15: | ||
stripeCustomer = _context2.sent; | ||
case 16: | ||
if (stripeCustomer) { | ||
_context2.next = 20; | ||
break; | ||
} | ||
_context2.next = 19; | ||
return this.stripeProviderService_.createCustomer(customer); | ||
case 19: | ||
stripeCustomer = _context2.sent; | ||
case 20: | ||
if (!(stripeCustomer.id === paymentIntent.customer)) { | ||
_context2.next = 22; | ||
break; | ||
} | ||
return _context2.abrupt("return", Promise.resolve()); | ||
case 22: | ||
if (paymentIntent.customer) { | ||
_context2.next = 24; | ||
break; | ||
} | ||
return _context2.abrupt("return", this.stripeProviderService_.updatePaymentIntentCustomer(stripeCustomer.id)); | ||
case 24: | ||
if (!(stripeCustomer.id !== paymentIntent.customer)) { | ||
_context2.next = 32; | ||
break; | ||
} | ||
_context2.next = 27; | ||
return this.stripeProviderService_.cancelPayment(paymentIntent); | ||
case 27: | ||
_context2.next = 29; | ||
return this.stripeProviderService_.createPayment(cart); | ||
case 29: | ||
newPaymentIntent = _context2.sent; | ||
_context2.next = 32; | ||
return this.cartService_.updatePaymentSession(cart._id, "stripe", newPaymentIntent); | ||
case 32: | ||
return _context2.abrupt("return", Promise.resolve()); | ||
case 33: | ||
case 8: | ||
case "end": | ||
@@ -175,0 +112,0 @@ return _context2.stop(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
58027
16
17
1405
Updatedmedusa-core-utils@^1.1.0