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

Commit

Permalink
create product on the fly (bug 820870)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy McKay committed Dec 13, 2012
1 parent a61b950 commit 93a116e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
43 changes: 35 additions & 8 deletions lib/solitude/api.py
Expand Up @@ -126,14 +126,8 @@ def configure_product_for_billing(self, webpay_trans_id,
seller_product__external_id=product_id seller_product__external_id=product_id
) )
if res['meta']['total_count'] == 0: if res['meta']['total_count'] == 0:
# TODO(Kumar) create products on the fly. bug 820164 bango_product_uri = self.create_product(product_id,
raise NotImplementedError( product_name, currency, amount, res['objects'][0])
'this product does not exist and must be created')

# Create the product on the fly.
# This case exists for in-app purchases where the
# seller is selling a new item for the first time.

else: else:
bango_product_uri = res['objects'][0]['resource_uri'] bango_product_uri = res['objects'][0]['resource_uri']
log.info('transaction %s: bango product: %s' log.info('transaction %s: bango product: %s'
Expand All @@ -150,6 +144,39 @@ def configure_product_for_billing(self, webpay_trans_id,
% (webpay_trans_id, bill_id)) % (webpay_trans_id, bill_id))
return bill_id return bill_id


def create_product(self, external_id, product_name, currency, amount, seller):
"""
Creates a product and a Bango ID on the fly in solitude.
"""
if not seller['bango']:
raise ValueError('No bango account set up for %s' %
seller['resource_pk'])

product = self.slumber.generic.product.post({
'external_id': external_id,
'seller': seller['bango']['seller']
})
bango = self.slumber.bango.product.post({
'seller_bango': seller['bango']['resource_uri'],
'seller_product': product['resource_uri'],
'name': product_name,
'categoryId': 1,
'secret': 'n' # This is likely going to be removed.
})
self.slumber.bango.premium.post({
'price': amount,
'currencyIso': currency,
'seller_product_bango': bango['resource_uri']
})

self.slumber.bango.rating.post({
'rating': 'UNIVERSAL',
'ratingScheme': 'GLOBAL',
'seller_product_bango': bango['resource_uri']
})
return bango['resource_uri']




if getattr(settings, 'SOLITUDE_URL', False): if getattr(settings, 'SOLITUDE_URL', False):
client = SolitudeAPI(settings.SOLITUDE_URL) client = SolitudeAPI(settings.SOLITUDE_URL)
Expand Down
23 changes: 23 additions & 0 deletions lib/solitude/tests.py
@@ -1,12 +1,14 @@
from django.conf import settings from django.conf import settings
from django.test import TestCase from django.test import TestCase


import mock
from nose.exc import SkipTest from nose.exc import SkipTest
from nose.tools import eq_ from nose.tools import eq_


from lib.solitude.api import client from lib.solitude.api import client
from lib.solitude.errors import ERROR_STRINGS from lib.solitude.errors import ERROR_STRINGS



class SolitudeAPITest(TestCase): class SolitudeAPITest(TestCase):


def setUp(self): def setUp(self):
Expand Down Expand Up @@ -79,3 +81,24 @@ def test_verify_good_pin(self):


def test_verify_alpha_pin(self): def test_verify_alpha_pin(self):
assert not client.verify_pin(self.uuid, 'lame') assert not client.verify_pin(self.uuid, 'lame')


class CreateBangoTest(TestCase):
uuid = 'some:pin'

def test_create_no_bango(self):
with self.assertRaises(ValueError):
client.create_product('ext:id', None, None, None,
{'bango': None, 'resource_pk': 'foo'})

@mock.patch('lib.solitude.api.client.slumber')
def test_create_bango(self, slumber):
# Temporary mocking. Remove when this is mocked properly.
slumber.bango.generic.post.return_value = {'product': 'some:uri'}
slumber.bango.product.post.return_value = {'resource_uri': 'some:uri'}
assert client.create_product('ext:id', 'product:name', 'CAD', 1,
{'bango': {'seller': 's', 'resource_uri': 'r'},
'resource_pk': 'foo'})
assert slumber.generic.product.post.called
assert slumber.bango.rating.post.called
assert slumber.bango.premium.post.called

0 comments on commit 93a116e

Please sign in to comment.