|
| 1 | +# Embedded Sending: |
| 2 | +# 1. create a draft envelope with three documents |
| 3 | +# 2. Open the sending view of the DocuSign web tool |
| 4 | +# |
| 5 | + |
| 6 | +# Configuration |
| 7 | +# 1. Search for and update '{USER_EMAIL}' and '{USER_FULLNAME}'. |
| 8 | +# They occur and re-occur multiple times below. |
| 9 | +# 2. Obtain an OAuth access token from |
| 10 | +# https://developers.hqtest.tst/oauth-token-generator |
| 11 | +access_token='{ACCESS_TOKEN}' |
| 12 | +# 3. Obtain your accountId from demo.docusign.com -- the account id is shown in |
| 13 | +# the drop down on the upper right corner of the screen by your picture or |
| 14 | +# the default picture. |
| 15 | +account_id='{ACCOUNT_ID}' |
| 16 | + |
| 17 | +# Check that we're in a bash shell |
| 18 | +if [[ $SHELL != *"bash"* ]]; then |
| 19 | + echo "PROBLEM: Run these scripts from within the bash shell." |
| 20 | +fi |
| 21 | +base_path="https://demo.docusign.net/restapi" |
| 22 | + |
| 23 | +# The sending editor can be opened in either of two views: |
| 24 | +echo "" |
| 25 | +PS3='Select the initial sending view: ' |
| 26 | +options=("Tagging view" "Recipients and documents view") |
| 27 | +select opt in "${options[@]}" |
| 28 | +do |
| 29 | + case $opt in |
| 30 | + "Tagging view") |
| 31 | + starting_view=tagging |
| 32 | + break |
| 33 | + ;; |
| 34 | + "Recipients and documents view") |
| 35 | + starting_view=recipient |
| 36 | + break |
| 37 | + ;; |
| 38 | + esac |
| 39 | +done |
| 40 | + |
| 41 | +# Create the document request body |
| 42 | +# document 1 (html) has tag **signature_1** |
| 43 | +# document 2 (docx) has tag /sn1/ |
| 44 | +# document 3 (pdf) has tag /sn1/ |
| 45 | +# |
| 46 | +# The envelope has two recipients. |
| 47 | +# recipient 1 - signer |
| 48 | +# recipient 2 - cc |
| 49 | +# The envelope will be sent first to the signer. |
| 50 | +# After it is signed, a copy is sent to the cc person. |
| 51 | + |
| 52 | +# temp files: |
| 53 | +request_data=$(mktemp /tmp/request-eg-002.XXXXXX) |
| 54 | +response=$(mktemp /tmp/response-eg-002.XXXXXX) |
| 55 | +doc1_base64=$(mktemp /tmp/eg-002-doc1.XXXXXX) |
| 56 | +doc2_base64=$(mktemp /tmp/eg-002-doc2.XXXXXX) |
| 57 | +doc3_base64=$(mktemp /tmp/eg-002-doc3.XXXXXX) |
| 58 | + |
| 59 | +# Fetch docs and encode |
| 60 | +cat ../demo_documents/doc_1.html | base64 > $doc1_base64 |
| 61 | +cat ../demo_documents/World_Wide_Corp_Battle_Plan_Trafalgar.docx | base64 > $doc2_base64 |
| 62 | +cat ../demo_documents/World_Wide_Corp_lorem.pdf | base64 > $doc3_base64 |
| 63 | + |
| 64 | +echo "" |
| 65 | +echo "Sending the envelope request to DocuSign..." |
| 66 | +echo "The envelope has three documents. Processing time will be about 15 seconds." |
| 67 | +echo "Results:" |
| 68 | +echo "" |
| 69 | + |
| 70 | +# Concatenate the different parts of the request |
| 71 | +printf \ |
| 72 | +'{ |
| 73 | + "emailSubject": "Please sign this document set", |
| 74 | + "documents": [ |
| 75 | + { |
| 76 | + "documentBase64": "' > $request_data |
| 77 | + cat $doc1_base64 >> $request_data |
| 78 | + printf '", |
| 79 | + "name": "Order acknowledgement", |
| 80 | + "fileExtension": "html", |
| 81 | + "documentId": "1" |
| 82 | + }, |
| 83 | + { |
| 84 | + "documentBase64": "' >> $request_data |
| 85 | + cat $doc2_base64 >> $request_data |
| 86 | + printf '", |
| 87 | + "name": "Battle Plan", |
| 88 | + "fileExtension": "docx", |
| 89 | + "documentId": "2" |
| 90 | + }, |
| 91 | + { |
| 92 | + "documentBase64": "' >> $request_data |
| 93 | + cat $doc3_base64 >> $request_data |
| 94 | + printf '", |
| 95 | + "name": "Lorem Ipsum", |
| 96 | + "fileExtension": "pdf", |
| 97 | + "documentId": "3" |
| 98 | + } |
| 99 | + ], |
| 100 | + "recipients": { |
| 101 | + "carbonCopies": [ |
| 102 | + { |
| 103 | + "email": "{USER_EMAIL}", |
| 104 | + "name": "Charles Copy", |
| 105 | + "recipientId": "2", |
| 106 | + "routingOrder": "2" |
| 107 | + } |
| 108 | + ], |
| 109 | + "signers": [ |
| 110 | + { |
| 111 | + "email": "{USER_EMAIL}", |
| 112 | + "name": "{USER_FULLNAME}", |
| 113 | + "recipientId": "1", |
| 114 | + "routingOrder": "1", |
| 115 | + "tabs": { |
| 116 | + "signHereTabs": [ |
| 117 | + { |
| 118 | + "anchorString": "**signature_1**", |
| 119 | + "anchorUnits": "pixels", |
| 120 | + "anchorXOffset": "20", |
| 121 | + "anchorYOffset": "10" |
| 122 | + }, |
| 123 | + { |
| 124 | + "anchorString": "/sn1/", |
| 125 | + "anchorUnits": "pixels", |
| 126 | + "anchorXOffset": "20", |
| 127 | + "anchorYOffset": "10" |
| 128 | + } |
| 129 | + ] |
| 130 | + } |
| 131 | + } |
| 132 | + ] |
| 133 | + }, |
| 134 | + "status": "draft" |
| 135 | +}' >> $request_data |
| 136 | + |
| 137 | +curl --header "Authorization: Bearer ${access_token}" \ |
| 138 | + --header "Content-Type: application/json" \ |
| 139 | + --data-binary @${request_data} \ |
| 140 | + --request POST ${base_path}/v2/accounts/${account_id}/envelopes \ |
| 141 | + --output $response |
| 142 | + |
| 143 | +echo "" |
| 144 | +cat $response |
| 145 | + |
| 146 | +# pull out the envelopeId |
| 147 | +envelope_id=`cat $response | grep envelopeId | sed 's/.*\"envelopeId\": \"//' | sed 's/\",.*//'` |
| 148 | + |
| 149 | +echo "" |
| 150 | +echo "Requesting the sender view url" |
| 151 | + |
| 152 | +# The returnUrl is normally your own web app. DocuSign will redirect |
| 153 | +# the signer to returnUrl when the sending ceremony completes. |
| 154 | +# For this example, we'll use http://httpbin.org/get to show the |
| 155 | +# query parameters passed back from DocuSign |
| 156 | +curl --header "Authorization: Bearer ${access_token}" \ |
| 157 | + --header "Content-Type: application/json" \ |
| 158 | + --data-binary "{\"returnUrl\": \"http://httpbin.org/get\"}" \ |
| 159 | + --request POST ${base_path}/v2/accounts/${account_id}/envelopes/${envelope_id}/views/sender \ |
| 160 | + --output $response |
| 161 | + |
| 162 | +echo "" |
| 163 | +cat $response |
| 164 | +sending_ceremony_url=`cat $response | grep url | sed 's/.*\"url\": \"//' | sed 's/\".*//'` |
| 165 | +# Next, we update the returned url if we want to start with the Recipient |
| 166 | +# and Documents view |
| 167 | +if [ "$starting_view" = "recipient" ]; then |
| 168 | + sending_ceremony_url=`printf "${sending_ceremony/send=1/send=0}"` |
| 169 | +fi |
| 170 | + |
| 171 | +echo "" |
| 172 | +printf "The sending ceremony URL is ${sending_ceremony_url}\n" |
| 173 | +printf "It is only valid for a couple of minutes. Attempting to automatically open your browser...\n" |
| 174 | +if which xdg-open &> /dev/null ; then |
| 175 | + xdg-open "$sending_ceremony_url" |
| 176 | +elif which open &> /dev/null ; then |
| 177 | + open "$sending_ceremony_url" |
| 178 | +elif which start &> /dev/null ; then |
| 179 | + start "$sending_ceremony_url" |
| 180 | +fi |
| 181 | + |
| 182 | +# cleanup |
| 183 | +rm "$request_data" |
| 184 | +rm "$response" |
| 185 | +rm "$doc1_base64" |
| 186 | +rm "$doc2_base64" |
| 187 | +rm "$doc3_base64" |
| 188 | + |
| 189 | +echo "" |
| 190 | +echo "" |
| 191 | +echo "Done." |
| 192 | +echo "" |
| 193 | + |
0 commit comments