Commit c7711ab7 authored by Rafael Monnerat's avatar Rafael Monnerat

slapos_panel: Generalize payment implementation

   Merge Deposit and Invoice payments under the same (or nearly similar) implementation.
parent 7f79b8a7
......@@ -862,12 +862,15 @@ class TestSlapOSVirtualMasterScenario(TestSlapOSVirtualMasterScenarioMixin):
deposit_amount = 42.0 + 99.0
ledger = self.portal.portal_categories.ledger.automated
amount = sum([i.total_price for i in project_owner_person.Entity_getOutstandingDepositAmountList(
currency.getUid(), ledger_uid=ledger.getUid())])
outstanding_amount_list = project_owner_person.Entity_getOutstandingDepositAmountList(
currency.getUid(), ledger_uid=ledger.getUid())
amount = sum([i.total_price for i in outstanding_amount_list])
self.assertEqual(amount, deposit_amount)
project_owner_person.Entity_createExternalPaymentTransactionFromDepositAndRedirect(
currency.getReference())
# Ensure to pay from the website
outstanding_amount = self.web_site.restrictedTraverse(outstanding_amount_list[0].getRelativeUrl())
outstanding_amount.Base_createExternalPaymentTransactionFromOutstandingAmountAndRedirect()
self.tic()
self.logout()
self.login()
......
......@@ -16,36 +16,18 @@ entity = portal.portal_membership.getAuthenticatedMember().getUserValue()
if entity is None:
return '<p>Nothing to pay with your account</p>'
is_payment_configured = 1
for currency_value, secure_service_relative_url in [
(portal.currency_module.EUR, portal.Base_getPayzenServiceRelativeUrl()),
# (portal.currency_module.CNY, portal.Base_getWechatServiceRelativeUrl())
for currency_uid, secure_service_relative_url in [
(portal.currency_module.EUR.getUid(), portal.Base_getPayzenServiceRelativeUrl()),
# (portal.currency_module.CNY.getUid(), portal.Base_getWechatServiceRelativeUrl())
]:
currency_uid = currency_value.getUid()
is_payment_configured = 1
if secure_service_relative_url is None:
is_payment_configured = 0
outstanding_amount_list = entity.Entity_getOutstandingAmountList(
ledger_uid=ledger_uid,
resource_uid=currency_uid
)
for outstanding_amount in outstanding_amount_list:
if not is_payment_configured:
return '<p>Please contact us to handle your payment</p>'
html_content += """
<p><a href="%(payment_url)s">%(total_price)s %(currency)s</a></p>
""" % {
'total_price': outstanding_amount.total_price,
'currency': outstanding_amount.getPriceCurrencyReference(),
'payment_url': '%s/SaleInvoiceTransaction_createExternalPaymentTransactionFromAmountAndRedirect' % outstanding_amount.absolute_url()
}
outstanding_amount_list = entity.Entity_getOutstandingDepositAmountList(
resource_uid=currency_uid,
ledger_uid=ledger_uid
)
for outstanding_amount in outstanding_amount_list:
for method in [entity.Entity_getOutstandingAmountList,
entity.Entity_getOutstandingDepositAmountList]:
for outstanding_amount in method(
ledger_uid=ledger_uid, resource_uid=currency_uid):
if 0 < outstanding_amount.total_price:
if not is_payment_configured:
return '<p>Please contact us to handle your payment</p>'
......@@ -54,8 +36,8 @@ for currency_value, secure_service_relative_url in [
<p><a href="%(payment_url)s">%(total_price)s %(currency)s</a></p>
""" % {
'total_price': outstanding_amount.total_price,
'currency': currency_value.getReference(),
'payment_url': '%s/Entity_createExternalPaymentTransactionFromDepositAndRedirect?currency_reference=%s' % (entity.absolute_url(), currency_value.getReference())
'currency': outstanding_amount.getPriceCurrencyReference(),
'payment_url': '%s/Base_createExternalPaymentTransactionFromOutstandingAmountAndRedirect' % outstanding_amount.absolute_url()
}
if not html_content:
......
portal = context.getPortalObject()
entity = portal.portal_membership.getAuthenticatedMember().getUserValue()
outstanding_amount = context
web_site = context.getWebSiteValue()
assert web_site is not None
assert outstanding_amount.getLedgerUid() == portal.portal_categories.ledger.automated.getUid()
assert outstanding_amount.getDestinationSectionUid() == entity.getUid()
payment_mode = outstanding_amount.Base_getPaymentModeForCurrency(outstanding_amount.getPriceCurrencyUid())
def wrapWithShadow(entity, outstanding_amount, payment_mode):
return entity.Entity_createPaymentTransaction(
entity.Entity_getOutstandingAmountList(
portal_type = outstanding_amount.getPortalType()
method_kw = dict(
section_uid=outstanding_amount.getSourceSectionUid(),
resource_uid=outstanding_amount.getPriceCurrencyUid(),
ledger_uid=outstanding_amount.getLedgerUid(),
group_by_node=False
ledger_uid=outstanding_amount.getLedgerUid()
)
if portal_type == "Sale Invoice Transaction":
return entity.Entity_createPaymentTransaction(
entity.Entity_getOutstandingAmountList(
group_by_node=False,
**method_kw
),
payment_mode=payment_mode
)
elif portal_type == "Subscription Request":
# We include deposit for Subscription Requests.
return entity.Entity_createDepositPaymentTransaction(
entity.Entity_getOutstandingDepositAmountList(
**method_kw),
payment_mode=payment_mode
)
raise ValueError("Unsupported outstanding amount type: %s" % (portal_type))
payment_transaction = entity.Person_restrictMethodAsShadowUser(
shadow_document=entity,
callable_object=wrapWithShadow,
argument_list=[entity, outstanding_amount, payment_mode])
return payment_transaction.PaymentTransaction_redirectToManualPayment(web_site=web_site)
return payment_transaction.PaymentTransaction_redirectToManualPayment()
......@@ -54,7 +54,7 @@
</item>
<item>
<key> <string>id</string> </key>
<value> <string>SaleInvoiceTransaction_createExternalPaymentTransactionFromAmountAndRedirect</string> </value>
<value> <string>Base_createExternalPaymentTransactionFromOutstandingAmountAndRedirect</string> </value>
</item>
</dictionary>
</pickle>
......
portal = context.getPortalObject()
currency_value = None
if currency_reference:
currency_value = portal.portal_catalog.getResultValue(
portal_type="Currency",
reference=currency_reference,
validation_state=("validated", "published"))
if currency_value is None:
raise ValueError("Unknown Currency: %s" % currency_reference)
ledger_uid = portal.portal_categories.ledger.automated.getUid()
currency_uid = currency_value.getUid()
payment_mode = context.Base_getPaymentModeForCurrency(currency_value.getUid())
outstanding_amount_list = context.Entity_getOutstandingDepositAmountList(
resource_uid=currency_uid, ledger_uid=ledger_uid)
if len(outstanding_amount_list) != 1:
raise ValueError("It was expected exactly one amount to pay for %s currency (%s found)" % (
currency_uid, outstanding_amount_list))
deposit_price = outstanding_amount_list[0].total_price
if 0 >= deposit_price:
raise ValueError("Nothing to pay")
def wrapWithShadow(entity, total_amount, currency_value, payment_mode):
return entity.Entity_addDepositPayment(
total_amount,
currency_value.getRelativeUrl(),
payment_mode=payment_mode
)
payment_transaction = context.Person_restrictMethodAsShadowUser(
shadow_document=context,
callable_object=wrapWithShadow,
argument_list=[context, deposit_price, currency_value, payment_mode])
web_site = context.getWebSiteValue()
return payment_transaction.PaymentTransaction_redirectToManualPayment(web_site=web_site)
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_bind_names</string> </key>
<value>
<object>
<klass>
<global name="_reconstructor" module="copy_reg"/>
</klass>
<tuple>
<global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
<global name="object" module="__builtin__"/>
<none/>
</tuple>
<state>
<dictionary>
<item>
<key> <string>_asgns</string> </key>
<value>
<dictionary>
<item>
<key> <string>name_container</string> </key>
<value> <string>container</string> </value>
</item>
<item>
<key> <string>name_context</string> </key>
<value> <string>context</string> </value>
</item>
<item>
<key> <string>name_m_self</string> </key>
<value> <string>script</string> </value>
</item>
<item>
<key> <string>name_subpath</string> </key>
<value> <string>traverse_subpath</string> </value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</state>
</object>
</value>
</item>
<item>
<key> <string>_params</string> </key>
<value> <string>currency_reference</string> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>Entity_createExternalPaymentTransactionFromDepositAndRedirect</string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
......@@ -3,4 +3,4 @@ if (context.getPaymentMode() == "wechat"):
elif (context.getPaymentMode() == "payzen"):
return context.PaymentTransaction_redirectToManualPayzenPayment()
else:
raise context.PaymentTransaction_triggerPaymentCheckAlarmAndRedirectToPanel(result="contact_us")
return context.PaymentTransaction_triggerPaymentCheckAlarmAndRedirectToPanel(result="contact_us")
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment