Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 36 additions & 25 deletions lib/models/invoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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`
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
50 changes: 50 additions & 0 deletions lib/models/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/recurly-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
}
Expand Down
35 changes: 21 additions & 14 deletions test/test-02-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

Expand All @@ -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'
}

Expand Down Expand Up @@ -344,7 +344,7 @@ describe('Subscription', () => {
// address2: 'Suite 500',
// city: 'San Francisco',
// state: 'CA',
// country: 'USA',
// country: 'US',
// zip: '94102'
// }
//
Expand Down Expand Up @@ -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()

Expand All @@ -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()
})
})
Expand All @@ -471,8 +475,7 @@ describe('Subscription', () => {
})

describe.skip('Coupons', () => {
let coupon,
couponCode
let coupon, couponCode

it('can create a coupon', done => {
couponCode = uuid.v4()
Expand Down Expand Up @@ -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()
Expand All @@ -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 }

Expand All @@ -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()
Expand Down Expand Up @@ -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'
}

Expand Down