work on payment unit tests
This commit is contained in:
parent
90868f9196
commit
40b9b12779
|
@ -5,9 +5,12 @@ build:
|
|||
SERVER_ENVIRONMENT: DOCKER
|
||||
commands:
|
||||
- sleep 15
|
||||
- pip3 install mock
|
||||
- pip3 install .
|
||||
- cd website
|
||||
- python3 -m "scaffold" "import"
|
||||
- cd ../
|
||||
- pip3 install .
|
||||
# - python3 -m "scaffold"
|
||||
- pytest
|
||||
|
||||
|
|
|
@ -38,6 +38,8 @@ services:
|
|||
network_mode: bridge
|
||||
# ports:
|
||||
# - "3300:3306"
|
||||
volumes:
|
||||
- database_files:/var/lib/mysql
|
||||
environment:
|
||||
MYSQL_DATABASE: maidstone_hackspace
|
||||
MYSQL_USER: mhackspace
|
||||
|
@ -54,3 +56,5 @@ services:
|
|||
volumes:
|
||||
sockets:
|
||||
driver: local
|
||||
database_files:
|
||||
driver: local
|
||||
|
|
|
@ -6,3 +6,8 @@ pip install -e bzr+lp:scaffold#egg=scaffold
|
|||
python-requests-oauthlib
|
||||
paypalrestsdk
|
||||
dateutil
|
||||
gocardless_pro
|
||||
braintree
|
||||
|
||||
# libraries for tests
|
||||
mock
|
||||
|
|
|
@ -0,0 +1,81 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
import unittest
|
||||
from mock import patch, Mock
|
||||
|
||||
from website.libs.payments import payment, gocardless_provider, braintree_provider
|
||||
|
||||
class TestPaymentGatewaysGocardless(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.auth_gocardless()
|
||||
|
||||
@patch('website.libs.payments.gocardless.request.requests.get', autospec=True)
|
||||
def auth_gocardless(self, mock_request):
|
||||
# mock braintree initalisation request
|
||||
mock_request.return_value = Mock(ok=True)
|
||||
mock_request.return_value.json.return_value = {
|
||||
"id": "1",
|
||||
"created_at": "2011-11-18T17:07:09Z",
|
||||
"access_token": "test_token",
|
||||
"next_payout_date": "2011-11-18T17:07:09Z"
|
||||
}
|
||||
|
||||
with patch('gocardless.resources.Merchant') as mock_subscription:
|
||||
self.provider = gocardless_provider()
|
||||
return self.provider #self.provider
|
||||
|
||||
def test_fetch_subscription_gocardless(self):
|
||||
items = [Mock(
|
||||
id='01',
|
||||
status='active',
|
||||
amount=20.00,
|
||||
reference='ref01',
|
||||
created_at='date'
|
||||
)]
|
||||
items[-1].user.return_value = Mock(email='test@test.com')
|
||||
|
||||
self.provider.client = Mock()
|
||||
self.provider.client.subscriptions = Mock(return_value=items)
|
||||
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('start_date'), 'date')
|
||||
self.assertEqual(item.get('amount'), 20.00)
|
||||
|
||||
|
||||
class TestPaymentGatewaysBraintree(unittest.TestCase):
|
||||
@patch('website.libs.payments.braintree.Configuration.configure')
|
||||
def auth_braintree(self, mock_request):
|
||||
# mock braintree initalisation request
|
||||
mock_request.return_value = Mock(ok=True)
|
||||
mock_request.return_value.json.return_value = {
|
||||
"id": "1",
|
||||
"created_at": "2011-11-18T17:07:09Z",
|
||||
"access_token": "test_token",
|
||||
"next_payout_date": "2011-11-18T17:07:09Z"
|
||||
}
|
||||
|
||||
self.provider = braintree_provider()
|
||||
|
||||
@patch('website.libs.payments.braintree.Subscription.search')
|
||||
def test_fetch_subscription_braintree(self, mock_request):
|
||||
provider = self.auth_braintree()
|
||||
|
||||
items = [Mock(
|
||||
id='01',
|
||||
status='active',
|
||||
amount=20.00,
|
||||
reference='ref01',
|
||||
created_at='date'
|
||||
)]
|
||||
items[-1].user.return_value = Mock(email='test@test.com')
|
||||
|
||||
mock_request.return_value = items
|
||||
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('start_date'), 'date')
|
||||
self.assertEqual(item.get('amount'), 20.00)
|
|
@ -49,20 +49,22 @@ google_calendar_id = ''
|
|||
google_calendar_api_key = ''
|
||||
|
||||
print(os.path.abspath('./'))
|
||||
settings_path = os.path.dirname(os.path.realpath(__file__))
|
||||
print(settings_path)
|
||||
if os.environ.get('SERVER_ENVIRONMENT') =='DOCKER':
|
||||
if os.path.exists('config/settings_docker.py'):
|
||||
if os.path.exists('%s/settings_docker.py' % settings_path):
|
||||
print('Using settings for docker enviroment')
|
||||
from website.config.settings_docker import *
|
||||
else:
|
||||
if os.path.exists('website/config/settings_dev.py'):
|
||||
if os.path.exists('%s/settings_dev.py' % settings_path):
|
||||
print('Using settings for dev enviroment')
|
||||
from website.config.settings_dev import *
|
||||
|
||||
if os.path.exists('config/settings_testing.py'):
|
||||
if os.path.exists('%s/settings_testing.py' % settings_path):
|
||||
print('Using settings for test enviroment')
|
||||
from website.config.settings_testing import *
|
||||
|
||||
if os.path.exists('config/settings_live.py'):
|
||||
if os.path.exists('%s/settings_live.py' % settings_path):
|
||||
print('Using settings for live enviroment')
|
||||
from website.config.settings_live import *
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import settings
|
||||
import braintree
|
||||
import gocardless
|
||||
import paypalrestsdk as paypal
|
||||
|
@ -43,6 +44,10 @@ class payment:
|
|||
|
||||
print(braintree.Configuration.configure(braintree.Environment.Sandbox, merchant_id='b3sdmyczd3fz6b3p', public_key='rxb7yffm3tk758rq', private_key='62f92d2b00a451c40fdf5fbca54f0421'))
|
||||
#braintree.Configuration.configure(**conf)
|
||||
print(braintree.Subscription.search(braintree.SubscriptionSearch.status == braintree.Subscription.Status.Active))
|
||||
|
||||
|
||||
|
||||
token = braintree.ClientToken.generate()
|
||||
print(token)
|
||||
result = braintree.Transaction.sale({
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
from pprint import pprint
|
||||
from datetime import datetime, timedelta
|
||||
import pytz
|
||||
import gocardless
|
||||
import braintree
|
||||
# import gocardless_pro
|
||||
import paypalrestsdk as paypal
|
||||
|
||||
from website.config import settings
|
||||
|
@ -11,14 +12,152 @@ PROVIDER_ID = {'gocardless':1, 'paypal': 2}
|
|||
PROVIDER_NAME = {1: 'gocardless', 2: 'paypal'}
|
||||
|
||||
|
||||
class provider(object):
|
||||
# Create based on class name:
|
||||
def factory(type):
|
||||
#return eval(type + "()")
|
||||
if type == "Circle": return Circle()
|
||||
if type == "Square": return Square()
|
||||
assert 0, "Bad shape creation: " + type
|
||||
factory = staticmethod(factory)
|
||||
def select_provider(type):
|
||||
if type == "gocardless": return gocardless_provider()
|
||||
if type == "braintree": return braintree_provider()
|
||||
if type == "paypal": return paypal_provider()
|
||||
assert 0, "No Provider for " + type
|
||||
|
||||
class paypal_provider:
|
||||
def __init__(self):
|
||||
print(settings.payment_providers['paypal']['credentials'])
|
||||
print(paypal.configure(**settings.payment_providers['paypal']['credentials']))
|
||||
|
||||
def fetch_subscriptions(self):
|
||||
#~ I-S39170DK26AF
|
||||
start_date, end_date = "2014-07-01", "2016-07-20"
|
||||
# plans_created = paypal.BillingPlan.all({"status": "CREATED", "sort_order": "DESC"})
|
||||
|
||||
# billing_agreement = paypal.BillingAgreement.all()
|
||||
# billing_agreement = paypal.BillingAgreement.find('0HX53832PF841861G')
|
||||
# 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":transactions 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.Invoice.all({"page_size": 2})
|
||||
|
||||
print("List Invoice:")
|
||||
for invoice in history.invoices:
|
||||
print(" -> Invoice[%s]" % (invoice.id))
|
||||
print(invoice.to_dict())
|
||||
|
||||
payment_history = paypal.Payment.all({"count": 10})
|
||||
print('List payments')
|
||||
print(dir(payment_history))
|
||||
for payment in payment_history.payments:
|
||||
print(payment['id'])
|
||||
# print(payment['payer']['payer_info']['name'])
|
||||
# print(payment['payer']['payer_info']['email'])
|
||||
# print(pprint(payment['transactions']))
|
||||
for transact in payment['transactions']:
|
||||
print(transact['amount']['total'])
|
||||
# print(payment_history.payments)
|
||||
|
||||
|
||||
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(plan['id'])
|
||||
|
||||
billing_agreement = paypal.BillingAgreement.find(plan['id'])
|
||||
print('agreement')
|
||||
print(billing_agreement.search_transactions(start_date, end_date))
|
||||
print(billing_agreement.to_dict())
|
||||
print(dir(plan))
|
||||
print(plan.to_dict())
|
||||
print(plan['name'])
|
||||
print(" -> BillingPlan[%s]" % (plan.id))
|
||||
|
||||
yield {
|
||||
'status': paying_member.status,
|
||||
'email': user.email,
|
||||
'start_date': paying_member.created_at,
|
||||
'reference': paying_member.id,
|
||||
'amount': paying_member.amount}
|
||||
#~ 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}
|
||||
|
||||
class gocardless_provider:
|
||||
client = None
|
||||
|
||||
def __init__(self):
|
||||
# gocardless are changing there api, not sure if we can switch yet
|
||||
# self.client = gocardless_pro.Client(
|
||||
# access_token=settings.payment_providers['gocardless']['credentials']['access_token'],
|
||||
# environment=settings.payment_providers['gocardless']['environment'])
|
||||
|
||||
print(settings.payment_providers.keys)
|
||||
gocardless.environment = settings.payment_providers['gocardless']['environment']
|
||||
gocardless.set_details(**settings.payment_providers['gocardless']['credentials'])
|
||||
self.client = gocardless.client.merchant()
|
||||
|
||||
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)
|
||||
yield {
|
||||
'status': paying_member.status,
|
||||
'email': user.email,
|
||||
'start_date': paying_member.created_at,
|
||||
'reference': paying_member.reference,
|
||||
'amount': paying_member.amount}
|
||||
|
||||
def create_subscription(self, amount, name, redirect_success, redirect_failure, interval_unit='month', interval_length='1'):
|
||||
if self.provider == 'gocardless':
|
||||
return gocardless.client.new_subscription_url(
|
||||
amount=float(amount),
|
||||
interval_length=interval_length,
|
||||
interval_unit=interval_unit,
|
||||
name=name,
|
||||
redirect_uri=redirect_success)
|
||||
|
||||
|
||||
class braintree_provider:
|
||||
def __init__(self):
|
||||
braintree.Configuration.configure(
|
||||
environment=braintree.Environment.Sandbox,
|
||||
merchant_id=settings.payment_providers['braintree']['credentials']['merchant_id'],
|
||||
public_key=settings.payment_providers['braintree']['credentials']['public_key'],
|
||||
private_key=settings.payment_providers['braintree']['credentials']['private_key'])
|
||||
|
||||
|
||||
def create_subscription(self, amount, name, redirect_success, redirect_failure, interval_unit='month', interval_length='1'):
|
||||
braintree.Subscription.create({
|
||||
"payment_method_token": "the_token",
|
||||
"plan_id": "membership",
|
||||
"price": 20.00,
|
||||
|
||||
})
|
||||
|
||||
def fetch_subscriptions(self):
|
||||
for paying_member in braintree.Subscription.search(braintree.SubscriptionSearch.status == braintree.Subscription.Status.Active):
|
||||
user=paying_member.user()
|
||||
yield {
|
||||
'status': paying_member.status,
|
||||
'email': user.email,
|
||||
'start_date': paying_member.created_at,
|
||||
'reference': paying_member.reference,
|
||||
'amount': paying_member.amount}
|
||||
|
||||
|
||||
class payment:
|
||||
|
@ -34,11 +173,14 @@ class payment:
|
|||
self.environment = int(mode=='production')
|
||||
self.provider_id = PROVIDER_ID.get(provider)
|
||||
|
||||
print(settings.payment_providers)
|
||||
if provider == 'paypal':
|
||||
paypal.configure(**settings.payment_providers[provider]['credentials'])
|
||||
print(settings.payment_providers[provider]['credentials'])
|
||||
return
|
||||
|
||||
gocardless_pro.Client(
|
||||
access_token=settings.payment_providers[provider]['credentials']['access_token'],
|
||||
environment=settings.payment_providers[provider])
|
||||
#~ environment = int('production' = settings.payment_providers[provider]['environment'])
|
||||
gocardless.environment = settings.payment_providers[provider]['environment']
|
||||
gocardless.set_details(**settings.payment_providers[provider]['credentials'])
|
||||
|
@ -88,6 +230,10 @@ class payment:
|
|||
merchant = gocardless.client.merchant()
|
||||
for paying_member in merchant.subscriptions():
|
||||
user=paying_member.user()
|
||||
print(dir(paying_member))
|
||||
print(paying_member.next_interval_start)
|
||||
print(paying_member.status)
|
||||
print(dir(paying_member.user()))
|
||||
yield {
|
||||
'email': user.email,
|
||||
'start_date': paying_member.created_at,
|
||||
|
@ -189,7 +335,7 @@ class payment:
|
|||
def subscribe(self, amount, name, redirect_success, redirect_failure, interval_unit='month', interval_length='1'):
|
||||
if self.provider == 'gocardless':
|
||||
return gocardless.client.new_subscription_url(
|
||||
amount=amount,
|
||||
amount=float(amount),
|
||||
interval_length=interval_length,
|
||||
interval_unit=interval_unit,
|
||||
name=name,
|
||||
|
|
|
@ -29,6 +29,8 @@ with web.template as setup:
|
|||
setup.persistent_header('<link rel="stylesheet" id="navigationCss" href="/static/css/sprite-action-white.css" media="" type="text/css" />')
|
||||
setup.persistent_header('<link rel="stylesheet" id="navigationCss" href="/static/css/sprite-content-white.css" media="" type="text/css" />')
|
||||
|
||||
setup.persistent_header('<link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">')
|
||||
|
||||
setup.persistent_header('<script type="text/javascript" src="/static/js/jquery-3.1.1.min.js"></script>')
|
||||
setup.persistent_header('<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.js"></script>')
|
||||
setup.persistent_header('<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular-animate.js"></script>')
|
||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,16 @@
|
|||
import os
|
||||
import sys
|
||||
|
||||
sys.path.append(os.path.abspath('../../'))
|
||||
|
||||
from website.config.settings import *
|
||||
from website.libs.payments import payment, gocardless_provider, paypal_provider
|
||||
|
||||
gocardless = gocardless_provider()
|
||||
for user in gocardless.fetch_subscriptions():
|
||||
print(user)
|
||||
|
||||
|
||||
# paypal = paypal_provider()
|
||||
# for user in paypal.fetch_subscriptions():
|
||||
# print(user)
|
|
@ -28,20 +28,23 @@ class control(base_widget_extended):
|
|||
<fieldset>
|
||||
<legend>Join Maidstone Hackspace</legend>
|
||||
<div class="row">
|
||||
<div class="input-field col s12">
|
||||
<div class="input-field col s8">
|
||||
<select name="provider">
|
||||
<option value="gocardless">GoCardless</option>
|
||||
<option value="paypal">PayPal</option>
|
||||
</select>
|
||||
<label for="provider">Payment provider</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="input-field col s8">
|
||||
<input name="amount" placeholder="20.00" value="20.00" type="text">
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="input-field col s12">
|
||||
<input name="amount" placeholder="20.00" value="20.00" type="text">
|
||||
<label for="amount">Subscription Amount</label></div>
|
||||
<div class="button">
|
||||
<button type="submit">submit</button>
|
||||
<button class="btn waves-effect waves-light" type="submit" name="action">Submit
|
||||
<i class="material-icons right">send</i>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</fieldset>
|
||||
|
|
Loading…
Reference in New Issue