Skip to content

Commit

Permalink
use product accounting data instead of last sale to be able to update…
Browse files Browse the repository at this point in the history
… customer addresses
  • Loading branch information
pofider committed Jan 19, 2022
1 parent 6221474 commit 1df249f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
8 changes: 3 additions & 5 deletions dist/lib/payments/subscriptionRenewal.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/lib/payments/subscriptionRenewal.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions src/lib/payments/subscriptionRenewal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,14 @@ export default class SubscriptionRenewal {
return this._processCancellation(customer, product)
}

const lastSale = product.sales[product.sales.length - 1]
let paymentIntent
try {
logger.info(`Initiating charge for ${product.name} for ${customer.email} of ${lastSale.accountingData.amount} USD`)
logger.info(`Initiating charge for ${product.name} for ${customer.email} of ${product.accountingData.amount} USD`)
const stripeCustomer = await this.services.stripe.findOrCreateCustomer(customer.email)
paymentIntent = await this.services.stripe.createConfirmedPaymentIntent(
stripeCustomer.id,
product.subscription.stripe.paymentMethodId,
lastSale.accountingData.amount
product.accountingData.amount
)
} catch (e) {
if (product.subscription.retryPlannedPayment) {
Expand Down Expand Up @@ -115,8 +114,7 @@ export default class SubscriptionRenewal {
}

async processSucesfullPayment(customer, product, paymentIntent) {
const lastSale = product.sales[product.sales.length - 1]
const sale = await this.services.customerRepository.createSale(lastSale.accountingData, {
const sale = await this.services.customerRepository.createSale(product.accountingData, {
paymentIntentId: paymentIntent.id,
})

Expand Down
8 changes: 6 additions & 2 deletions test/payments/subscriptionRenewalTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ databaseTest((getDb) => {
customerRepository,
stripe,
sendEmail: async (m) => emails.push(m),
renderInvoice: async (s: Sale) => {},
renderInvoice: async (s: Sale) => { },
notifyLicensingServer: async (customer, product, sale) => {
licensingServerNotifications.push({
customer,
Expand All @@ -41,6 +41,8 @@ databaseTest((getDb) => {
let customer = await customerRepository.findOrCreate('[email protected]')
const product = createProduct()
const originalNextPayment = (product.subscription.nextPayment = moment().add('-1', 'days').toDate())
product.accountingData.email = '[email protected]'
product.accountingData.vatNumber = 'DIFERENT'
customer.products = [product]
await customerRepository.update(customer)

Expand Down Expand Up @@ -74,7 +76,9 @@ databaseTest((getDb) => {
emails[0].to.should.be.eql(customer.email)
emails[0].content.should.containEql('renewed')

customer.products[0].sales[1].accountingData.amount.should.be.eql(customer.products[0].sales[0].accountingData.amount)
customer.products[0].sales[1].accountingData.amount.should.be.eql(customer.products[0].accountingData.amount)
customer.products[0].sales[1].accountingData.email.should.be.eql(customer.products[0].accountingData.email)
customer.products[0].sales[1].accountingData.vatNumber.should.be.eql(customer.products[0].accountingData.vatNumber)

licensingServerNotifications[0].customer.email.should.be.eql(customer.email)
})
Expand Down

0 comments on commit 1df249f

Please sign in to comment.