Skip to content
This repository was archived by the owner on Jan 25, 2018. It is now read-only.

Commit 34c8998

Browse files
author
Andy McKay
committed
Merge pull request #8 from andymckay/820870
create product on the fly (bug 820870)
2 parents a2bc5b7 + 93a116e commit 34c8998

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

lib/solitude/api.py

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,8 @@ def configure_product_for_billing(self, webpay_trans_id,
150150
seller_product__external_id=product_id
151151
)
152152
if res['meta']['total_count'] == 0:
153-
# TODO(Kumar) create products on the fly. bug 820164
154-
raise NotImplementedError(
155-
'this product does not exist and must be created')
156-
157-
# Create the product on the fly.
158-
# This case exists for in-app purchases where the
159-
# seller is selling a new item for the first time.
160-
153+
bango_product_uri = self.create_product(product_id,
154+
product_name, currency, amount, res['objects'][0])
161155
else:
162156
bango_product_uri = res['objects'][0]['resource_uri']
163157
log.info('transaction %s: bango product: %s'
@@ -174,6 +168,39 @@ def configure_product_for_billing(self, webpay_trans_id,
174168
% (webpay_trans_id, bill_id))
175169
return bill_id
176170

171+
def create_product(self, external_id, product_name, currency, amount, seller):
172+
"""
173+
Creates a product and a Bango ID on the fly in solitude.
174+
"""
175+
if not seller['bango']:
176+
raise ValueError('No bango account set up for %s' %
177+
seller['resource_pk'])
178+
179+
product = self.slumber.generic.product.post({
180+
'external_id': external_id,
181+
'seller': seller['bango']['seller']
182+
})
183+
bango = self.slumber.bango.product.post({
184+
'seller_bango': seller['bango']['resource_uri'],
185+
'seller_product': product['resource_uri'],
186+
'name': product_name,
187+
'categoryId': 1,
188+
'secret': 'n' # This is likely going to be removed.
189+
})
190+
self.slumber.bango.premium.post({
191+
'price': amount,
192+
'currencyIso': currency,
193+
'seller_product_bango': bango['resource_uri']
194+
})
195+
196+
self.slumber.bango.rating.post({
197+
'rating': 'UNIVERSAL',
198+
'ratingScheme': 'GLOBAL',
199+
'seller_product_bango': bango['resource_uri']
200+
})
201+
return bango['resource_uri']
202+
203+
177204

178205
if getattr(settings, 'SOLITUDE_URL', False):
179206
client = SolitudeAPI(settings.SOLITUDE_URL)

lib/solitude/tests.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from django.conf import settings
22
from django.test import TestCase
33

4+
import mock
45
from nose.exc import SkipTest
56
from nose.tools import eq_
67

@@ -100,3 +101,24 @@ def test_verify_without_confirm_and_good_pin(self):
100101

101102
def test_verify_alpha_pin(self):
102103
assert not client.verify_pin(self.uuid, 'lame')
104+
105+
106+
class CreateBangoTest(TestCase):
107+
uuid = 'some:pin'
108+
109+
def test_create_no_bango(self):
110+
with self.assertRaises(ValueError):
111+
client.create_product('ext:id', None, None, None,
112+
{'bango': None, 'resource_pk': 'foo'})
113+
114+
@mock.patch('lib.solitude.api.client.slumber')
115+
def test_create_bango(self, slumber):
116+
# Temporary mocking. Remove when this is mocked properly.
117+
slumber.bango.generic.post.return_value = {'product': 'some:uri'}
118+
slumber.bango.product.post.return_value = {'resource_uri': 'some:uri'}
119+
assert client.create_product('ext:id', 'product:name', 'CAD', 1,
120+
{'bango': {'seller': 's', 'resource_uri': 'r'},
121+
'resource_pk': 'foo'})
122+
assert slumber.generic.product.post.called
123+
assert slumber.bango.rating.post.called
124+
assert slumber.bango.premium.post.called

0 commit comments

Comments
 (0)