Skip to content

Commit bbe7a94

Browse files
committed
Restore purchases create method
1 parent a402a18 commit bbe7a94

3 files changed

Lines changed: 24 additions & 118 deletions

File tree

lib/drip/client/purchases.rb

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,17 @@
1-
require "cgi"
2-
31
module Drip
42
class Client
53
module Purchases
64
# Public: Create a purchase.
75
#
86
# email - Required. The String email address of the subscriber.
97
# amount - Required. The total amount of the purchase in cents.
10-
# options - A Hash of options.
11-
# - properties - Optional. An Object containing properties about
12-
# the order.
13-
# - items - Optional. An Array of objects containing information
14-
# about specific order items.
15-
# - name - Required. The product name.
16-
# - amount - Required. The line total (in
17-
# cents).
18-
# - quantity - Optional. The quantity of the
19-
# item purchased (if omitted,
20-
# defaults to 1).
21-
# - product_id - Optional. A unique identifier
22-
# for the specific product.
23-
# - sku - Optional. The product SKU number.
24-
# - properties - Optional. An Object containing
25-
# properties about the line item.
26-
# - provider - Optional. The identifier for the provider from
27-
# which the purchase data was received
28-
# - order_id - Optional. A unique identifier for the order
29-
# (generally the primary key generated by the
30-
# order management system).
31-
# - permalink - Optional. A URL for the human-readable
32-
# interface to view the order details.
33-
# - occurred_at - Optional. The String time at which the event
34-
# occurred in ISO-8601 format. Defaults to the
35-
# current time.
36-
#
8+
# options - Required. A Hash of additional order options. Refer to the
9+
# Drip API docs for the required schema.
3710
# Returns a Drip::Response.
38-
# See https://www.getdrip.com/docs/rest-api#create_purchase
11+
# See https://developer.drip.com/#orders
3912
def create_purchase(email, amount, options = {})
40-
data = options.merge(amount: amount)
41-
post "#{account_id}/subscribers/#{CGI.escape email}/purchases", generate_resource("purchases", data)
42-
end
43-
44-
# Public: Fetch a list of purchases for a subscriber.
45-
#
46-
# email - The String email address of the subscriber.
47-
#
48-
# Returns a Drip::Response.
49-
# See https://www.getdrip.com/docs/rest-api#list_purchases
50-
def purchases(email)
51-
get "#{account_id}/subscribers/#{CGI.escape email}/purchases"
52-
end
53-
54-
# Public: Fetch a purchase.
55-
#
56-
# email - The String email address of the subscriber.
57-
# id - The String ID of the purchase
58-
#
59-
# Returns a Drip::Response.
60-
# See https://www.getdrip.com/docs/rest-api#list_purchases
61-
def purchase(email, id)
62-
get "#{account_id}/subscribers/#{CGI.escape email}/purchases/#{id}"
13+
data = options.merge({ amount: amount, email: email })
14+
post "#{account_id}/orders", generate_resource("orders", data)
6315
end
6416
end
6517
end

lib/drip/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Drip
2-
VERSION = "1.0.1".freeze
2+
VERSION = "2.0.0".freeze
33
end

test/drip/client/purchases_test.rb

Lines changed: 18 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -15,79 +15,33 @@ def setup
1515
context "#create_purchase" do
1616
setup do
1717
@email = "derrick@getdrip.com"
18-
@amount = 3900
19-
@properties = {
20-
address: '123 Anywhere St'
21-
}
22-
23-
@items = [
24-
{
25-
name: 'foo',
26-
amount: 100
27-
},
28-
{
29-
name: 'bar',
30-
amount: 200
18+
@amount = 4900
19+
@options = {
20+
"email": @email,
21+
"provider": "shopify",
22+
"upstream_id": "abcdef",
23+
"amount": @amount,
24+
"tax": 100,
25+
"fees": 0,
26+
"discount": 0,
27+
"currency_code": "USD",
28+
"properties": {
29+
"size": "medium",
30+
"color": "red"
3131
}
32-
]
33-
34-
@payload = {
35-
purchases: [{
36-
properties: @properties,
37-
items: @items,
38-
amount: @amount
39-
}]
40-
}.to_json
41-
42-
@response_status = 201
43-
@response_body = stub
44-
45-
@stubs.post "12345/subscribers/#{CGI.escape @email}/purchases", @payload do
46-
[@response_status, {}, @response_body]
47-
end
48-
end
49-
50-
should "send the right request" do
51-
expected = Drip::Response.new(@response_status, @response_body)
52-
assert_equal expected, @client.create_purchase(@email, @amount, {
53-
properties: @properties,
54-
items: @items
55-
})
56-
end
57-
end
58-
59-
context "#purchases" do
60-
setup do
61-
@email = "derrick@getdrip.com"
62-
@response_status = 201
63-
@response_body = stub
64-
65-
@stubs.get "12345/subscribers/#{CGI.escape @email}/purchases" do
66-
[@response_status, {}, @response_body]
67-
end
68-
end
69-
70-
should "send the right request" do
71-
expected = Drip::Response.new(@response_status, @response_body)
72-
assert_equal expected, @client.purchases(@email)
73-
end
74-
end
75-
76-
context "#purchase" do
77-
setup do
78-
@email = "derrick@getdrip.com"
79-
@id = '23456'
80-
@response_status = 201
32+
}
33+
@payload = { "orders" => [@options] }.to_json
34+
@response_status = 202
8135
@response_body = stub
8236

83-
@stubs.get "12345/subscribers/#{CGI.escape @email}/purchases/#{@id}" do
37+
@stubs.post "12345/orders", @payload do
8438
[@response_status, {}, @response_body]
8539
end
8640
end
8741

8842
should "send the right request" do
8943
expected = Drip::Response.new(@response_status, @response_body)
90-
assert_equal expected, @client.purchase(@email, @id)
44+
assert_equal expected, @client.create_purchase(@email, @amount, @options)
9145
end
9246
end
9347
end

0 commit comments

Comments
 (0)