本文整理汇总了Python中billy.models.subscription.SubscriptionModel.cancel方法的典型用法代码示例。如果您正苦于以下问题:Python SubscriptionModel.cancel方法的具体用法?Python SubscriptionModel.cancel怎么用?Python SubscriptionModel.cancel使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类billy.models.subscription.SubscriptionModel
的用法示例。
在下文中一共展示了SubscriptionModel.cancel方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: subscription_cancel
# 需要导入模块: from billy.models.subscription import SubscriptionModel [as 别名]
# 或者: from billy.models.subscription.SubscriptionModel import cancel [as 别名]
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