|
| 1 | +require 'bundler/inline' |
| 2 | + |
| 3 | +gemfile do |
| 4 | + source 'https://rubygems.org' |
| 5 | + gem 'docusign_esign',' ~> 3.15.0' |
| 6 | +end |
| 7 | + |
| 8 | +class ESign |
| 9 | +end |
| 10 | + |
| 11 | +require 'docusign_esign' |
| 12 | +require_relative '../app/services/api_creator' |
| 13 | +require_relative '../app/services/e_sign/eg002_signing_via_email_service.rb' |
| 14 | +require 'yaml' |
| 15 | + |
| 16 | +$SCOPES = [ |
| 17 | + "signature", "impersonation" |
| 18 | +] |
| 19 | + |
| 20 | +def load_config_data |
| 21 | + config_file_path = 'jwt_config.yml' |
| 22 | + begin |
| 23 | + config_file_contents = File.read(config_file_path) |
| 24 | + rescue Errno::ENOENT |
| 25 | + $stderr.puts "missing config file" |
| 26 | + raise |
| 27 | + end |
| 28 | + YAML.load(config_file_contents) |
| 29 | +end |
| 30 | + |
| 31 | +def get_consent |
| 32 | + url_scopes = $SCOPES.join('+'); |
| 33 | + # Construct consent URL |
| 34 | + redirect_uri = "https://developers.docusign.com/platform/auth/consent"; |
| 35 | + consent_url = "https://#{CONFIG["authorization_server"]}/oauth/auth?response_type=code&" + |
| 36 | + "scope=#{url_scopes}&client_id=#{CONFIG["jwt_integration_key"]}&" + |
| 37 | + "redirect_uri=#{redirect_uri}" |
| 38 | + |
| 39 | + puts "Open the following URL in your browser to grant consent to the application:" |
| 40 | + puts consent_url |
| 41 | + puts "Consent granted? \n 1)Yes \n 2)No" |
| 42 | + continue = gets; |
| 43 | + if continue.chomp == "1" |
| 44 | + return true; |
| 45 | + else |
| 46 | + puts "Please grant consent" |
| 47 | + exit |
| 48 | + end |
| 49 | +end |
| 50 | + |
| 51 | +def authenticate |
| 52 | + configuration = DocuSign_eSign::Configuration.new |
| 53 | + configuration.debugging = true |
| 54 | + api_client = DocuSign_eSign::ApiClient.new(configuration) |
| 55 | + api_client.set_oauth_base_path(CONFIG["authorization_server"]) |
| 56 | + |
| 57 | + rsa_pk = 'docusign_private_key.txt' |
| 58 | + begin |
| 59 | + token = api_client.request_jwt_user_token(CONFIG["jwt_integration_key"], CONFIG["impersonated_user_guid"], rsa_pk, expires_in=3600, $SCOPES) |
| 60 | + user_info_response = api_client.get_user_info(token.access_token) |
| 61 | + account = user_info_response.accounts.find(&:is_default) |
| 62 | + |
| 63 | + account_info = { |
| 64 | + access_token: token.access_token, |
| 65 | + account_id: account.account_id, |
| 66 | + base_path: account.base_uri |
| 67 | + } |
| 68 | + account_info |
| 69 | + rescue OpenSSL::PKey::RSAError => exception |
| 70 | + Rails.logger.error exception.inspect |
| 71 | + if File.read(rsa_pk).starts_with? '{RSA_PRIVATE_KEY}' |
| 72 | + fail "Please add your private RSA key to: #{rsa_pk}" |
| 73 | + else |
| 74 | + raise |
| 75 | + end |
| 76 | + rescue DocuSign_eSign::ApiError => exception |
| 77 | + body = JSON.parse(exception.response_body) |
| 78 | + if body['error'] == "consent_required" |
| 79 | + authenticate if get_consent |
| 80 | + else |
| 81 | + puts "API Error" |
| 82 | + puts body['error'] |
| 83 | + puts body['message'] |
| 84 | + exit |
| 85 | + end |
| 86 | + end |
| 87 | +end |
| 88 | + |
| 89 | +def get_args(apiAccountId, accessToken, basePath) |
| 90 | + puts "Enter the signer's email address: " |
| 91 | + signerEmail = gets.chomp |
| 92 | + puts "Enter the signer's name: " |
| 93 | + signerName = gets.chomp |
| 94 | + puts "Enter the carbon copy's email address: " |
| 95 | + ccSignerEmail = gets.chomp |
| 96 | + puts "Enter the carbon copy's name: " |
| 97 | + ccSignerName = gets.chomp |
| 98 | + |
| 99 | + envelope_args = { |
| 100 | + signer_email: signerEmail, |
| 101 | + signer_name: signerName, |
| 102 | + cc_email: ccSignerEmail, |
| 103 | + cc_name: ccSignerName, |
| 104 | + status: 'sent', |
| 105 | + doc_docx: '../data/World_Wide_Corp_Battle_Plan_Trafalgar.docx', |
| 106 | + doc_pdf: '../data/World_Wide_Corp_lorem.pdf' |
| 107 | + } |
| 108 | + args = { |
| 109 | + account_id: apiAccountId, |
| 110 | + base_path: basePath, |
| 111 | + access_token: accessToken, |
| 112 | + envelope_args: envelope_args |
| 113 | + } |
| 114 | + |
| 115 | + return args |
| 116 | +end |
| 117 | + |
| 118 | +def main |
| 119 | + load_config_data |
| 120 | + account_info = authenticate |
| 121 | + args = get_args(account_info[:account_id], account_info[:access_token], account_info[:base_path]) |
| 122 | + results = ESign::Eg002SigningViaEmailService.new(args).worker |
| 123 | + puts "Successfully sent envelope with envelope ID: #{results['envelope_id']}" |
| 124 | +end |
| 125 | + |
| 126 | +CONFIG = load_config_data |
| 127 | +main |
0 commit comments