diff --git a/mhackspace/subscriptions/payments.py b/mhackspace/subscriptions/payments.py index 2db4533..6a188d4 100644 --- a/mhackspace/subscriptions/payments.py +++ b/mhackspace/subscriptions/payments.py @@ -68,6 +68,16 @@ class gocardless_provider: def get_token(self): return 'N/A' + def cancel_subscribe(self, reference): + subscription = gocardless.client.subscription(reference) + response = subscription.cancel() + return { + 'amount': subscription.amount, + 'start_date': subscription.created_at, + 'reference': subscription.id, + 'success': response.success + } + def create_subscription(self, amount, name, redirect_success, redirect_failure, interval_unit='month', interval_length='1'): return gocardless.client.new_subscription_url( amount=float(amount), diff --git a/mhackspace/subscriptions/tests/test_payment_gateways.py b/mhackspace/subscriptions/tests/test_payment_gateways.py index acb65e2..5b19544 100644 --- a/mhackspace/subscriptions/tests/test_payment_gateways.py +++ b/mhackspace/subscriptions/tests/test_payment_gateways.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- from test_plus.test import TestCase -# import unittest +from unittest import skip from mock import patch, Mock from mhackspace.subscriptions.payments import payment, gocardless_provider, braintree_provider @@ -26,6 +26,23 @@ class TestPaymentGatewaysGocardless(TestCase): self.provider = gocardless_provider() return self.provider #self.provider + @skip("Need to implement") + @patch('mhackspace.subscriptions.payments.gocardless.client.subscription', autospec=True) + def test_unsubscribe(self, mock_subscription): + mock_subscription.return_value = Mock(success='success') + mock_subscription.cancel.return_value = Mock( + id='01', + status='active', + amount=20.00, + created_at='date' + ) + result = self.provider.cancel_subscription(reference='M01') + + 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') + # @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)