Skip to content

Commit

Permalink
[api] Add missing actions to Checkout resource
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed Apr 29, 2017
1 parent 3329331 commit 41f66c8
Show file tree
Hide file tree
Showing 13 changed files with 583 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -152,8 +152,13 @@ shopify.order.list({ limit: 5 })
- `list()`
- `update(id, params)`
- checkout
- `complete(token)`
- `count([params])`
- `create(params)`
- `get(token)`
- `list([params])`
- `shippingRates(token)`
- `update(token, params)`
- collect
- `count([params])`
- `create(params)`
Expand Down
29 changes: 27 additions & 2 deletions resources/checkout.js
@@ -1,7 +1,7 @@
'use strict';

const assign = require('lodash/assign');
const pick = require('lodash/pick');
const omit = require('lodash/omit');

const base = require('../mixins/base');

Expand All @@ -19,6 +19,31 @@ function Checkout(shopify) {
this.key = 'checkout';
}

assign(Checkout.prototype, pick(base, ['count', 'list', 'buildUrl']));
assign(Checkout.prototype, omit(base, 'delete'));

/**
* Completes a free checkout.
*
* @param {String} token Checkout token
* @return {Promise} Promise that resolves with the result
* @public
*/
Checkout.prototype.complete = function complete(token) {
const url = this.buildUrl(`${token}/complete`);
return this.shopify.request(url, 'POST', undefined, {})
.then(body => body[this.key]);
};

/**
* Gets a list of available shipping rates for the specified checkout.
*
* @param {String} token Checkout token
* @return {Promise} Promise that resolves with the result
* @public
*/
Checkout.prototype.shippingRates = function shippingRates(token) {
const url = this.buildUrl(`${token}/shipping_rates`);
return this.shopify.request(url, 'GET', 'shipping_rates');
};

module.exports = Checkout;
60 changes: 60 additions & 0 deletions test/checkout.test.js
Expand Up @@ -50,4 +50,64 @@ describe('Shopify#checkout', () => {
return shopify.checkout.list({ foo: 'bar' })
.then(data => expect(data).to.deep.equal(output.checkouts));
});

it('creates a new checkout', () => {
const input = fixtures.req.create;
const output = fixtures.res.create;

scope
.post('/admin/checkouts.json', input)
.reply(201, output);

return shopify.checkout.create(input.checkout)
.then(data => expect(data).to.deep.equal(output.checkout));
});

it('completes a free checkout', () => {
const token = 'b490a9220cd14d7344024f4874f640a6';
const output = fixtures.res.complete;

scope
.post(`/admin/checkouts/${token}/complete.json`, {})
.reply(202, output);

return shopify.checkout.complete(token)
.then(data => expect(data).to.deep.equal(output.checkout));
});

it('gets a single checkout by its token', () => {
const output = fixtures.res.get;

scope
.get('/admin/checkouts/bd5a8aa1ecd019dd3520ff791ee3a24c.json')
.reply(200, output);

return shopify.checkout.get('bd5a8aa1ecd019dd3520ff791ee3a24c')
.then(data => expect(data).to.deep.equal(output.checkout));
});

it('updates an existing checkout', () => {
const token = 'exuw7apwoycchjuwtiqg8nytfhphr62a';
const input = fixtures.req.update;
const output = fixtures.res.update;

scope
.put(`/admin/checkouts/${token}.json`, input)
.reply(202, output);

return shopify.checkout.update(token, input.checkout)
.then(data => expect(data).to.deep.equal(output.checkout));
});

it('gets a list of shipping rates', () => {
const token = 'exuw7apwoycchjuwtiqg8nytfhphr62a';
const output = fixtures.res.shippingRates;

scope
.get(`/admin/checkouts/${token}/shipping_rates.json`)
.reply(200, output);

return shopify.checkout.shippingRates(token)
.then(data => expect(data).to.deep.equal(output.shipping_rates));
});
});
1 change: 1 addition & 0 deletions test/fixtures/checkout/index.js
@@ -1,3 +1,4 @@
'use strict';

exports.req = require('./req');
exports.res = require('./res');
10 changes: 10 additions & 0 deletions test/fixtures/checkout/req/create.json
@@ -0,0 +1,10 @@
{
"checkout": {
"line_items": [
{
"variant_id": 39072856,
"quantity": 5
}
]
}
}
4 changes: 4 additions & 0 deletions test/fixtures/checkout/req/index.js
@@ -0,0 +1,4 @@
'use strict';

exports.create = require('./create');
exports.update = require('./update');
16 changes: 16 additions & 0 deletions test/fixtures/checkout/req/update.json
@@ -0,0 +1,16 @@
{
"checkout": {
"token": "exuw7apwoycchjuwtiqg8nytfhphr62a",
"email": "john.smith@example.com",
"shipping_address": {
"first_name": "John",
"last_name": "Smith",
"address1": "126 York St.",
"city": "Los Angeles",
"province_code": "CA",
"country_code": "US",
"phone": "(123)456-7890",
"zip": "90002"
}
}
}
101 changes: 101 additions & 0 deletions test/fixtures/checkout/res/complete.json
@@ -0,0 +1,101 @@
{
"checkout": {
"completed_at": null,
"created_at": "2012-10-12T07:05:27-04:00",
"currency": "USD",
"customer_id": 207119551,
"device_id": null,
"discount_code": null,
"email": "bob.norman@hostmail.com",
"location_id": null,
"name": "#86568385",
"note": "",
"note_attributes": {
"custom engraving": "Happy Birthday",
"colour": "green"
},
"order_id": null,
"order": null,
"payment_due": "0.00",
"payment_url": "https://app.local/cardserver/sessions",
"payments": [],
"privacy_policy_url": null,
"refund_policy_url": null,
"requires_shipping": false,
"reservation_time_left": 0,
"reservation_time": null,
"source_identifier": null,
"source_name": "web",
"source_url": null,
"subtotal_price": "0.00",
"tax_exempt": false,
"taxes_included": false,
"terms_of_service_url": null,
"token": "b490a9220cd14d7344024f4874f640a6",
"total_price": "0.00",
"total_tax": "0.00",
"updated_at": "2017-03-20T14:02:07-04:00",
"user_id": null,
"web_url": "https://checkout.local/690933842/checkouts/b490a9220cd14d7344024f4874f640a6",
"web_processing_url": "https://checkout.local/690933842/checkouts/b490a9220cd14d7344024f4874f640a6/processing",
"line_items": [
{
"id": 49148385,
"key": 49148385,
"product_id": 632910392,
"variant_id": 49148385,
"sku": "IPOD2008RED",
"vendor": "Apple",
"title": "IPod Nano - 8GB",
"variant_title": "Red",
"taxable": true,
"requires_shipping": false,
"price": "0.00",
"compare_at_price": null,
"line_price": "0.00",
"properties": {},
"quantity": 1,
"grams": 200,
"fulfillment_service": "manual",
"applied_discounts": []
}
],
"gift_cards": [],
"tax_lines": [],
"tax_manipulations": [],
"shipping_line": null,
"shipping_rate": null,
"shipping_address": {
"id": 550558813,
"first_name": "Bob",
"last_name": "Norman",
"phone": "555-625-1199",
"company": null,
"address1": "Chestnut Street 92",
"address2": "",
"city": "Louisville",
"province": "Kentucky",
"province_code": "KY",
"country": "United States",
"country_code": "US",
"zip": "40202"
},
"credit_card": null,
"billing_address": {
"id": 550558813,
"first_name": "Bob",
"last_name": "Norman",
"phone": "555-625-1199",
"company": null,
"address1": "Chestnut Street 92",
"address2": "",
"city": "Louisville",
"province": "Kentucky",
"province_code": "KY",
"country": "United States",
"country_code": "US",
"zip": "40202"
},
"applied_discount": null
}
}
70 changes: 70 additions & 0 deletions test/fixtures/checkout/res/create.json
@@ -0,0 +1,70 @@
{
"checkout": {
"completed_at": null,
"created_at": "2017-03-20T14:02:12-04:00",
"currency": "USD",
"customer_id": null,
"device_id": null,
"discount_code": null,
"email": null,
"location_id": null,
"name": "#1066348318",
"note": "",
"note_attributes": {},
"order_id": null,
"order": null,
"payment_due": "995.00",
"payment_url": "https://app.local/cardserver/sessions",
"payments": [],
"privacy_policy_url": null,
"refund_policy_url": null,
"requires_shipping": true,
"reservation_time_left": 300,
"reservation_time": 300,
"source_identifier": null,
"source_name": "755357713",
"source_url": null,
"subtotal_price": "995.00",
"tax_exempt": false,
"taxes_included": false,
"terms_of_service_url": null,
"token": "2c63454d64c96cc1e6657aaa6899d8f7",
"total_price": "995.00",
"total_tax": "0.00",
"updated_at": "2017-03-20T14:02:12-04:00",
"user_id": null,
"web_url": "https://checkout.local/690933842/checkouts/2c63454d64c96cc1e6657aaa6899d8f7",
"web_processing_url": "https://checkout.local/690933842/checkouts/2c63454d64c96cc1e6657aaa6899d8f7/processing",
"line_items": [
{
"id": "983b87404e074fe98e070c5de624bb54",
"key": "983b87404e074fe98e070c5de624bb54",
"product_id": 632910392,
"variant_id": 39072856,
"sku": "IPOD2008GREEN",
"vendor": "Apple",
"title": "IPod Nano - 8GB",
"variant_title": "Green",
"taxable": true,
"requires_shipping": true,
"price": "199.00",
"compare_at_price": null,
"line_price": "995.00",
"properties": {},
"quantity": 5,
"grams": 567,
"fulfillment_service": "manual",
"applied_discounts": []
}
],
"gift_cards": [],
"tax_lines": [],
"tax_manipulations": [],
"shipping_line": null,
"shipping_rate": null,
"shipping_address": null,
"credit_card": null,
"billing_address": null,
"applied_discount": null
}
}

0 comments on commit 41f66c8

Please sign in to comment.