本文整理汇总了Python中billy.models.transaction.TransactionModel类的典型用法代码示例。如果您正苦于以下问题:Python TransactionModel类的具体用法?Python TransactionModel怎么用?Python TransactionModel使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TransactionModel类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_subscription_cancel_with_prorated_refund_and_amount_overwrite
def test_subscription_cancel_with_prorated_refund_and_amount_overwrite(self):
from billy.models.transaction import TransactionModel
model = self.make_one(self.session)
tx_model = TransactionModel(self.session)
with freeze_time('2013-06-01'):
with db_transaction.manager:
guid = model.create(
customer_guid=self.customer_tom_guid,
plan_guid=self.monthly_plan_guid,
amount=10000,
)
tx_guids = model.yield_transactions()
transaction = tx_model.get(tx_guids[0])
transaction.status = tx_model.STATUS_DONE
transaction.external_id = 'MOCK_BALANCED_DEBIT_URI'
self.session.add(transaction)
# it is a monthly plan, there is 30 days in June, and only
# 6 days are elapsed, so 6 / 30 days, the rate should be 1 - 0.2 = 0.8
# and we have 100 dollars as the amount, we should return 80 dollars to
# customer
with freeze_time('2013-06-07'):
with db_transaction.manager:
refund_guid = model.cancel(guid, prorated_refund=True)
transaction = tx_model.get(refund_guid)
self.assertEqual(transaction.amount, 8000)
示例2: test_yield_transactions_for_specific_subscriptions
def test_yield_transactions_for_specific_subscriptions(self):
from billy.models.transaction import TransactionModel
model = self.make_one(self.session)
tx_model = TransactionModel(self.session)
with db_transaction.manager:
guid1 = model.create(
customer_guid=self.customer_tom_guid,
plan_guid=self.monthly_plan_guid,
)
model.create(
customer_guid=self.customer_tom_guid,
plan_guid=self.monthly_plan_guid,
)
guid2 = model.create(
customer_guid=self.customer_tom_guid,
plan_guid=self.monthly_plan_guid,
)
model.create(
customer_guid=self.customer_tom_guid,
plan_guid=self.monthly_plan_guid,
)
tx_guids = model.yield_transactions([guid1, guid2])
self.assertEqual(len(tx_guids), 2)
subscription_guids = [tx_model.get(tx_guid).subscription_guid
for tx_guid in tx_guids]
self.assertEqual(set(subscription_guids), set([guid1, guid2]))
示例3: test_subscription_cancel_with_prorated_refund_rounding
def test_subscription_cancel_with_prorated_refund_rounding(self):
from billy.models.transaction import TransactionModel
model = self.make_one(self.session)
tx_model = TransactionModel(self.session)
with freeze_time('2013-06-01'):
with db_transaction.manager:
guid = model.create(
customer_guid=self.customer_tom_guid,
plan_guid=self.monthly_plan_guid,
)
tx_guids = model.yield_transactions()
transaction = tx_model.get(tx_guids[0])
transaction.status = tx_model.STATUS_DONE
transaction.external_id = 'MOCK_BALANCED_DEBIT_URI'
self.session.add(transaction)
# 17 / 30 days, the rate should be 1 - 0.56666..., which is
# 0.43333...
with freeze_time('2013-06-18'):
with db_transaction.manager:
refund_guid = model.cancel(guid, prorated_refund=True)
transaction = tx_model.get(refund_guid)
self.assertEqual(transaction.amount, 433)
示例4: test_cancel_subscription
def test_cancel_subscription(self):
from billy.models.subscription import SubscriptionModel
from billy.models.transaction import TransactionModel
subscription_model = SubscriptionModel(self.testapp.session)
tx_model = TransactionModel(self.testapp.session)
now = datetime.datetime.utcnow()
with db_transaction.manager:
subscription_guid = subscription_model.create(
customer_guid=self.customer_guid,
plan_guid=self.plan_guid,
)
tx_model.create(
subscription_guid=subscription_guid,
transaction_type=tx_model.TYPE_CHARGE,
amount=100,
scheduled_at=now,
)
with freeze_time('2013-08-16 07:00:00'):
canceled_at = datetime.datetime.utcnow()
res = self.testapp.post(
'/v1/subscriptions/{}/cancel'.format(subscription_guid),
extra_environ=dict(REMOTE_USER=self.api_key),
status=200,
)
subscription = res.json
self.assertEqual(subscription['canceled'], True)
self.assertEqual(subscription['canceled_at'], canceled_at.isoformat())
示例5: test_cancel_a_canceled_subscription
def test_cancel_a_canceled_subscription(self):
from billy.models.subscription import SubscriptionModel
from billy.models.transaction import TransactionModel
subscription_model = SubscriptionModel(self.testapp.session)
tx_model = TransactionModel(self.testapp.session)
now = datetime.datetime.utcnow()
with db_transaction.manager:
subscription_guid = subscription_model.create(
customer_guid=self.customer_guid,
plan_guid=self.plan_guid,
)
tx_model.create(
subscription_guid=subscription_guid,
transaction_type=tx_model.TYPE_CHARGE,
amount=100,
scheduled_at=now,
)
self.testapp.post(
'/v1/subscriptions/{}/cancel'.format(subscription_guid),
extra_environ=dict(REMOTE_USER=self.api_key),
status=200,
)
self.testapp.post(
'/v1/subscriptions/{}/cancel'.format(subscription_guid),
extra_environ=dict(REMOTE_USER=self.api_key),
status=400,
)
示例6: test_transaction_list_by_company
def test_transaction_list_by_company(self):
from billy.models.transaction import TransactionModel
transaction_model = TransactionModel(self.testapp.session)
guids = [self.transaction_guid]
with db_transaction.manager:
for i in range(9):
with freeze_time('2013-08-16 00:00:{:02}'.format(i + 1)):
guid = transaction_model.create(
subscription_guid=self.subscription_guid,
transaction_type=transaction_model.TYPE_CHARGE,
amount=10 * i,
payment_uri='/v1/cards/tester',
scheduled_at=datetime.datetime.utcnow(),
)
guids.append(guid)
guids = list(reversed(guids))
res = self.testapp.get(
'/v1/transactions?offset=5&limit=3',
extra_environ=dict(REMOTE_USER=self.api_key),
status=200,
)
self.assertEqual(res.json['offset'], 5)
self.assertEqual(res.json['limit'], 3)
items = res.json['items']
result_guids = [item['guid'] for item in items]
self.assertEqual(set(result_guids), set(guids[5:8]))
示例7: test_get_transaction
def test_get_transaction(self):
from billy.models.transaction import TransactionModel
transaction_model = TransactionModel(self.testapp.session)
res = self.testapp.get(
'/v1/transactions/{}'.format(self.transaction_guid),
extra_environ=dict(REMOTE_USER=self.api_key),
status=200,
)
transaction = transaction_model.get(self.transaction_guid)
self.assertEqual(res.json['guid'], transaction.guid)
self.assertEqual(res.json['created_at'],
transaction.created_at.isoformat())
self.assertEqual(res.json['updated_at'],
transaction.updated_at.isoformat())
self.assertEqual(res.json['scheduled_at'],
transaction.scheduled_at.isoformat())
self.assertEqual(res.json['amount'], str(transaction.amount))
self.assertEqual(res.json['payment_uri'], transaction.payment_uri)
self.assertEqual(res.json['transaction_type'], 'charge')
self.assertEqual(res.json['status'], 'init')
self.assertEqual(res.json['error_message'], None)
self.assertEqual(res.json['failure_count'], 0)
self.assertEqual(res.json['external_id'], None)
self.assertEqual(res.json['subscription_guid'],
transaction.subscription_guid)
示例8: main
def main(argv=sys.argv, processor=None):
logger = logging.getLogger(__name__)
if len(argv) != 2:
usage(argv)
config_uri = argv[1]
setup_logging(config_uri)
settings = get_appsettings(config_uri)
settings = setup_database({}, **settings)
session = settings['session']
subscription_model = SubscriptionModel(session)
tx_model = TransactionModel(session)
maximum_retry = int(settings.get(
'billy.transaction.maximum_retry',
TransactionModel.DEFAULT_MAXIMUM_RETRY,
))
resolver = DottedNameResolver()
if processor is None:
processor_factory = settings['billy.processor_factory']
processor_factory = resolver.maybe_resolve(processor_factory)
processor = processor_factory()
# yield all transactions and commit before we process them, so that
# we won't double process them.
with db_transaction.manager:
logger.info('Yielding transaction ...')
subscription_model.yield_transactions()
with db_transaction.manager:
logger.info('Processing transaction ...')
tx_model.process_transactions(processor, maximum_retry=maximum_retry)
logger.info('Done')
示例9: test_subscription_cancel_with_refund_amount
def test_subscription_cancel_with_refund_amount(self):
from billy.models.transaction import TransactionModel
model = self.make_one(self.session)
tx_model = TransactionModel(self.session)
with db_transaction.manager:
guid = model.create(
customer_guid=self.customer_tom_guid,
plan_guid=self.monthly_plan_guid,
)
tx_guids = model.yield_transactions()
transaction = tx_model.get(tx_guids[0])
transaction.status = tx_model.STATUS_DONE
transaction.external_id = 'MOCK_BALANCED_DEBIT_URI'
self.session.add(transaction)
# let's cancel and refund the latest transaction with amount 566 cent
with db_transaction.manager:
refund_guid = model.cancel(guid, refund_amount=566)
transaction = tx_model.get(refund_guid)
self.assertEqual(transaction.refund_to_guid, tx_guids[0])
self.assertEqual(transaction.subscription_guid, guid)
self.assertEqual(transaction.transaction_type, tx_model.TYPE_REFUND)
self.assertEqual(transaction.amount, 566)
示例10: test_transaction_list_by_subscription
def test_transaction_list_by_subscription(self):
from billy.models.transaction import TransactionModel
from billy.models.subscription import SubscriptionModel
subscription_model = SubscriptionModel(self.testapp.session)
transaction_model = TransactionModel(self.testapp.session)
with db_transaction.manager:
subscription_guid1 = subscription_model.create(
customer_guid=self.customer_guid,
plan_guid=self.plan_guid,
)
subscription_guid2 = subscription_model.create(
customer_guid=self.customer_guid,
plan_guid=self.plan_guid,
)
guids1 = []
guids2 = []
with db_transaction.manager:
for i in range(10):
with freeze_time('2013-08-16 00:00:{:02}'.format(i + 1)):
guid = transaction_model.create(
subscription_guid=subscription_guid1,
transaction_type=transaction_model.TYPE_CHARGE,
amount=10 * i,
payment_uri='/v1/cards/tester',
scheduled_at=datetime.datetime.utcnow(),
)
guids1.append(guid)
for i in range(20):
with freeze_time('2013-08-16 00:00:{:02}'.format(i + 1)):
guid = transaction_model.create(
subscription_guid=subscription_guid2,
transaction_type=transaction_model.TYPE_CHARGE,
amount=10 * i,
payment_uri='/v1/cards/tester',
scheduled_at=datetime.datetime.utcnow(),
)
guids2.append(guid)
guids1 = list(reversed(guids1))
guids2 = list(reversed(guids2))
res = self.testapp.get(
'/v1/subscriptions/{}/transactions'.format(subscription_guid1),
extra_environ=dict(REMOTE_USER=self.api_key),
status=200,
)
items = res.json['items']
result_guids = [item['guid'] for item in items]
self.assertEqual(result_guids, guids1)
res = self.testapp.get(
'/v1/subscriptions/{}/transactions'.format(subscription_guid2),
extra_environ=dict(REMOTE_USER=self.api_key),
status=200,
)
items = res.json['items']
result_guids = [item['guid'] for item in items]
self.assertEqual(result_guids, guids2)
示例11: subscription_cancel
def subscription_cancel(request):
"""Cancel a subscription
"""
# TODO: it appears a DELETE request with body is not a good idea
# for HTTP protocol as many server doesn't support this, this is why
# we use another view with post method, maybe we should use a better
# approach later
company = auth_api_key(request)
form = validate_form(SubscriptionCancelForm, request)
guid = request.matchdict['subscription_guid']
prorated_refund = asbool(form.data.get('prorated_refund', False))
refund_amount = form.data.get('refund_amount')
maximum_retry = int(request.registry.settings.get(
'billy.transaction.maximum_retry',
TransactionModel.DEFAULT_MAXIMUM_RETRY,
))
model = SubscriptionModel(request.session)
tx_model = TransactionModel(request.session)
get_and_check_subscription(request, company, guid)
subscription = model.get(guid)
# TODO: maybe we can find a better way to integrate this with the
# form validation?
if refund_amount is not None:
if subscription.amount is not None:
amount = subscription.amount
else:
amount = subscription.plan.amount
if refund_amount > amount:
return form_errors_to_bad_request(dict(
refund_amount=['refund_amount cannot be greater than '
'subscription amount {}'.format(amount)]
))
if subscription.canceled:
return HTTPBadRequest('Cannot cancel a canceled subscription')
with db_transaction.manager:
tx_guid = model.cancel(
guid,
prorated_refund=prorated_refund,
refund_amount=refund_amount,
)
if tx_guid is not None:
with db_transaction.manager:
tx_model.process_transactions(
processor=request.processor,
guids=[tx_guid],
maximum_retry=maximum_retry,
)
subscription = model.get(guid)
return subscription
示例12: subscription_list_post
def subscription_list_post(request):
"""Create a new subscription
"""
company = auth_api_key(request)
form = validate_form(SubscriptionCreateForm, request)
customer_guid = form.data['customer_guid']
plan_guid = form.data['plan_guid']
amount = form.data.get('amount')
payment_uri = form.data.get('payment_uri')
if not payment_uri:
payment_uri = None
started_at = form.data.get('started_at')
maximum_retry = int(request.registry.settings.get(
'billy.transaction.maximum_retry',
TransactionModel.DEFAULT_MAXIMUM_RETRY,
))
model = SubscriptionModel(request.session)
plan_model = PlanModel(request.session)
customer_model = CustomerModel(request.session)
tx_model = TransactionModel(request.session)
customer = customer_model.get(customer_guid)
if customer.company_guid != company.guid:
return HTTPForbidden('Can only subscribe to your own customer')
if customer.deleted:
return HTTPBadRequest('Cannot subscript to a deleted customer')
plan = plan_model.get(plan_guid)
if plan.company_guid != company.guid:
return HTTPForbidden('Can only subscribe to your own plan')
if plan.deleted:
return HTTPBadRequest('Cannot subscript to a deleted plan')
# create subscription and yield transactions
with db_transaction.manager:
guid = model.create(
customer_guid=customer_guid,
plan_guid=plan_guid,
amount=amount,
payment_uri=payment_uri,
started_at=started_at,
)
tx_guids = model.yield_transactions([guid])
# this is not a deferred subscription, just process transactions right away
if started_at is None:
with db_transaction.manager:
tx_model.process_transactions(
processor=request.processor,
guids=tx_guids,
maximum_retry=maximum_retry,
)
subscription = model.get(guid)
return subscription
示例13: test_cancel_subscription_with_refund_amount
def test_cancel_subscription_with_refund_amount(self):
from billy.models.subscription import SubscriptionModel
from billy.models.transaction import TransactionModel
subscription_model = SubscriptionModel(self.testapp.session)
tx_model = TransactionModel(self.testapp.session)
now = datetime.datetime.utcnow()
with db_transaction.manager:
subscription_guid = subscription_model.create(customer_guid=self.customer_guid, plan_guid=self.plan_guid)
tx_guid = tx_model.create(
subscription_guid=subscription_guid,
transaction_type=tx_model.TYPE_CHARGE,
amount=1000,
scheduled_at=now,
)
subscription = subscription_model.get(subscription_guid)
subscription.period = 1
subscription.next_transaction_at = datetime.datetime(2013, 8, 23)
self.testapp.session.add(subscription)
transaction = tx_model.get(tx_guid)
transaction.status = tx_model.STATUS_DONE
transaction.external_id = "MOCK_BALANCED_DEBIT_URI"
self.testapp.session.add(transaction)
refund_called = []
def mock_refund(transaction):
refund_called.append(transaction)
return "MOCK_PROCESSOR_REFUND_URI"
mock_processor = flexmock(DummyProcessor)
(mock_processor.should_receive("refund").replace_with(mock_refund).once())
res = self.testapp.post(
"/v1/subscriptions/{}/cancel".format(subscription_guid),
dict(refund_amount=234),
extra_environ=dict(REMOTE_USER=self.api_key),
status=200,
)
subscription = res.json
transaction = refund_called[0]
self.testapp.session.add(transaction)
self.assertEqual(transaction.refund_to.guid, tx_guid)
self.assertEqual(transaction.subscription_guid, subscription_guid)
self.assertEqual(transaction.amount, 234)
self.assertEqual(transaction.status, tx_model.STATUS_DONE)
res = self.testapp.get("/v1/transactions", extra_environ=dict(REMOTE_USER=self.api_key), status=200)
guids = [item["guid"] for item in res.json["items"]]
self.assertEqual(set(guids), set([tx_guid, transaction.guid]))
示例14: transaction_get
def transaction_get(request):
"""Get and return a transaction
"""
company = auth_api_key(request)
model = TransactionModel(request.session)
guid = request.matchdict['transaction_guid']
transaction = model.get(guid)
if transaction is None:
return HTTPNotFound('No such transaction {}'.format(guid))
if transaction.subscription.customer.company_guid != company.guid:
return HTTPForbidden('You have no permission to access transaction {}'
.format(guid))
return transaction
示例15: server_info
def server_info(request):
"""Get server information
"""
tx_model = TransactionModel(request.session)
last_transaction = tx_model.get_last_transaction()
last_transaction_dt = None
if last_transaction is not None:
last_transaction_dt = last_transaction.created_at.isoformat()
return dict(
server='Billy - The recurring payment server',
powered_by='BalancedPayments.com',
revision=get_git_rev(),
last_transaction_created_at=last_transaction_dt,
)