<?xml version="1.0"?>
<ZopeData>
  <record id="1" aka="AAAAAAAAAAE=">
    <pickle>
      <global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
    </pickle>
    <pickle>
      <dictionary>
        <item>
            <key> <string>Script_magic</string> </key>
            <value> <int>3</int> </value>
        </item>
        <item>
            <key> <string>_bind_names</string> </key>
            <value>
              <object>
                <klass>
                  <global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
                </klass>
                <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>_body</string> </key>
            <value> <string encoding="cdata"><![CDATA[

from DateTime import DateTime\n
portal = context.getPortalObject()\n
service = None\n
\n
def addTicket(transaction):\n
  portal = transaction.getPortalObject()\n
  ticket = portal.portal_catalog.getResultValue(portal_type=\'Support Request\',\n
    default_source_project_uid=transaction.getUid(),\n
    validation_state=\'validated\')\n
  tag = \'ticket_creation_%s\' % transaction.getUid()\n
  activate_kw={\'tag\': tag}\n
  if ticket is None and \\\n
      portal.portal_activities.countMessageWithTag(tag) == 0:\n
    ticket = portal.support_request_module.newContent(\n
      portal_type=\'Support Request\',\n
      source_project_uid=transaction.getUid(),\n
      activate_kw=activate_kw)\n
    ticket.validate(comment=\'New automatic ticket for %s\' % \\\n
      transaction.getTitle())\n
    return ticket\n
  return None\n
\n
try:\n
  service = portal.portal_secure_payments.find()\n
except ValueError, e:\n
  portal.system_event_module.newContent(\n
    title=\'Transaction %s cannot be updated, because of service error: %s\' % \\\n
      (context.getTitle(), e), portal_type=\'Payzen Event\',\n
      source_value=service, destination_value=context)\\\n
    .confirm()\n
  return\n
\n
def createRequestManualPaymentTicket(transaction):\n
  # no previous payment\n
  # shall create ticket + web message for user\n
  portal = transaction.getPortalObject()\n
  ticket = addTicket(context)\n
  if ticket is not None:\n
    tag = \'site_message_creation_%s\' % transaction.getUid()\n
    activate_kw={\'tag\': tag}\n
    site_message = portal.portal_catalog.getResultValue(\n
      portal_type=\'Site Message\', default_follow_up_uid=ticket.getUid())\n
    if site_message is None and \\\n
        portal.portal_activities.countMessageWithTag(tag) == 0:\n
      site_message = portal.event_module.newContent(\n
        portal_type=\'Site Message\',\n
        start_date=DateTime(),\n
        destination_uid=transaction.getDestinationSectionUid(),\n
        follow_up=ticket.getRelativeUrl(),\n
        source_uid=transaction.getSourceSectionUid(),\n
        text_content=\'Please pay your payment by clicking <a \'\\\n
          \'href="%s/AccountingTransaction_startPayment">here</a>.\' % \\\n
            transaction.getRelativeUrl(),\n
        activate_kw=activate_kw)\n
      site_message.start(comment=\'Requested manual payment.\')\n
\n
state = context.getSimulationState()\n
if state == \'confirmed\':\n
  if context.PaymentTransaction_getPreviousPayzenId() is not None:\n
    # there is previous payment\n
    context.setStartDate(DateTime())\n
    context.updateCausalityState()\n
    portal.system_event_module.newContent(\n
       title=\'Transaction %s Payzen registration\' % context.getTitle(),\n
       portal_type=\'Payzen Event\',\n
       source_value=service,\n
       destination_value=context).registerPayzen()\n
    comment=\'Automatically duplicated in payzen.\'\n
  else:\n
    createRequestManualPaymentTicket(context)\n
    comment=\'Requested manual payment\'\n
  context.start(comment=comment)\n
\n
elif state == \'started\':\n
  transaction_id = portal.restrictedTraverse(\n
    portal.portal_preferences.getPreferredPayzenIntegrationSite()\n
    ).getMappingFromCategory(\'causality/%s\' % context.getRelativeUrl())\n
  if transaction_id != \'Causality/%s\' % context.getRelativeUrl():\n
    transaction_id = transaction_id.split(\'/\')[1]\n
  else:\n
    transaction_id = None\n
\n
  if transaction_id is None:\n
    # inform administrators\n
    addTicket(context)\n
  else:\n
    # so the payment is registered in payzen\n
    portal.system_event_module.newContent(\n
      title=\'Transaction %s Payzen status update\' % context.getTitle(),\n
      portal_type=\'Payzen Event\',\n
      source_value=service,\n
      destination_value=context).updateStatus()\n
    if context.getSimulationState() != \'stopped\':\n
      # not paid yet and shall be, lets open ticket\n
      addTicket(context)\n
else:\n
  raise NotImplementedError(\'State %s is not supported\' % state)\n


]]></string> </value>
        </item>
        <item>
            <key> <string>_params</string> </key>
            <value> <string></string> </value>
        </item>
        <item>
            <key> <string>id</string> </key>
            <value> <string>PaymentTransaction_updateStatus</string> </value>
        </item>
      </dictionary>
    </pickle>
  </record>
</ZopeData>
