예제 #1
0
    def make_envelope(cls, args):
        """
        This function creates the envelope definition for the
        order form.
        document 1 (html) has multiple tags:
         /l1q/ and /l2q/ -- quantities: drop down
         /l1e/ and /l2e/ -- extended: payment lines
         /l3t/ -- total -- formula

        The envelope has two recipients.
          recipient 1 - signer
          recipient 2 - cc
        The envelope will be sent first to the signer.
        After it is signed, a copy is sent to the cc person.

        #################################################################
        #                                                               #
        # NOTA BENA: This method programmatically constructs the        #
        #            order form. For many use cases, it would be        #
        #            better to create the order form as a template      #
        #            using the DocuSign web tool as WYSIWYG             #
        #            form designer.                                     #
        #                                                               #
        #################################################################

        """

        # Order form constants
        l1_name = "Harmonica"
        l1_price = 5
        l1_description = f"${l1_price} each"
        l2_name = "Xylophone"
        l2_price = 150
        l2_description = f"${l2_price} each"
        currency_multiplier = 100

        # read the html file from a local directory
        # The read could raise an exception if the file is not available!
        doc1_file = "order_form.html"
        with open(path.join(demo_docs_path, doc1_file), "r") as file:
            doc1_html_v1 = file.read()

        # Substitute values into the HTML
        # Substitute for: {signerName}, {signerEmail}, {cc_name}, {cc_email}
        doc1_html_v2 = doc1_html_v1.replace("{signer_name}", args["signer_name"]) \
            .replace("{signer_email}", args["signer_email"]) \
            .replace("{cc_name}", args["cc_name"]) \
            .replace("{cc_email}", args["cc_email"])

        # create the envelope definition
        envelope_definition = EnvelopeDefinition(
            email_subject="Please complete your order")
        # add the document
        doc1_b64 = base64.b64encode(bytes(doc1_html_v2,
                                          "utf-8")).decode("ascii")
        doc1 = Document(
            document_base64=doc1_b64,
            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=args["signer_email"],
                         name=args["signer_name"],
                         recipient_id="1",
                         routing_order="1")
        # create a cc recipient to receive a copy of the documents
        cc1 = CarbonCopy(email=args["cc_email"],
                         name=args["cc_name"],
                         recipient_id="2",
                         routing_order="2")
        # Create signHere fields (also known as tabs) on the documents,
        # We"re using anchor (autoPlace) positioning
        sign_here1 = SignHere(anchor_string="/sn1/",
                              anchor_y_offset="10",
                              anchor_units="pixels",
                              anchor_x_offset="20")
        list_item0 = ListItem(text="none", value="0")
        list_item1 = ListItem(text="1", value="1")
        list_item2 = ListItem(text="2", value="2")
        list_item3 = ListItem(text="3", value="3")
        list_item4 = ListItem(text="4", value="4")
        list_item5 = ListItem(text="5", value="5")
        list_item6 = ListItem(text="6", value="6")
        list_item7 = ListItem(text="7", value="7")
        list_item8 = ListItem(text="8", value="8")
        list_item9 = ListItem(text="9", value="9")
        list_item10 = ListItem(text="10", value="10")

        listl1q = List(font="helvetica",
                       font_size="size11",
                       anchor_string="/l1q/",
                       anchor_y_offset="-10",
                       anchor_units="pixels",
                       anchor_x_offset="0",
                       list_items=[
                           list_item0, list_item1, list_item2, list_item3,
                           list_item4, list_item5, list_item6, list_item7,
                           list_item8, list_item9, list_item10
                       ],
                       required="true",
                       tab_label="l1q")
        listl2q = List(font="helvetica",
                       font_size="size11",
                       anchor_string="/l2q/",
                       anchor_y_offset="-10",
                       anchor_units="pixels",
                       anchor_x_offset="0",
                       list_items=[
                           list_item0, list_item1, list_item2, list_item3,
                           list_item4, list_item5, list_item6, list_item7,
                           list_item8, list_item9, list_item10
                       ],
                       required="true",
                       tab_label="l2q")
        # create two formula tabs for the extended price on the line items
        formulal1e = FormulaTab(
            font="helvetica",
            font_size="size11",
            anchor_string="/l1e/",
            anchor_y_offset="-8",
            anchor_units="pixels",
            anchor_x_offset="105",
            tab_label="l1e",
            formula=f"[l1q] * {l1_price}",
            round_decimal_places="0",
            required="true",
            locked="true",
            disable_auto_size="false",
        )
        formulal2e = FormulaTab(
            font="helvetica",
            font_size="size11",
            anchor_string="/l2e/",
            anchor_y_offset="-8",
            anchor_units="pixels",
            anchor_x_offset="105",
            tab_label="l2e",
            formula=f"[l2q] * {l2_price}",
            round_decimal_places="0",
            required="true",
            locked="true",
            disable_auto_size="false",
        )
        # Formula for the total
        formulal3t = FormulaTab(
            font="helvetica",
            bold="true",
            font_size="size12",
            anchor_string="/l3t/",
            anchor_y_offset="-8",
            anchor_units="pixels",
            anchor_x_offset="50",
            tab_label="l3t",
            formula="[l1e] + [l2e]",
            round_decimal_places="0",
            required="true",
            locked="true",
            disable_auto_size="false",
        )
        # Payment line items
        payment_line_iteml1 = PaymentLineItem(name=l1_name,
                                              description=l1_description,
                                              amount_reference="l1e")
        payment_line_iteml2 = PaymentLineItem(name=l2_name,
                                              description=l2_description,
                                              amount_reference="l2e")
        payment_details = PaymentDetails(
            gateway_account_id=args["gateway_account_id"],
            currency_code="USD",
            gateway_name=args["gateway_name"],
            line_items=[payment_line_iteml1, payment_line_iteml2])
        # Hidden formula for the payment itself
        formula_payment = FormulaTab(
            tab_label="payment",
            formula=f"([l1e] + [l2e]) * {currency_multiplier}",
            round_decimal_places="0",
            payment_details=payment_details,
            hidden="true",
            required="true",
            locked="true",
            document_id="1",
            page_number="1",
            x_position="0",
            y_position="0")

        # Tabs are set per recipient / signer
        signer1_tabs = Tabs(
            sign_here_tabs=[sign_here1],
            list_tabs=[listl1q, listl2q],
            formula_tabs=[formulal1e, formulal2e, formulal3t, formula_payment])
        signer1.tabs = signer1_tabs

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

        # Request that the envelope be sent by setting |status| to "sent".
        # To request that the envelope be created as a draft, set to "created"
        envelope_definition.status = args["status"]

        return envelope_definition
예제 #2
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 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