Skip to content

Commit fd4c2ee

Browse files
2 parents c81d93c + d3bf2f6 commit fd4c2ee

2 files changed

Lines changed: 253 additions & 76 deletions

File tree

‎examples/eSignature/eg044FocusedView.sh‎

Lines changed: 229 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
# Check that we're in a bash shell
55
if [[ $SHELL != *"bash"* ]]; then
6-
echo "PROBLEM: Run these scripts from within the bash shell."
6+
echo "PROBLEM: Run these scripts from within the bash shell."
77
fi
88

99
ds_access_token_path="config/ds_access_token.txt"
@@ -12,12 +12,90 @@ document_path="demo_documents/World_Wide_Corp_lorem.pdf"
1212

1313
# Obtain your OAuth token
1414
# Note: Substitute these values with your own
15-
ACCESS_TOKEN=$(cat ${ds_access_token_path})
15+
ACCESS_TOKEN=$(cat "${ds_access_token_path}")
1616

1717
# Set up variables for full code example
1818
# Note: Substitute these values with your own
19-
ACCOUNT_ID=$(cat ${api_account_id_path})
19+
ACCOUNT_ID=$(cat "${api_account_id_path}")
2020

21+
signer_country_code=""
22+
signer_phone_number=""
23+
use_phone_authentication=0
24+
workflow_id=""
25+
temp_files=()
26+
27+
cleanup_temp_files() {
28+
for temp_path in "$@"; do
29+
if [[ -n "$temp_path" && -f "$temp_path" ]]; then
30+
rm -f "$temp_path"
31+
fi
32+
done
33+
}
34+
35+
write_api_failure_and_exit() {
36+
local step_name="$1"
37+
local http_status="$2"
38+
local response_file="$3"
39+
40+
echo ""
41+
echo "${step_name} failed."
42+
43+
if [[ -n "$http_status" ]]; then
44+
echo "HTTP status: ${http_status}"
45+
fi
46+
47+
if [[ -n "$response_file" && -f "$response_file" ]]; then
48+
echo "DocuSign error: $(cat "$response_file")"
49+
fi
50+
51+
cleanup_temp_files "${temp_files[@]}"
52+
exit 1
53+
}
54+
55+
echo ""
56+
read -r -p "Enter signer country code to enable optional phone authentication (press Enter to skip): " signer_country_code
57+
read -r -p "Enter signer phone number to enable optional phone authentication (press Enter to skip): " signer_phone_number
58+
59+
signer_country_code=$(echo "$signer_country_code" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')
60+
signer_phone_number=$(echo "$signer_phone_number" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')
61+
62+
has_signer_country_code=0
63+
has_signer_phone_number=0
64+
65+
if [[ -n "$signer_country_code" ]]; then
66+
has_signer_country_code=1
67+
fi
68+
69+
if [[ -n "$signer_phone_number" ]]; then
70+
has_signer_phone_number=1
71+
fi
72+
73+
if [[ "$has_signer_phone_number" -eq 1 && "$has_signer_country_code" -eq 0 ]]; then
74+
echo "Country code is required when phone number is provided."
75+
exit 1
76+
fi
77+
78+
if [[ "$has_signer_country_code" -eq 1 && "$has_signer_phone_number" -eq 0 ]]; then
79+
echo "Phone number is required when country code is provided."
80+
exit 1
81+
fi
82+
83+
if [[ "$has_signer_phone_number" -eq 1 ]]; then
84+
use_phone_authentication=1
85+
signer_country_code="${signer_country_code#+}"
86+
signer_phone_number=$(echo "$signer_phone_number" | tr -d '[:space:]()-')
87+
signer_phone_number="${signer_phone_number#+}"
88+
89+
if [[ ! "$signer_country_code" =~ ^[0-9]{1,4}$ ]]; then
90+
echo "Country code must contain 1 to 4 digits."
91+
exit 1
92+
fi
93+
94+
if [[ ! "$signer_phone_number" =~ ^[0-9]{4,20}$ ]]; then
95+
echo "Phone number must contain 4 to 20 digits."
96+
exit 1
97+
fi
98+
fi
2199

22100
# Create the envelope.
23101
# The signer recipient includes a clientUserId setting
@@ -28,26 +106,93 @@ ACCOUNT_ID=$(cat ${api_account_id_path})
28106
base_path="https://demo.docusign.net/restapi"
29107

30108
# temp files:
31-
request_data=$(mktemp /tmp/request-eg-001.XXXXXX)
32-
response=$(mktemp /tmp/response-eg-001.XXXXXX)
33-
doc1_base64=$(mktemp /tmp/eg-001-doc1.XXXXXX)
109+
request_data=$(mktemp /tmp/request-eg-044.XXXXXX)
110+
response=$(mktemp /tmp/response-eg-044.XXXXXX)
111+
doc1_base64=$(mktemp /tmp/eg-044-doc1.XXXXXX)
112+
temp_files+=("$request_data" "$response" "$doc1_base64")
34113

35114
# Fetch doc and encode
36-
cat $document_path | base64 > $doc1_base64
115+
base64 < "$document_path" | tr -d '\n' > "$doc1_base64"
116+
117+
if [[ "$use_phone_authentication" -eq 1 ]]; then
118+
identity_verification_response=$(mktemp /tmp/identity-verification-eg-044.XXXXXX)
119+
temp_files+=("$identity_verification_response")
120+
121+
echo ""
122+
echo "Looking up Phone Authentication identity verification workflow..."
123+
124+
identity_http_status=$(curl --silent --show-error \
125+
--header "Authorization: Bearer ${ACCESS_TOKEN}" \
126+
--header "Accept: application/json" \
127+
--request GET "${base_path}/v2.1/accounts/${ACCOUNT_ID}/identity_verification" \
128+
--output "${identity_verification_response}" \
129+
--write-out "%{http_code}")
130+
131+
if [[ "$identity_http_status" -lt 200 || "$identity_http_status" -ge 300 ]]; then
132+
write_api_failure_and_exit "Identity verification lookup request" "$identity_http_status" "$identity_verification_response"
133+
fi
134+
135+
workflow_id=$(php -r '
136+
$payload = json_decode(file_get_contents($argv[1]), true);
137+
if (!is_array($payload) || !isset($payload["identityVerification"]) || !is_array($payload["identityVerification"])) {
138+
exit(0);
139+
}
140+
foreach ($payload["identityVerification"] as $workflow) {
141+
if (isset($workflow["defaultName"]) && $workflow["defaultName"] === "Phone Authentication") {
142+
if (isset($workflow["workflowId"])) {
143+
echo $workflow["workflowId"];
144+
}
145+
break;
146+
}
147+
}
148+
' "$identity_verification_response")
149+
150+
if [[ -z "$workflow_id" ]]; then
151+
echo "IDENTITY_WORKFLOW_INVALID_ID"
152+
cleanup_temp_files "${temp_files[@]}"
153+
exit 1
154+
fi
155+
fi
37156

38157
echo ""
39158
echo "Sending the envelope request to DocuSign..."
159+
echo ""
160+
161+
identity_verification_json=""
162+
if [[ "$use_phone_authentication" -eq 1 ]]; then
163+
identity_verification_json=$(cat <<EOF
164+
,
165+
"identityVerification": {
166+
"workflowId": "${workflow_id}",
167+
"steps": null,
168+
"inputOptions": [
169+
{
170+
"name": "phone_number_list",
171+
"valueType": "PhoneNumberList",
172+
"phoneNumberList": [
173+
{
174+
"countryCode": "${signer_country_code}",
175+
"number": "${signer_phone_number}"
176+
}
177+
]
178+
}
179+
],
180+
"idCheckConfigurationName": ""
181+
}
182+
EOF
183+
)
184+
fi
185+
186+
doc1_base64_content=$(cat "$doc1_base64")
40187

41188
# Concatenate the different parts of the request
42189
#ds-snippet-start:eSign44Step2
43-
printf \
44-
'{
190+
cat > "$request_data" <<EOF
191+
{
45192
"emailSubject": "Please sign this document set",
46193
"documents": [
47194
{
48-
"documentBase64": "' > $request_data
49-
cat $doc1_base64 >> $request_data
50-
printf '",
195+
"documentBase64": "${doc1_base64_content}",
51196
"name": "Lorem Ipsum",
52197
"fileExtension": "pdf",
53198
"documentId": "1"
@@ -56,45 +201,52 @@ printf \
56201
"recipients": {
57202
"signers": [
58203
{
59-
"email": "'"${SIGNER_EMAIL}"'",
60-
"name": "'"${SIGNER_NAME}"'",
204+
"email": "${SIGNER_EMAIL}",
205+
"name": "${SIGNER_NAME}",
61206
"recipientId": "1",
62207
"routingOrder": "1",
63-
"clientUserId": "1000",
64-
"tabs": {
65-
"signHereTabs": [
66-
{
67-
"anchorString": "/sn1/",
68-
"anchorUnits": "pixels",
69-
"anchorXOffset": "20",
70-
"anchorYOffset": "10"
71-
}
72-
]
73-
}
208+
"clientUserId": "1000"${identity_verification_json}
74209
}
75210
]
76211
},
77212
"status": "sent"
78-
}' >> $request_data
213+
}
214+
EOF
79215
#ds-snippet-end:eSign44Step2
80216

81217
# Call DocuSign to create the envelope
82-
83218
#ds-snippet-start:eSign44Step3
84-
curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
85-
--header "Content-Type: application/json" \
86-
--data-binary @${request_data} \
87-
--request POST ${base_path}/v2.1/accounts/${ACCOUNT_ID}/envelopes \
88-
--output ${response}
219+
create_envelope_http_status=$(curl --silent --show-error \
220+
--header "Authorization: Bearer ${ACCESS_TOKEN}" \
221+
--header "Content-Type: application/json" \
222+
--data-binary @"${request_data}" \
223+
--request POST "${base_path}/v2.1/accounts/${ACCOUNT_ID}/envelopes" \
224+
--output "${response}" \
225+
--write-out "%{http_code}")
89226
#ds-snippet-end:eSign44Step3
90227

228+
if [[ "$create_envelope_http_status" -lt 200 || "$create_envelope_http_status" -ge 300 ]]; then
229+
write_api_failure_and_exit "Create envelope request" "$create_envelope_http_status" "$response"
230+
fi
231+
232+
echo "Response: $(cat "$response")"
91233
echo ""
92-
echo "Response:" `cat $response`
93-
echo ""
94234

95-
# Pull out the envelopeId
96-
envelope_id=`cat $response | grep envelopeId | sed 's/.*\"envelopeId\":\"//' | sed 's/\",.*//'`
235+
envelope_id=$(php -r '
236+
$payload = json_decode(file_get_contents($argv[1]), true);
237+
if (isset($payload["envelopeId"])) {
238+
echo $payload["envelopeId"];
239+
}
240+
' "$response")
241+
242+
if [[ -z "$envelope_id" ]]; then
243+
echo "Create envelope request failed: envelopeId was not returned."
244+
cleanup_temp_files "${temp_files[@]}"
245+
exit 1
246+
fi
247+
97248
echo "EnvelopeId: ${envelope_id}"
249+
echo ""
98250

99251
# Create a recipient view (an embedded signing view)
100252
# that the signer will directly open in their browser to sign.
@@ -105,61 +257,70 @@ echo "EnvelopeId: ${envelope_id}"
105257
# query parameters passed back from DocuSign
106258

107259
# temp files:
108-
request_data=$(mktemp /tmp/request-eg-001.XXXXXX)
109-
response=$(mktemp /tmp/response-eg-001.XXXXXX)
260+
request_data=$(mktemp /tmp/request-eg-044.XXXXXX)
261+
response=$(mktemp /tmp/response-eg-044.XXXXXX)
262+
temp_files+=("$request_data" "$response")
110263

111264
#ds-snippet-start:eSign44Step4
112-
printf \
113-
'{
265+
cat > "$request_data" <<EOF
266+
{
114267
"returnUrl": "http://httpbin.org/get",
115268
"authenticationMethod": "none",
116-
"email": "'"${SIGNER_EMAIL}"'",
117-
"userName": "'"${SIGNER_NAME}"'",
269+
"email": "${SIGNER_EMAIL}",
270+
"userName": "${SIGNER_NAME}",
118271
"clientUserId": 1000,
119272
"frameAncestors": ["http://localhost:8080", "https://apps-d.docusign.com"],
120-
"messageOrigins": ["https://apps-d.docusign.com"],
121-
}' >> $request_data
273+
"messageOrigins": ["https://apps-d.docusign.com"]
274+
}
275+
EOF
122276
#ds-snippet-end:eSign44Step4
123277

124-
# Create the recipient view and call the API to initiate the signing
125-
126278
echo ""
127279
echo "Requesting the url for the embedded signing..."
128280
echo ""
129281

130282
#ds-snippet-start:eSign44Step5
131-
Status=$(curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
132-
--header "Content-Type: application/json" \
133-
--data-binary @${request_data} \
134-
--request POST ${base_path}/v2.1/accounts/${ACCOUNT_ID}/envelopes/${envelope_id}/views/recipient \
135-
--output ${response})
283+
create_recipient_view_http_status=$(curl --silent --show-error \
284+
--header "Authorization: Bearer ${ACCESS_TOKEN}" \
285+
--header "Content-Type: application/json" \
286+
--data-binary @"${request_data}" \
287+
--request POST "${base_path}/v2.1/accounts/${ACCOUNT_ID}/envelopes/${envelope_id}/views/recipient" \
288+
--output "${response}" \
289+
--write-out "%{http_code}")
136290
#ds-snippet-end:eSign44Step5
137291

138-
if [[ "$Status" -gt "201" ]] ; then
139-
echo ""
140-
echo "Signing request failed."
141-
echo ""
142-
cat $response
143-
exit 0
292+
if [[ "$create_recipient_view_http_status" -lt 200 || "$create_recipient_view_http_status" -ge 300 ]]; then
293+
write_api_failure_and_exit "Create recipient view request" "$create_recipient_view_http_status" "$response"
144294
fi
145295

146-
signing_url=`cat $response | grep url | sed 's/.*\"url\":\"//' | sed 's/\".*//'`
147-
# ***DS.snippet.0.end
296+
echo "Response: $(cat "$response")"
297+
298+
signing_url=$(php -r '
299+
$payload = json_decode(file_get_contents($argv[1]), true);
300+
if (isset($payload["url"])) {
301+
echo $payload["url"];
302+
}
303+
' "$response")
304+
305+
if [[ -z "$signing_url" ]]; then
306+
echo "Create recipient view request failed: signing URL was not returned."
307+
cleanup_temp_files "${temp_files[@]}"
308+
exit 1
309+
fi
148310

149311
host_url="http://localhost:8080"
150-
if which xdg-open &> /dev/null ; then
151-
xdg-open $host_url
152-
elif which open &> /dev/null ; then
153-
open $host_url
154-
elif which start &> /dev/null ; then
155-
start $host_url
312+
if which xdg-open &> /dev/null; then
313+
xdg-open "$host_url"
314+
elif which open &> /dev/null; then
315+
open "$host_url"
316+
elif which start &> /dev/null; then
317+
start "$host_url"
156318
fi
319+
157320
php ./examples/eSignature/lib/startServerForFocusedView.php "$INTEGRATION_KEY_AUTH_CODE" "$signing_url"
158321

159322
# cleanup
160-
rm "$request_data"
161-
rm "$response"
162-
rm "$doc1_base64"
323+
cleanup_temp_files "${temp_files[@]}"
163324

164325
echo ""
165326
echo "Done."

0 commit comments

Comments
 (0)