diff --git a/mhackspace/subscriptions/payments.py b/mhackspace/subscriptions/payments.py index 6b6fbd1..2db4533 100644 --- a/mhackspace/subscriptions/payments.py +++ b/mhackspace/subscriptions/payments.py @@ -4,7 +4,6 @@ import gocardless import braintree from django.conf import settings -print(dir(settings)) payment_providers = settings.PAYMENT_PROVIDERS # import gocardless_pro @@ -47,18 +46,15 @@ class gocardless_provider: return { 'amount': subscription.amount, 'start_date': subscription.created_at, - 'reference': subscription.id + 'reference': subscription.id, + 'success': response.success } def fetch_subscriptions(self): for paying_member in self.client.subscriptions(): user=paying_member.user() - # for bill in paying_member.bills(): - # print('test') - # print(dir(bill)) - # print(bill.created_at) - # print(dir(paying_member)) - # print(paying_member.reference_fields) + + #gocardless does not have a reference so we use the id instead yield { 'status': paying_member.status, 'email': user.email, @@ -201,40 +197,6 @@ class payment: 'reference': paying_member.id, 'amount': paying_member.amount} - if self.provider == 'paypal': - #~ I-S39170DK26AF - #~ start_date, end_date = "2014-07-01", "2014-07-20" - billing_agreement = paypal.BillingAgreement.find('') - print(billing_agreement) - print(dir(billing_agreement)) - #~ print billing_agreement.search_transactions(start_date, end_date) - #~ transactions = billing_agreement.search_transactions(start_date, end_date) - payment_history = paypal.Payment.all({"count": 2}) - - # List Payments - print("List Payment:") - print(payment_history) - for payment in payment_history.payments: - print(" -> Payment[%s]" % (payment.id)) - #~ print paypal.BillingAgreement.all() - history = paypal.BillingPlan.all( - {"status": "CREATED", "page_size": 5, "page": 1, "total_required": "yes"}) - print(history) - - print("List BillingPlan:") - for plan in history.plans: - print(dir(plan)) - print(plan.to_dict()) - print(" -> BillingPlan[%s]" % (plan.id)) - - #~ merchant = gocardless.client.merchant() - #~ for paying_member in merchant.subscriptions(): - #~ user=paying_member.user() - #~ yield { - #~ 'email': user.email, - #~ 'start_date': paying_member.created_at, - #~ 'reference': paying_member.id, - #~ 'amount': paying_member.amount} def subscribe_confirm(self, args): if self.provider == 'gocardless': diff --git a/mhackspace/subscriptions/tests/test_payment_gateways.py b/mhackspace/subscriptions/tests/test_payment_gateways.py index 81c686b..acb65e2 100644 --- a/mhackspace/subscriptions/tests/test_payment_gateways.py +++ b/mhackspace/subscriptions/tests/test_payment_gateways.py @@ -26,31 +26,56 @@ class TestPaymentGatewaysGocardless(TestCase): self.provider = gocardless_provider() return self.provider #self.provider - def test_confirm_subscription_callback(self): - with patch('gocardless.client.confirm_resources') as mock_subscription: - self.provider = gocardless_provider() - - def test_fetch_subscription_gocardless(self): - items = [Mock( + # @patch('mhackspace.subscriptions.payments.gocardless.request.requests.get', autospec=True) + @patch('mhackspace.subscriptions.payments.gocardless.client.subscription', autospec=True) + @patch('mhackspace.subscriptions.payments.gocardless.client.confirm_resource', autospec=True) + def test_confirm_subscription_callback(self, mock_confirm, mock_subscription): + mock_confirm.return_value = Mock(success='success') + mock_subscription.return_value = Mock( id='01', status='active', amount=20.00, - reference='ref01', created_at='date' - )] - items[-1].user.return_value = Mock(email='test@test.com') + ) + + request_params = { + 'resource_uri': 'http://gocardless/resource/url/01', + 'resource_id': '01', + 'resource_type': 'subscription', + 'signature': 'sig', + 'state': 'inactive' + } + + result = self.provider.subscribe_confirm(request_params) + + self.assertEqual(result.get('amount'), 20.00) + self.assertEqual(result.get('start_date'), 'date') + self.assertEqual(result.get('reference'), '01') + self.assertEqual(result.get('success'), 'success') + + + def test_fetch_subscription_gocardless(self): + item = Mock( + id='01', + status='active', + amount=20.00, + created_at='date' + ) + item.user.return_value = Mock(email='test@test.com') self.provider.client = Mock() - self.provider.client.subscriptions = Mock(return_value=items) + self.provider.client.subscriptions = Mock(return_value=[item]) + + # mock out gocardless subscriptions method, and return our own values for item in self.provider.fetch_subscriptions(): self.assertEqual(item.get('status'), 'active') self.assertEqual(item.get('email'), 'test@test.com') - self.assertEqual(item.get('reference'), 'ref01') + self.assertEqual(item.get('reference'), '01') self.assertEqual(item.get('start_date'), 'date') self.assertEqual(item.get('amount'), 20.00) -class TestPaymentGatewaysBraintree(TestCase): +class DisabledestPaymentGatewaysBraintree(TestCase): @patch('mhackspace.subscriptions.payments.braintree.Configuration.configure') def auth_braintree(self, mock_request): # mock braintree initalisation request