| 1 | const Crypto = require('crypto'); |
| 2 | const generateSignature = (establishData, accessKey) => { |
| 3 | let query = ''; |
| 4 | query += `accessId=${establishData.accessId}`; |
| 5 | query += `&merchantId=${establishData.merchantId}`; |
| 6 | query += `&description=${establishData.description}`; |
| 7 | query += `¤cy=${establishData.currency}`; |
| 8 | query += `&amount=${establishData.amount}`; |
| 9 | |
| 10 | if (establishData.displayAmount) query += `&displayAmount=${establishData.displayAmount}`; |
| 11 | if (establishData.minimumBalance) query += `&minimumBalance=${establishData.minimumBalance}`; |
| 12 | |
| 13 | query += `&merchantReference=${establishData.merchantReference}`; |
| 14 | query += `&paymentType=${establishData.paymentType}`; |
| 15 | |
| 16 | if (establishData.timeZone) query += `&timeZone=${establishData.timeZone}`; |
| 17 | |
| 18 | if (establishData.paymentType === 'Recurring' && establishData.recurrence) { |
| 19 | if (establishData.recurrence.startDate) query += `&recurrence.startDate=${establishData.recurrence.startDate}`; |
| 20 | if (establishData.recurrence.endDate) query += `&recurrence.endDate=${establishData.recurrence.endDate}`; |
| 21 | if (establishData.recurrence.frequency) query += `&recurrence.frequency=${establishData.recurrence.frequency}`; |
| 22 | if (establishData.recurrence.frequencyUnit) query += `&recurrence.frequencyUnit=${establishData.recurrence.frequencyUnit}`; |
| 23 | if (establishData.recurrence.frequencyUnitType) query += `&recurrence.frequencyUnitType=${establishData.recurrence.frequencyUnitType}`; |
| 24 | if (establishData.recurrence.recurringAmount) query += `&recurrence.recurringAmount=${establishData.recurrence.recurringAmount}`; |
| 25 | if (establishData.recurrence.automaticCapture) query += `&recurrence.automaticCapture=${establishData.recurrence.automaticCapture}`; |
| 26 | } |
| 27 | |
| 28 | if (establishData.verification) { |
| 29 | if (establishData.verification.status) query += `&verification.status=${establishData.verification.status}`; |
| 30 | if (establishData.verification.verifyCustomer) query += `&verification.verifyCustomer=${establishData.verification.verifyCustomer}`; |
| 31 | } |
| 32 | |
| 33 | if (establishData.customer) { |
| 34 | if (establishData.customer.customerId) query += `&customer.customerId=${establishData.customer.customerId}`; |
| 35 | if (establishData.customer.externalId) query += `&customer.externalId=${establishData.customer.externalId}`; |
| 36 | if (establishData.customer.name) query += `&customer.name=${establishData.customer.name}`; |
| 37 | if (establishData.customer.vip !== undefined) query += `&customer.vip=${establishData.customer.vip}`; |
| 38 | if (establishData.customer.taxId) query += `&customer.taxId=${establishData.customer.taxId}`; |
| 39 | if (establishData.customer.driverLicense) { |
| 40 | if (establishData.customer.driverLicense.number) query += `&customer.driverLicense.number=${establishData.customer.driverLicense.number}`; |
| 41 | if (establishData.customer.driverLicense.state) query += `&customer.driverLicense.state=${establishData.customer.driverLicense.state}`; |
| 42 | } |
| 43 | if (establishData.customer.address) { |
| 44 | if (establishData.customer.address.address1) query += `&customer.address.address1=${establishData.customer.address.address1}`; |
| 45 | if (establishData.customer.address.address2) query += `&customer.address.address2=${establishData.customer.address.address2}`; |
| 46 | if (establishData.customer.address.city) query += `&customer.address.city=${establishData.customer.address.city}`; |
| 47 | if (establishData.customer.address.state) query += `&customer.address.state=${establishData.customer.address.state}`; |
| 48 | if (establishData.customer.address.zip) query += `&customer.address.zip=${establishData.customer.address.zip}`; |
| 49 | if (establishData.customer.address.country) query += `&customer.address.country=${establishData.customer.address.country}`; |
| 50 | } |
| 51 | if (establishData.customer.phone) query += `&customer.phone=${establishData.customer.phone}`; |
| 52 | if (establishData.customer.email) query += `&customer.email=${establishData.customer.email}`; |
| 53 | if (establishData.customer.balance) query += `&customer.balance=${establishData.customer.balance}`; |
| 54 | if (establishData.customer.currency) query += `&customer.currency=${establishData.customer.currency}`; |
| 55 | if (establishData.customer.enrollDate) query += `&customer.enrollDate=${establishData.customer.enrollDate}`; |
| 56 | if (establishData.customer.dateOfBirth) query += `&customer.dateOfBirth=${establishData.customer.dateOfBirth}`; |
| 57 | } |
| 58 | |
| 59 | if (establishData.account) { |
| 60 | if (establishData.account.nameOnAccount) query += `&account.nameOnAccount=${establishData.account.nameOnAccount}`; |
| 61 | if (establishData.account.name) query += `&account.name=${establishData.account.name}`; |
| 62 | if (establishData.account.type) query += `&account.type=${establishData.account.type}`; |
| 63 | if (establishData.account.profile) query += `&account.profile=${establishData.account.profile}`; |
| 64 | if (establishData.account.accountNumber) query += `&account.accountNumber=${establishData.account.accountNumber}`; |
| 65 | if (establishData.account.routingNumber) query += `&account.routingNumber=${establishData.account.routingNumber}`; |
| 66 | } |
| 67 | |
| 68 | // Required for International Money Transfer |
| 69 | if (establishData.beneficiary) { |
| 70 | if (establishData.beneficiary.name) query += `&beneficiary.name=${establishData.beneficiary.name}`; |
| 71 | if (establishData.beneficiary.taxId) query += `&beneficiary.taxId=${establishData.beneficiary.taxId}`; |
| 72 | if (establishData.beneficiary.address) { |
| 73 | if (establishData.beneficiary.address.address1) query += `&beneficiary.address.address1=${establishData.beneficiary.address.address1}`; |
| 74 | if (establishData.beneficiary.address.city) query += `&beneficiary.address.city=${establishData.beneficiary.address.city}`; |
| 75 | if (establishData.beneficiary.address.state) query += `&beneficiary.address.state=${establishData.beneficiary.address.state}`; |
| 76 | if (establishData.beneficiary.address.zip) query += `&beneficiary.address.zip=${establishData.beneficiary.address.zip}`; |
| 77 | if (establishData.beneficiary.address.country) query += `&beneficiary.address.country=${establishData.beneficiary.address.country}`; |
| 78 | } |
| 79 | if (establishData.beneficiary.dateOfBirth) query += `&beneficiary.dateOfBirth=${establishData.beneficiary.dateOfBirth}`; |
| 80 | } |
| 81 | if (establishData.beneficiaryAccount) { |
| 82 | if (establishData.beneficiaryAccount.paymentProvider) { |
| 83 | if (establishData.beneficiaryAccount.paymentProvider.name) query += `&beneficiaryAccount.paymentProvider.name=${establishData.beneficiaryAccount.paymentProvider.name}`; |
| 84 | if (establishData.beneficiaryAccount.paymentProvider.routingNumber) query += `&beneficiaryAccount.paymentProvider.routingNumber=${establishData.beneficiaryAccount.paymentProvider.routingNumber}`; |
| 85 | if (establishData.beneficiaryAccount.paymentProvider.swift) query += `&beneficiaryAccount.paymentProvider.swift=${establishData.beneficiaryAccount.paymentProvider.swift}`; |
| 86 | if (establishData.beneficiaryAccount.paymentProvider.country) query += `&beneficiaryAccount.paymentProvider.country=${establishData.beneficiaryAccount.paymentProvider.country}`; |
| 87 | } |
| 88 | if (establishData.beneficiaryAccount.iban) query += `&beneficiaryAccount.iban=${establishData.beneficiaryAccount.iban}`; |
| 89 | } |
| 90 | |
| 91 | if (establishData.transactionId) query += `&transactionId=${establishData.transactionId}`; |
| 92 | |
| 93 | const requestSignature = Crypto.createHmac('sha1', accessKey).update(query).digest('base64'); |
| 94 | return requestSignature; |
| 95 | } |