Skip to content

Commit

Permalink
Merge pull request #37 from DripEmail/orders
Browse files Browse the repository at this point in the history
Orders API
  • Loading branch information
samudary committed Mar 27, 2018
2 parents dce59be + 6f9366c commit 77f5ff9
Show file tree
Hide file tree
Showing 15 changed files with 236 additions and 123 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,15 @@ as methods on the client object. The following methods are currently available:
| List all forms | `#forms` |
| Fetch a form | `#form(id)` |

#### Purchases
#### Orders

**Note:** The beta purchases endpoint has been deprecated and its methods have been removed from the gem except `create_purchase`, which now sends requests to the Order creation endpoint [here](https://developer.drip.com/#orders)

| Actions | Methods |
| :------------------------- | :--------------------------------------------------- |
| List purchases for a subscriber | `#purchases(email)` |
| Create a purchase         | `#create_purchase(email, amount, options = {})` |
| Fetch a purchase | `#purchase(email, id)` |
| Create or update an order | `#create_or_update_order(email, options = {})` |
| Create or update a batch of orders | `#create_or_update_orders(orders = {})` |
| Create or update a refund | `#create_or_update_refund(options = {})` |

#### Subscribers

Expand Down
2 changes: 2 additions & 0 deletions lib/drip/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require "drip/client/custom_fields"
require "drip/client/events"
require "drip/client/forms"
require "drip/client/orders"
require "drip/client/purchases"
require "drip/client/subscribers"
require "drip/client/tags"
Expand All @@ -27,6 +28,7 @@ class Client
include CustomFields
include Events
include Forms
include Orders
include Purchases
include Subscribers
include Tags
Expand Down
2 changes: 0 additions & 2 deletions lib/drip/client/events.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "cgi"

module Drip
class Client
module Events
Expand Down
45 changes: 45 additions & 0 deletions lib/drip/client/orders.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module Drip
class Client
module Orders
# Public: Create or update an order.
#
# email - Required. The String email address of the subscriber.
# options - Required. A Hash of additional order options. Refer to the
# Drip API docs for the required schema.
#
# Returns a Drip::Response.
# See https://developer.drip.com/#orders
def create_or_update_order(email, options = {})
data = options.merge(email: email)
post "#{account_id}/orders", generate_resource("orders", data)
end

# Public: Create or update a batch of orders.
#
# orders - Required. An Array with between 1 and 1000 objects containing order data
#
# Returns a Drip::Response.
# See https://developer.drip.com/#create-or-update-a-batch-of-orders
def create_or_update_orders(orders)
post "#{account_id}/orders/batches", generate_resource("batches", { "orders" => orders })
end

# Public: Create or update a refund.
#
# options - Required. A Hash of refund properties
# amount - Required. The amount of the refund.
# provider - Required. The provider for the Order being refunded.
# order_upstream_id - Required. The upstream_id for the Order being refunded.
# upstream_id - The unique id of refund in the order management system.
# note - A note about the refund.
# processed_at - The String time at which the refund was processed in
# ISO-8601 format.
#
# Returns a Drip::Response.
# See https://developer.drip.com/#create-or-update-a-refund
def create_or_update_refund(options)
post "#{account_id}/refunds", generate_resource("refunds", options)
end
end
end
end
61 changes: 9 additions & 52 deletions lib/drip/client/purchases.rb
Original file line number Diff line number Diff line change
@@ -1,65 +1,22 @@
require "cgi"

module Drip
class Client
module Purchases
# Public: Create a purchase.
#
# email - Required. The String email address of the subscriber.
# amount - Required. The total amount of the purchase in cents.
# options - A Hash of options.
# - properties - Optional. An Object containing properties about
# the order.
# - items - Optional. An Array of objects containing information
# about specific order items.
# - name - Required. The product name.
# - amount - Required. The line total (in
# cents).
# - quantity - Optional. The quantity of the
# item purchased (if omitted,
# defaults to 1).
# - product_id - Optional. A unique identifier
# for the specific product.
# - sku - Optional. The product SKU number.
# - properties - Optional. An Object containing
# properties about the line item.
# - provider - Optional. The identifier for the provider from
# which the purchase data was received
# - order_id - Optional. A unique identifier for the order
# (generally the primary key generated by the
# order management system).
# - permalink - Optional. A URL for the human-readable
# interface to view the order details.
# - occurred_at - Optional. The String time at which the event
# occurred in ISO-8601 format. Defaults to the
# current time.
#
# options - Required. A Hash of additional order options. Refer to the
# Drip API docs for the required schema.
# Returns a Drip::Response.
# See https://www.getdrip.com/docs/rest-api#create_purchase
def create_purchase(email, amount, options = {})
data = options.merge(amount: amount)
post "#{account_id}/subscribers/#{CGI.escape email}/purchases", generate_resource("purchases", data)
end

# Public: Fetch a list of purchases for a subscriber.
#
# email - The String email address of the subscriber.
# See https://developer.drip.com/#orders
#
# Returns a Drip::Response.
# See https://www.getdrip.com/docs/rest-api#list_purchases
def purchases(email)
get "#{account_id}/subscribers/#{CGI.escape email}/purchases"
end
# DEPRECATED: The beta Purchase endpoint has been deprecated and this method now sends
# requests to the Order creation endpoint. Please use `create_or_update_order` instead
def create_purchase(email, amount, options = {})
warn "[DEPRECATED] `create_purchase` is deprecated. Please use `create_or_update_order` instead."

# Public: Fetch a purchase.
#
# email - The String email address of the subscriber.
# id - The String ID of the purchase
#
# Returns a Drip::Response.
# See https://www.getdrip.com/docs/rest-api#list_purchases
def purchase(email, id)
get "#{account_id}/subscribers/#{CGI.escape email}/purchases/#{id}"
data = options.merge({ amount: amount, email: email })
post "#{account_id}/orders", generate_resource("orders", data)
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/drip/collections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "drip/collections/campaigns"
require "drip/collections/campaign_subscriptions"
require "drip/collections/errors"
require "drip/collections/orders"
require "drip/collections/purchases"
require "drip/collections/subscribers"
require "drip/collections/tags"
Expand All @@ -19,6 +20,7 @@ def self.classes
Drip::Campaigns,
Drip::CampaignSubscriptions,
Drip::Errors,
Drip::Orders,
Drip::Purchases,
Drip::Subscribers,
Drip::Tags,
Expand Down
13 changes: 13 additions & 0 deletions lib/drip/collections/orders.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require "drip/collection"

module Drip
class Orders < Collection
def self.collection_name
"orders"
end

def self.resource_name
"order"
end
end
end
2 changes: 2 additions & 0 deletions lib/drip/resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "drip/resources/campaign"
require "drip/resources/campaign_subscription"
require "drip/resources/error"
require "drip/resources/order"
require "drip/resources/purchase"
require "drip/resources/subscriber"
require "drip/resources/tag"
Expand All @@ -19,6 +20,7 @@ def self.classes
Drip::Campaign,
Drip::CampaignSubscription,
Drip::Error,
Drip::Order,
Drip::Purchase,
Drip::Subscriber,
Drip::Tag,
Expand Down
9 changes: 9 additions & 0 deletions lib/drip/resources/order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "drip/resource"

module Drip
class Order < Resource
def self.resource_name
"order"
end
end
end
2 changes: 1 addition & 1 deletion lib/drip/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Drip
VERSION = "1.0.0".freeze
VERSION = "2.0.0".freeze
end
120 changes: 120 additions & 0 deletions test/drip/client/orders_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
require File.dirname(__FILE__) + '/../../test_helper.rb'

class Drip::Client::OrdersTest < Drip::TestCase
def setup
@stubs = Faraday::Adapter::Test::Stubs.new

@connection = Faraday.new do |builder|
builder.adapter :test, @stubs
end

@client = Drip::Client.new { |c| c.account_id = "12345" }
@client.expects(:connection).at_least_once.returns(@connection)
end

context "#create_or_update_order" do
setup do
@email = "drippy@drip.com"
@options = {
"email": @email,
"provider": "shopify",
"upstream_id": "abcdef",
"amount": 4900,
"tax": 100,
"fees": 0,
"discount": 0,
"currency_code": "USD",
"properties": {
"size": "medium",
"color": "red"
}
}
@payload = { "orders" => [@options] }.to_json
@response_status = 202
@response_body = stub

@stubs.post "12345/orders", @payload do
[@response_status, {}, @response_body]
end
end

should "send the correct request" do
expected = Drip::Response.new(@response_status, @response_body)
assert_equal expected, @client.create_or_update_order(@email, @options)
end
end

context "#create_or_update_orders" do
setup do
@orders = [
{
"email": "drippy@drip.com",
"provider": "shopify",
"upstream_id": "abcdef",
"amount": 4900,
"tax": 100,
"fees": 0,
"discount": 0,
"currency_code": "USD",
"properties": {
"size": "medium",
"color": "red"
}
},
{
"email": "dripster@drip.com",
"provider": "shopify",
"upstream_id": "abcdef",
"amount": 1500,
"tax": 10,
"fees": 0,
"discount": 0,
"currency_code": "SGD",
"properties": {
"size": "medium",
"color": "black"
}
}
]

@payload = { "batches" => [{ "orders" => @orders }] }.to_json
@response_status = 202
@response_body = stub

@stubs.post "12345/orders/batches", @payload do
[@response_status, {}, @response_body]
end
end

should "send the correct request" do
expected = Drip::Response.new(@response_status, @response_body)
assert_equal expected, @client.create_or_update_orders(@orders)
end
end

context "#create_or_update_refund" do
setup do
@options = {
"provider": "shopify",
"order_upstream_id": "abcdef",
"amount": 4900,
"upstream_id": "tuvwx",
"note": "Incorrect size",
"processed_at": "2013-06-22T10:41:11Z"
}

@payload = { "refunds" => [@options] }.to_json
@response_status = 202
@response_body = stub

@stubs.post "12345/refunds", @payload do
[@response_status, {}, @response_body]
end
end

should "send the correct request" do
expected = Drip::Response.new(@response_status, @response_body)
assert_equal expected, @client.create_or_update_refund(@options)
end
end
end
Loading

0 comments on commit 77f5ff9

Please sign in to comment.