|
| 1 | +import base64 |
| 2 | +from os import path |
| 3 | + |
| 4 | +from docusign_esign import AccountsApi, EnvelopesApi, EnvelopeDefinition, Document, Signer, SignHere, Tabs, Recipients, RecipientIdentityInputOption, RecipientIdentityPhoneNumber |
| 5 | +from docusign_esign.client.api_exception import ApiException |
| 6 | +from flask import current_app as app, session, request |
| 7 | + |
| 8 | +from ....consts import demo_docs_path, pattern |
| 9 | +from ....docusign import create_api_client |
| 10 | +from ....ds_config import DS_CONFIG |
| 11 | +from ....error_handlers import process_error |
| 12 | + |
| 13 | + |
| 14 | +class Eg020Controller: |
| 15 | + @staticmethod |
| 16 | + def get_args(): |
| 17 | + """Get required session and request arguments""" |
| 18 | + # More data validation would be a good idea here |
| 19 | + # Strip anything other than the characters listed |
| 20 | + signer_email = pattern.sub("", request.form.get("signer_email")) |
| 21 | + signer_name = pattern.sub("", request.form.get("signer_name")) |
| 22 | + country_code = pattern.sub("", request.form.get("country_code")) |
| 23 | + phone_number = pattern.sub("", request.form.get("phone_number")) |
| 24 | + envelope_args = { |
| 25 | + "signer_email": signer_email, |
| 26 | + "signer_name": signer_name, |
| 27 | + "phone_number": phone_number, |
| 28 | + "country_code": country_code, |
| 29 | + "status": "sent", |
| 30 | + "workflow_id": session['workflow_id'] |
| 31 | + } |
| 32 | + args = { |
| 33 | + "account_id": session["ds_account_id"], # represents your {ACCOUNT_ID} |
| 34 | + "base_path": session["ds_base_path"], |
| 35 | + "access_token": session["ds_access_token"], # represnts your {ACCESS_TOKEN} |
| 36 | + "envelope_args": envelope_args |
| 37 | + } |
| 38 | + return args |
| 39 | + |
| 40 | + @staticmethod |
| 41 | + def worker(args): |
| 42 | + """ |
| 43 | + 1. Create an api client |
| 44 | + 2. Create an envelope definition object |
| 45 | + """ |
| 46 | + # Construct your API headers |
| 47 | + # Step 2 start |
| 48 | + api_client = create_api_client(base_path=args["base_path"], access_token=args["access_token"]) |
| 49 | + # Step 2 end |
| 50 | + |
| 51 | + # Construct your envelope |
| 52 | + # Step 3 start |
| 53 | + envelope_definition = EnvelopeDefinition( |
| 54 | + email_subject="Please sign this document set" |
| 55 | + ) |
| 56 | + |
| 57 | + # Open the example file |
| 58 | + with open(path.join(demo_docs_path, DS_CONFIG["doc_pdf"]), "rb") as file: |
| 59 | + content_bytes = file.read() |
| 60 | + base64_file_content = base64.b64encode(content_bytes).decode("ascii") |
| 61 | + |
| 62 | + # Add a document |
| 63 | + document1 = Document( # create the DocuSign document object |
| 64 | + document_base64=base64_file_content, |
| 65 | + document_id="1", # a label used to reference the doc |
| 66 | + file_extension="pdf", # many different document types are accepted |
| 67 | + name="Lorem" # can be different from actual file name |
| 68 | + ) |
| 69 | + |
| 70 | + envelope_definition.documents = [document1] |
| 71 | + envelope_definition.status = args["envelope_args"]["status"] |
| 72 | + |
| 73 | + # Create your signature tab |
| 74 | + sign_here1 = SignHere( |
| 75 | + name="SignHereTab", |
| 76 | + anchor_string="/sn1/", |
| 77 | + anchor_units="pixels", |
| 78 | + anchor_y_offset="10", |
| 79 | + anchor_x_offset="20", |
| 80 | + tab_label="SignHereTab", |
| 81 | + page_number="1", |
| 82 | + document_id="1", |
| 83 | + # A 1- to 8-digit integer or 32-character GUID to match recipient IDs on your own systems. |
| 84 | + # This value is referenced in the Tabs element below to assign tabs on a per-recipient basis. |
| 85 | + recipient_id="1" # represents your {RECIPIENT_ID} |
| 86 | + ) |
| 87 | + |
| 88 | + signer1 = Signer( |
| 89 | + email=args["envelope_args"]["signer_email"], # Represents your {signer_email} |
| 90 | + name=args["envelope_args"]["signer_name"], # Represents your {signer_name} |
| 91 | + role_name="", |
| 92 | + note="", |
| 93 | + status="created", |
| 94 | + delivery_method="email", |
| 95 | + recipient_id="1", # Represents your {RECIPIENT_ID} |
| 96 | + routing_order="1", |
| 97 | + identity_verification={ "workflowId": session['workflow_id'], "steps": "null", "inputOptions":[{"name":"phone_number_list","valueType":"PhoneNumberList","phoneNumberList":[{"countryCode":args["envelope_args"]["country_code"],"code":"1","number":args["envelope_args"]["phone_number"]}]}], "idCheckConfigurationName":""}, |
| 98 | + tabs=Tabs(sign_here_tabs=[sign_here1]) |
| 99 | + ) |
| 100 | + |
| 101 | + # Tabs are set per recipient |
| 102 | + envelope_definition.recipients = Recipients(signers=[signer1]) |
| 103 | + # Step 3 end |
| 104 | + |
| 105 | + # Call the eSignature REST API |
| 106 | + # Step 4 start |
| 107 | + envelopes_api = EnvelopesApi(api_client) |
| 108 | + results = envelopes_api.create_envelope(account_id=args["account_id"], envelope_definition=envelope_definition) |
| 109 | + # Step 4 end |
| 110 | + |
| 111 | + return results |
| 112 | + |
| 113 | + @staticmethod |
| 114 | + def get_workflow(args): |
| 115 | + """Retrieve the workflow id""" |
| 116 | + try: |
| 117 | + api_client = create_api_client(base_path=args["base_path"], access_token=args["access_token"]) |
| 118 | + |
| 119 | + workflow_details = AccountsApi(api_client) |
| 120 | + workflow_response = workflow_details.get_account_identity_verification(account_id=args["account_id"]) |
| 121 | + |
| 122 | + # Check that idv authentication is enabled |
| 123 | + # The workflow ID is a hard-coded value which is unique to this phone authentication workflow |
| 124 | + if workflow_response.identity_verification: |
| 125 | + session['workflow_id'] = "c368e411-1592-4001-a3df-dca94ac539ae" |
| 126 | + return session['workflow_id'] |
| 127 | + |
| 128 | + else: |
| 129 | + return None |
| 130 | + |
| 131 | + except ApiException as err: |
| 132 | + return process_error(err) |
| 133 | + |
| 134 | + |
| 135 | + |
0 commit comments