|
| 1 | +const axios = require('axios').default |
| 2 | +const crypto = require('crypto') |
| 3 | +const Helper = require('./utils/Helper') |
| 4 | + |
| 5 | +/** |
| 6 | + * @class ThePeer |
| 7 | + */ |
| 8 | +class ThePeer { |
| 9 | + /** |
| 10 | + *This is a constructor for creating a Peer Instance |
| 11 | + * @param {string} secretkey - Thepeer secret key |
| 12 | + * @returns { ThePeer } - An instance of thePeer |
| 13 | + */ |
| 14 | + constructor (secretkey) { |
| 15 | + this.secretKey = secretkey |
| 16 | + this.request = axios.create({ |
| 17 | + baseURL: 'https://api.thepeer.co', |
| 18 | + headers: { |
| 19 | + 'x-api-key': secretkey, |
| 20 | + 'Content-Type': 'application/json' |
| 21 | + } |
| 22 | + }) |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * @static |
| 27 | + * @param {object} payload - The payload to be verified. |
| 28 | + * @param {string} signature - The signature to compare with |
| 29 | + * @returns { Boolean } - True if same signature otherwise false |
| 30 | + * @memberof ThePeer |
| 31 | + */ |
| 32 | + validateSignature (payload, signature) { |
| 33 | + return signature === crypto.createHmac('sha1', this.secretKey).update(payload).digest('hex') |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @param {string} name - The name of the user |
| 38 | + * @param {string} identifier - The identifier of the user |
| 39 | + * @param {string} email - The email of the user |
| 40 | + * @returns {JSON} A JSON response containing the details of the user |
| 41 | + * @memberof ThePeer |
| 42 | + */ |
| 43 | + async indexUser (name, identifier, email) { |
| 44 | + try { |
| 45 | + const response = await this.request.post('/users', { |
| 46 | + name: name, |
| 47 | + identifier: identifier, |
| 48 | + email: email |
| 49 | + }) |
| 50 | + |
| 51 | + return response.data |
| 52 | + } catch (e) { |
| 53 | + Helper.processError(e) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @param {string} reference - The reference returned when the user was indexed |
| 59 | + * @param {string} identifier - The identifier of the user |
| 60 | + * @returns {JSON} A JSON response containing the details of the user |
| 61 | + * @memberof ThePeer |
| 62 | + */ |
| 63 | + async updateUser (reference, identifier) { |
| 64 | + try { |
| 65 | + const response = await this.request.put(`/users/${reference}`, { |
| 66 | + identifier: identifier |
| 67 | + }) |
| 68 | + |
| 69 | + return response.data |
| 70 | + } catch (e) { |
| 71 | + Helper.processError(e) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param {string} reference - The reference returned when the user was indexed |
| 77 | + * @returns {JSON} A JSON response containing the details of the user |
| 78 | + * @memberof ThePeer |
| 79 | + */ |
| 80 | + async deleteUser (reference) { |
| 81 | + try { |
| 82 | + const response = await this.request.delete(`/users/${reference}`) |
| 83 | + |
| 84 | + return response.data |
| 85 | + } catch (e) { |
| 86 | + Helper.processError(e) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @param {string} reference - The reference returned when the user was indexed |
| 92 | + * @returns {JSON} A JSON response containing the details of the user |
| 93 | + * @memberof ThePeer |
| 94 | + */ |
| 95 | + async getUser (reference) { |
| 96 | + try { |
| 97 | + const response = await this.request.get(`/users/${reference}`) |
| 98 | + |
| 99 | + return response.data |
| 100 | + } catch (e) { |
| 101 | + Helper.processError(e) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * |
| 107 | + * @param {string} linkid - The id of a user linked account |
| 108 | + * @returns {JSON} A JSON response containing the details of the user |
| 109 | + * @memberof ThePeer |
| 110 | + */ |
| 111 | + async getLink (linkid) { |
| 112 | + try { |
| 113 | + const response = await this.request.get(`/link/${linkid}`) |
| 114 | + |
| 115 | + return response.data |
| 116 | + } catch (e) { |
| 117 | + Helper.processError(e) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * A function that charges your user's linked account at any time |
| 123 | + * @param {string} linkid - The id of the link |
| 124 | + * @param {integer} amount - amount in kobo |
| 125 | + * @param {string} remark - The reason for initiating a direct charge |
| 126 | + * @returns {JSON} A JSON response containing the details of the user |
| 127 | + * @memberof ThePeer |
| 128 | + */ |
| 129 | + async chargeLink (linkid, amount, remark) { |
| 130 | + try { |
| 131 | + const response = await this.request.post(`/link/${linkid}/charge`, { |
| 132 | + amount: amount, |
| 133 | + remark: remark |
| 134 | + }) |
| 135 | + |
| 136 | + return response.data |
| 137 | + } catch (e) { |
| 138 | + Helper.processError(e) |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Authorize direct charge request via webhooks |
| 144 | + * @param {*} reference - The direct charge reference sent via webhook |
| 145 | + * @param {boolean} insufficientFunds - Status of user funds |
| 146 | + * @returns {JSON} A JSON response containing the details of the user |
| 147 | + * @memberof ThePeer |
| 148 | + */ |
| 149 | + async authorizeDirectCharge (reference, insufficientFunds) { |
| 150 | + try { |
| 151 | + const response = await this.request.post(`/debit/${reference}`, { |
| 152 | + insufficient_funds: insufficientFunds |
| 153 | + }) |
| 154 | + |
| 155 | + return response.data |
| 156 | + } catch (e) { |
| 157 | + Helper.processError(e) |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * @memberof ThePeer |
| 163 | + * @param {string} receipt - The reference returned to your receiptURL via the Chain SDK |
| 164 | + * @param {boolean} insufficientFunds - Status of user funds |
| 165 | + * @returns {JSON} A JSON response containing the details of the user |
| 166 | + */ |
| 167 | + async processSendReceipt (receipt, insufficientFunds) { |
| 168 | + try { |
| 169 | + const response = await this.request.post(`/send/${receipt}`, { |
| 170 | + insufficient_funds: insufficientFunds |
| 171 | + }) |
| 172 | + |
| 173 | + return response.data |
| 174 | + } catch (e) { |
| 175 | + Helper.processError(e) |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * @memberof ThePeer |
| 181 | + * @param {string} receipt - The reference returned to your receiptURL via the Chain SDK |
| 182 | + * @returns {JSON} A JSON response containing the details of the user |
| 183 | + */ |
| 184 | + async getSendReceipt (receipt) { |
| 185 | + try { |
| 186 | + const response = await this.request.get(`/send/${receipt}`) |
| 187 | + |
| 188 | + return response.data |
| 189 | + } catch (e) { |
| 190 | + Helper.processError(e) |
| 191 | + } |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +module.exports = ThePeer |
0 commit comments