Beispiel #1
0
    def create_with_payment(cls, tpl, user, insurance_info, envelope_args):
        """Create envelope with payment feature included
        Parameters:
            tpl (str): Template path for the document
            user (dict}: User information
            insurance_info (dict): Insurance information for enrollment
            envelope_args (dict): Parameters for the envelope
        Returns:
            EnvelopeDefinition object that will be submitted to Docusign
        """
        currency_multiplier = 100
        discount_percent = 5
        insurance_rate_percent = 10

        # Read template and fill it up
        with open(path.join(TPL_PATH, tpl), 'r') as file:
            content_bytes = file.read()

        # Get base64 logo representation to paste it into the HTML file
        with open(path.join(IMG_PATH, 'logo.png'), 'rb') as file:
            img_base64_src = base64.b64encode(file.read()).decode('utf-8')
        content_bytes = Environment(
            loader=BaseLoader).from_string(content_bytes).render(
                user_name=f"{user['first_name']} {user['last_name']}",
                user_email=user['email'],
                address=f"{user['street']}, {user['city']}, {user['state']}",
                zip_code=user['zip_code'],
                detail_1=insurance_info['detail1']['name'],
                detail_2=insurance_info['detail2']['name'],
                value_detail_1=insurance_info['detail1']['value'],
                value_detail_2=insurance_info['detail2']['value'],
                img_base64_src=img_base64_src)

        base64_file_content = base64.b64encode(bytes(content_bytes,
                                                     'utf-8')).decode('ascii')

        # Create the envelope definition
        envelope_definition = EnvelopeDefinition(
            email_subject='Buy New Insurance')

        # Create the document
        doc1 = Document(
            document_base64=base64_file_content,
            name=
            'Insurance order form',  # Can be different from actual file name
            file_extension='html',  # Source data format
            document_id='1'  # A label used to reference the doc
        )
        envelope_definition.documents = [doc1]

        # Create a signer recipient to sign the document
        signer1 = Signer(email=user['email'],
                         name=f"{user['first_name']} {user['last_name']}",
                         recipient_id='1',
                         routing_order='1',
                         client_user_id=envelope_args['signer_client_id'])
        sign_here1 = SignHere(
            anchor_string='/sn1/',
            anchor_y_offset='10',
            anchor_units='pixels',
            anchor_x_offset='20',
        )

        # Create number tabs for the coverage amount and deductible
        coverage = Number(
            font='helvetica',
            font_size='size11',
            anchor_string='/l1e/',
            anchor_y_offset='-7',
            anchor_units='pixels',
            tab_label='l1e',
            required='true',
        )

        deductible = Number(
            font='helvetica',
            font_size='size11',
            anchor_string='/l2e/',
            anchor_y_offset='-7',
            anchor_units='pixels',
            tab_label='l2e',
            required='true',
        )

        # Create checkbox and trigger tabs to apply the discount
        checkbox = Checkbox(
            font='helvetica',
            font_size='size11',
            anchor_string='/cb/',
            anchor_y_offset='-4',
            anchor_units='pixels',
            anchor_x_offset='-8',
            tab_label='checkbox',
            height='50',
            bold='true',
        )

        trigger = FormulaTab(
            anchor_string='/trigger/',
            font_color='white',
            anchor_y_offset='10',
            tab_label='trigger',
            conditional_parent_label='checkbox',
            conditional_parent_value='on',
            formula='1',
            required='true',
            locked='true',
        )

        discount = FormulaTab(
            font='helvetica',
            font_size='size11',
            bold='true',
            anchor_string='/dt/',
            anchor_y_offset='-4',
            anchor_units='pixels',
            anchor_x_offset='0',
            tab_label='discount',
            formula=f"if([trigger] > 0, {discount_percent}, 0)",
            round_decimal_places='0',
            locked='true',
        )

        # Create a formula tab for the insurance price
        total = f'([l1e]-[l2e]) * {insurance_rate_percent}/100'

        formula_total = FormulaTab(
            font='helvetica',
            bold='true',
            font_size='size12',
            anchor_string='/l4t/',
            anchor_y_offset='-6',
            anchor_units='pixels',
            anchor_x_offset='84',
            tab_label='l4t',
            formula=f'({total}) - (({total}) * [discount]/100)',
            round_decimal_places='2',
            required='true',
            locked='true',
        )

        # Create payment line item
        payment_line_iteml1 = PaymentLineItem(name='Insurance payment',
                                              description='$[l4t]',
                                              amount_reference='l4t')

        payment_details = PaymentDetails(
            gateway_account_id=envelope_args['gateway_account_id'],
            currency_code='USD',
            gateway_name=envelope_args['gateway_name'],
            line_items=[payment_line_iteml1])

        # Create a hidden formula tab for the payment itself
        formula_payment = FormulaTab(
            tab_label='payment',
            formula=f'([l4t]) * {currency_multiplier}',
            round_decimal_places='2',
            payment_details=payment_details,
            hidden='true',
            required='true',
            locked='true',
            document_id='1',
            page_number='1',
            x_position='0',
            y_position='0')

        # Create tabs for the signer
        signer1_tabs = Tabs(
            sign_here_tabs=[sign_here1],
            number_tabs=[coverage, deductible],
            formula_tabs=[formula_payment, formula_total, discount, trigger],
            checkbox_tabs=[checkbox])
        signer1.tabs = signer1_tabs

        # Add the recipients to the envelope object
        recipients = Recipients(signers=[signer1])
        envelope_definition.recipients = recipients

        # Request that the envelope be sent by setting status to 'sent'
        envelope_definition.status = 'sent'

        return envelope_definition
    def make_template_req(cls):
        """Creates template req object"""

        # document 1 (pdf)
        #
        # The template has two recipient roles.
        # recipient 1 - signer
        # recipient 2 - cc
        with open(path.join(demo_docs_path, doc_file), "rb") as file:
            content_bytes = file.read()
        base64_file_content = base64.b64encode(content_bytes).decode("ascii")

        # Create the document model
        document = Document(  # create the DocuSign document object
            document_base64=base64_file_content,
            name="Lorem Ipsum",  # can be different from actual file name
            file_extension="pdf",  # many different document types are accepted
            document_id=1  # a label used to reference the doc
        )

        # Create the signer recipient model
        signer = Signer(role_name="signer",
                        recipient_id="1",
                        routing_order="1")
        # create a cc recipient to receive a copy of the envelope (transaction)
        cc = CarbonCopy(role_name="cc", recipient_id="2", routing_order="2")
        # Create fields using absolute positioning
        # Create a sign_here tab (field on the document)
        sign_here = SignHere(document_id="1",
                             page_number="1",
                             x_position="191",
                             y_position="148")
        check1 = Checkbox(document_id="1",
                          page_number="1",
                          x_position="75",
                          y_position="417",
                          tab_label="ckAuthorization")
        check2 = Checkbox(document_id="1",
                          page_number="1",
                          x_position="75",
                          y_position="447",
                          tab_label="ckAuthentication")
        check3 = Checkbox(document_id="1",
                          page_number="1",
                          x_position="75",
                          y_position="478",
                          tab_label="ckAgreement")
        check4 = Checkbox(document_id="1",
                          page_number="1",
                          x_position="75",
                          y_position="508",
                          tab_label="ckAcknowledgement")
        list1 = List(document_id="1",
                     page_number="1",
                     x_position="142",
                     y_position="291",
                     font="helvetica",
                     font_size="size14",
                     tab_label="list",
                     required="false",
                     list_items=[
                         ListItem(text="Red", value="red"),
                         ListItem(text="Orange", value="orange"),
                         ListItem(text="Yellow", value="yellow"),
                         ListItem(text="Green", value="green"),
                         ListItem(text="Blue", value="blue"),
                         ListItem(text="Indigo", value="indigo"),
                         ListItem(text="Violet", value="violet")
                     ])
        number1 = Number(document_id="1",
                         page_number="1",
                         x_position="163",
                         y_position="260",
                         font="helvetica",
                         font_size="size14",
                         tab_label="numbersOnly",
                         width="84",
                         required="false")
        radio_group = RadioGroup(document_id="1",
                                 group_name="radio1",
                                 radios=[
                                     Radio(page_number="1",
                                           x_position="142",
                                           y_position="384",
                                           value="white",
                                           required="false"),
                                     Radio(page_number="1",
                                           x_position="74",
                                           y_position="384",
                                           value="red",
                                           required="false"),
                                     Radio(page_number="1",
                                           x_position="220",
                                           y_position="384",
                                           value="blue",
                                           required="false")
                                 ])
        text = Text(document_id="1",
                    page_number="1",
                    x_position="153",
                    y_position="230",
                    font="helvetica",
                    font_size="size14",
                    tab_label="text",
                    height="23",
                    width="84",
                    required="false")
        # Add the tabs model to the signer
        # The Tabs object wants arrays of the different field/tab types
        signer.tabs = Tabs(sign_here_tabs=[sign_here],
                           checkbox_tabs=[check1, check2, check3, check4],
                           list_tabs=[list1],
                           number_tabs=[number1],
                           radio_group_tabs=[radio_group],
                           text_tabs=[text])

        # Top object:
        template_request = EnvelopeTemplate(
            documents=[document],
            email_subject="Please sign this document",
            recipients=Recipients(signers=[signer], carbon_copies=[cc]),
            description="Example template created via the API",
            name=template_name,
            shared="false",
            status="created")

        return template_request
    def create_with_payment(cls, tpl, student, activity_info, envelope_args):
        """Create envelope with payment feature included
        Parameters:
            tpl (str): template path for the document
            student (dict}: student information
            activity_info (dict): activity information for enrollment
            envelope_args (dict): parameters for the envelope
        Returns:
            EnvelopeDefinition object that will be submitted to Docusign
        """
        l1_name = activity_info['name']
        l1_price = activity_info['price']
        l1_description = f'${l1_price}'
        currency_multiplier = 100

        # Read template and fill it up
        with open(path.join(TPL_PATH, tpl), 'r') as file:
            content_bytes = file.read()

        # Get base64 logo representation to paste it into the html
        with open(path.join(IMG_PATH, 'logo.png'), 'rb') as file:
            img_base64_src = base64.b64encode(file.read()).decode('utf-8')
        content_bytes = Environment(
            loader=BaseLoader).from_string(content_bytes).render(
                user_name=f"{student['first_name']} {student['last_name']}",
                user_email=student['email'],
                activity_name=l1_name,
                activity_price=l1_price,
                img_base64_src=img_base64_src)

        base64_file_content = base64.b64encode(bytes(content_bytes,
                                                     'utf-8')).decode('ascii')

        # Create the envelope definition
        envelope_definition = EnvelopeDefinition(
            email_subject='Register for extra-curricular activity')

        # Create the document
        doc1 = Document(
            document_base64=base64_file_content,
            name='Order form',  # can be different from actual file name
            file_extension='html',  # Source data format.
            document_id='1'  # a label used to reference the doc
        )
        envelope_definition.documents = [doc1]

        # Create a signer recipient to sign the document
        signer1 = Signer(
            email=student['email'],
            name=f"{student['first_name']} {student['last_name']}",
            recipient_id='1',
            routing_order='1',
            client_user_id=envelope_args['signer_client_id'])
        sign_here1 = SignHere(anchor_string='/sn1/',
                              anchor_y_offset='10',
                              anchor_units='pixels',
                              anchor_x_offset='20')

        # Create number tab for the price
        numberl1e = Number(
            font='helvetica',
            font_size='size11',
            anchor_string='/l1e/',
            anchor_y_offset='-8',
            anchor_units='pixels',
            anchor_x_offset='-7',
            tab_label='l1e',
            formula=l1_price,
            required='true',
            locked='true',
            disable_auto_size='false',
        )

        # Create formula tab for the total
        formula_total = FormulaTab(
            font='helvetica',
            bold='true',
            font_size='size12',
            anchor_string='/l2t/',
            anchor_y_offset='-6',
            anchor_units='pixels',
            anchor_x_offset='30',
            tab_label='l2t',
            formula='[l1e]',
            round_decimal_places='2',
            required='true',
            locked='true',
            disable_auto_size='false',
        )

        # Create payment line items
        payment_line_iteml1 = PaymentLineItem(name=l1_name,
                                              description=l1_description,
                                              amount_reference='l1e')

        payment_details = PaymentDetails(
            gateway_account_id=envelope_args['gateway_account_id'],
            currency_code='USD',
            gateway_name=envelope_args['gateway_name'],
            line_items=[payment_line_iteml1])

        # Create hidden formula tab for the payment itself
        formula_payment = FormulaTab(
            tab_label='payment',
            formula=f'{l1_price} * {currency_multiplier}',
            round_decimal_places='2',
            payment_details=payment_details,
            hidden='true',
            required='true',
            locked='true',
            document_id='1',
            page_number='1',
            x_position='0',
            y_position='0')

        # Create tabs for signer
        signer1_tabs = Tabs(sign_here_tabs=[sign_here1],
                            formula_tabs=[formula_payment, formula_total],
                            number_tabs=[numberl1e])
        signer1.tabs = signer1_tabs

        # Add the recipients to the envelope object
        recipients = Recipients(signers=[signer1])
        envelope_definition.recipients = recipients

        # Request that the envelope be sent by setting |status| to 'sent'.
        envelope_definition.status = 'sent'

        return envelope_definition