diff --git a/lib/models/invoice.js b/lib/models/invoice.js index 62a2170..e22c72f 100644 --- a/lib/models/invoice.js +++ b/lib/models/invoice.js @@ -63,25 +63,30 @@ class Invoice extends RecurlyData { fetchPDF(callback) { if (!this.href) { - throw (new Error('cannot fetch a record without an href')) + throw new Error('cannot fetch a record without an href') } - this.get(this.href, { }, { - headers: { - Accept: 'application/pdf' + this.get( + this.href, + {}, + { + headers: { + Accept: 'application/pdf' + }, + encoding: null, + noParse: true }, - encoding: null, - noParse: true - }, (err, response, payload) => { - if (err) { - return callback(err) + (err, response, payload) => { + if (err) { + return callback(err) + } + if (response.statusCode === 404) { + return callback(new Error('not_found')) + } + + callback(err, payload) } - if (response.statusCode === 404) { - return callback(new Error('not_found')) - } - - callback(err, payload) - }) + ) } refund(options, callback) { @@ -90,16 +95,20 @@ class Invoice extends RecurlyData { options = null } - options = _.defaults({ - amount_in_cents: undefined, - refund_apply_order: undefined - }, options, { refund_apply_order: 'credit' }) + options = _.defaults( + { + amount_in_cents: undefined, + refund_method: undefined + }, + options, + { refund_method: 'credit_first' } + ) debug('Got options as %o', options) let uri = _.get(this, 'a.refund.href') if (!uri && !this.invoice_number) { - throw (new Error('cannot refund an invoice without an invoice_number')) + throw new Error('cannot refund an invoice without an invoice_number') } uri = uri || `${Invoice.ENDPOINT}/${this.invoice_number}/refund` @@ -108,7 +117,7 @@ class Invoice extends RecurlyData { debug('Calling refund uri %s with body %o', uri, body) this.post(uri, body, (err, response, payload) => { - const error = handleRecurlyError(err, response, payload, [ 201 ]) + const error = handleRecurlyError(err, response, payload, [201]) if (error) { return callback(error) } @@ -120,13 +129,15 @@ class Invoice extends RecurlyData { markSuccessful(callback) { let uri = _.get(this, 'a.mark_successful.href') if (!uri && !this.invoice_number) { - throw (new Error('cannot mark invoice as successful without an invoice_number')) + throw new Error( + 'cannot mark invoice as successful without an invoice_number' + ) } uri = uri || `${Invoice.ENDPOINT}/${this.invoice_number}/mark_successful` this.put(uri, null, (err, response, payload) => { - const error = handleRecurlyError(err, response, payload, [ 200 ]) + const error = handleRecurlyError(err, response, payload, [200]) if (error) { return callback(error) } @@ -138,13 +149,13 @@ class Invoice extends RecurlyData { markFailed(callback) { let uri = _.get(this, 'a.mark_failed.href') if (!uri && !this.invoice_number) { - throw (new Error('cannot mark invoice as failed without an invoice_number')) + throw new Error('cannot mark invoice as failed without an invoice_number') } uri = uri || `${Invoice.ENDPOINT}/${this.invoice_number}/mark_failed` this.put(uri, null, (err, response, payload) => { - const error = handleRecurlyError(err, response, payload, [ 200 ]) + const error = handleRecurlyError(err, response, payload, [200]) if (error) { return callback(error) } diff --git a/lib/models/subscription.js b/lib/models/subscription.js index d34bb29..026b6a0 100644 --- a/lib/models/subscription.js +++ b/lib/models/subscription.js @@ -240,6 +240,56 @@ class Subscription extends RecurlyData { callback(null, this) }) } + + pause(pauseCycles, callback) { + if (!this.id) { + throw (new Error('cannot pause a subscription without a uuid')) + } + let href + if (this.a && this.a.pause) { + href = this.a.pause.href + } + else { + href = `${Subscription.ENDPOINT}/${this.id}/pause` + } + + const body = data2xml(Subscription.SINGULAR, { remaining_pause_cycles: pauseCycles }) + + + this.put(href, body, (err, response, payload) => { + const error = handleRecurlyError(err, response, payload, [ 200, 201 ]) + if (error) { + return callback(error) + } + + this.inflate(payload) + callback(null, this) + }) + } + + resume(callback) { + if (!this.id) { + throw (new Error('cannot resume a subscription without a uuid')) + } + + let href + if (this.a && this.a.resume) { + href = this.a.resume.href + } + else { + href = `${Subscription.ENDPOINT}/${this.id}/resume` + } + + this.put(href, '', (err, response, payload) => { + const error = handleRecurlyError(err, response, payload, [ 200 ]) + if (error) { + return callback(error) + } + + this.inflate(payload) + callback(null, this) + }) + } } module.exports = Subscription diff --git a/lib/recurly-data.js b/lib/recurly-data.js index 04878be..7b3054f 100644 --- a/lib/recurly-data.js +++ b/lib/recurly-data.js @@ -64,7 +64,7 @@ class RecurlyData { 'Accept': 'application/xml', 'Authorization': this._recurring.AUTH_BASIC, 'User-Agent': `${pkg.name}/${pkg.version}`, - 'X-Api-Version': '2.7' + 'X-Api-Version': '2.17' } } } diff --git a/test/test-02-api.js b/test/test-02-api.js index 36a5a3d..1e334a2 100644 --- a/test/test-02-api.js +++ b/test/test-02-api.js @@ -246,13 +246,13 @@ describe('BillingInfo', () => { last_name: account.last_name, number: '4000-0000-0000-0077', month: 1, - year: (new Date()).getFullYear() + 3, + year: new Date().getFullYear() + 3, verification_value: '111', address1: '760 Market Street', address2: 'Suite 500', city: 'San Francisco', state: 'CA', - country: 'USA', + country: 'US', zip: '94102' } @@ -271,13 +271,13 @@ describe('BillingInfo', () => { last_name: account.last_name, number: '4111-1111-1111-1111', month: 1, - year: (new Date()).getFullYear() + 3, + year: new Date().getFullYear() + 3, verification_value: '111', address1: '760 Market Street', address2: 'Suite 500', city: 'San Francisco', state: 'CA', - country: 'USA', + country: 'US', zip: '94102' } @@ -344,7 +344,7 @@ describe('Subscription', () => { // address2: 'Suite 500', // city: 'San Francisco', // state: 'CA', - // country: 'USA', + // country: 'US', // zip: '94102' // } // @@ -441,7 +441,9 @@ describe('Subscription', () => { demand(err).not.exist() subscription.state.must.equal('active') subscription.activated_at.must.be.a.date() - subscription.activated_at.getTime().must.equal(subscription.current_period_started_at.getTime()) + subscription.activated_at + .getTime() + .must.equal(subscription.current_period_started_at.getTime()) subscription.canceled_at.must.be.a.string() subscription.expires_at.must.be.a.string() @@ -455,7 +457,9 @@ describe('Subscription', () => { subscription.postpone(nextDate, err => { demand(err).not.exist() - nextDate.getTime().must.equal(subscription.current_period_ends_at.getTime()) + nextDate + .getTime() + .must.equal(subscription.current_period_ends_at.getTime()) done() }) }) @@ -471,8 +475,7 @@ describe('Subscription', () => { }) describe.skip('Coupons', () => { - let coupon, - couponCode + let coupon, couponCode it('can create a coupon', done => { couponCode = uuid.v4() @@ -654,7 +657,7 @@ describe('Invoices', () => { describe('refunds', () => { before(function(done) { - recurly.Invoice().all({state: 'collected'}, (err, invoices) => { + recurly.Invoice().all({ state: 'paid' }, (err, invoices) => { demand(err).not.exist() this.invoices = invoices done() @@ -671,7 +674,9 @@ describe('Invoices', () => { }) it('can issue an open amount refund for a specific amount against an invoice', function(done) { - const refundableInvoice = _.find(this.invoices, invoice => _.get(invoice, 'a.refund')) + const refundableInvoice = _.find(this.invoices, invoice => + _.get(invoice, 'a.refund') + ) debug('invoice to refund', refundableInvoice) const refundOptions = { amount_in_cents: 5 } @@ -696,7 +701,9 @@ describe('Invoices', () => { }) it('can issue an open amount refund for the full amount against an invoice', function(done) { - const refundableInvoice = _.findLast(this.invoices, invoice => _.get(invoice, 'a.refund')) + const refundableInvoice = _.findLast(this.invoices, invoice => + _.get(invoice, 'a.refund') + ) debug('invoice to refund', refundableInvoice) const invoice = recurly.Invoice() @@ -864,13 +871,13 @@ describe('RecurlyError', () => { last_name: this.account.properties.last_name, number: '4000-0000-0000-0101', month: 1, - year: (new Date()).getFullYear() + 3, + year: new Date().getFullYear() + 3, verification_value: '111', address1: '760 Market Street', address2: 'Suite 500', city: 'San Francisco', state: 'CA', - country: 'USA', + country: 'US', zip: '94102' }