UNPKG

magpie-js-sdk

Version:
195 lines (158 loc) 4.24 kB
## Description A NodeJS based wrapper that connects to Magpie REST APIs. ## Usage ### Magpie To use the sdk, we need to require the Magpie SDK class and create an instance of it. ```javascript const Magpie = require('magpie-js-sdk'); const isSandbox = false; const magpie = new Magpie('pk_mypublickey', 'sk_mysecretkey', isSandbox, 'v1.1'); // the `magpie` object contains (3) properties // - magpie.token // - magpie.charge // - magpie.customer // each property contains method that is a mapping to Magpie's REST endpoint. ``` ### Token * Create token using `magpie.token.create` ```javascript magpie.token.create('John Doe', '4242424242424242', '02', '2022', '123') .then(response => { // response contains object in this shape, { statusCode, body } // create token success returns `201` status code if (response.statusCode !== 201) { // handle error in response here } // handle success scenario }) ``` * Get token info using `magpie.token.get` ```javascript magpie.token.get('tok_123asdfa034jasdf') .then(response => { // response contains object in this shape, { statusCode, body } if (response.statusCode !== 200) { // handle error in response here } // handle success scenario here }) ``` ### Customer * Create customer: `magpie.customer.create` ```javascript const email = 'john.doe@gmail.com'; const description = 'Developer'; magpie.customer.create(email, description) .then(response => { if (response.statusCode !== 200) { // handle error scenario here } // handle success scenario here }); ``` * Get customer: `magpie.customer.get` ```javascript magpie.customer.get('cus_1adlkfjas03asdf') .then(response => { if (response.statusCode !== 200) { // handle error scenario here } // handle success scenario here }); ``` * Add/update customer payment source: `magpie.customer.update` ```javascript magpie.customer.update('cus_1adlkfjas03asdf', 'tok_asdfas39234asdf') .then(response => { if (response.statusCode !== 200) { // handle error scenario here } // handle success scenario here }); ``` * Delete customer payment source: `magpie.customer.deleteSource` ```javascript magpie.customer.deleteSource('cus_1adlkfjas03asdf', 'card_asdfas3asdfas3') .then(response => { if (response.statusCode !== 200) { // handle error scenario here } // handle success scenario here }); ``` * Delete customer: `magpie.customer.delete` ```javascript magpie.customer.delete('cus_1adlkfjas03asdf') .then(response => { if (response.statusCode !== 200) { // handle error scenario here } // handle success scenario here }); ``` ### Charge * Create charge: `magpie.charge.create` ```javascript const amount = 5000; const currency = "PHP"; const source = 'tok_asldfjalsdfja98a7sfa'; const description = "Test create charge"; const statementDescriptor = "Test statement"; const capture = true; magpie.charge.create( amount, currency, source, description, statementDescriptor, capture ) .then(response => { // create charge success returns `201` status code if (response.statusCode !== 201) { // handle error scenario here } // handle success scenario here }); ``` * Get charge: `magpie.charge.get` ```javascript magpie.charge.get('ch_awdkfjalsdjf30234a') .then(response => { if (response.statusCode !== 200) { // handle error scenario here } // handle success scenario here }); ``` * Capture charge: `magpie.charge.capture` ```javascript magpie.charge.capture('ch_awdkfjalsdjf30234a', 5000) .then(response => { if (response.statusCode !== 200) { // handle error scenario here } // handle success scenario here }); ``` * Void charge: `magpie.charge.void` ```javascript magpie.charge.void('ch_awdkfjalsdjf30234a') .then(response => { if (response.statusCode !== 200) { // handle error scenario here } // handle success scenario here }); ``` * Refund charge: `magpie.charge.refund` ```javascript magpie.charge.refund('ch_awdkfjalsdjf30234a', 5000) .then(response => { if (response.statusCode !== 200) { // handle error scenario here } // handle success scenario here }); ``` If you have suggestions or comments, kindly email me markronquillo23@gmail.com