Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
contitouchtechnologies committed Mar 11, 2024
0 parents commit 9bcbeb0
Show file tree
Hide file tree
Showing 10 changed files with 759 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/node_modules
simple_direct_example.js
simple_redirect_example.js
direct_example.js
redirect_example.js
.env
159 changes: 159 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
# ContiPay JavaScript Client Documentation

## Requirements

1. ContiPay Account

2. ContiPay Secret and Key

## How it Works

### 1. Install latest version

```bash
npm install contipay-js
```

### 2. Import Classes

```javascript
// Import necessary classes
const Contipay = require("contipay-js/src/contipay");
const SimpleDirectMethod = require("contipay-js/src/helpers/simple_direct_method");
const DirectMethod = require("contipay-js/src/helpers/direct_method");
const SimpleRedirectMethod = require("contipay-js/src/helpers/simple_redirect_method");
const RedirectMethod = require("contipay-js/src/helpers/redirect_method");
```

### 3. Process Payment

#### i. Basic Direct Payment Example

```javascript
const contipay = new Contipay("token-here", "secret-here");

const phone = "263782000340";

const payload = new SimpleDirectMethod(merchantCode, webhookUrl)
.setUpProvider("InnBucks", "IB")
.preparePayload(amount, phone);

contipay
.setAppMode("DEV") // LIVE as another option
.setPaymentMethod()
.process(payload)
.then((res) => {
console.log(res);
})
.catch((error) => {
console.error(error);
});
```

#### ii. Basic Redirect Payment Example

```javascript
const contipay = new Contipay("token-here", "secret-here");

const phone = "263782000340";

const payload = new SimpleRedirectMethod(
merchantCode,
webhookUrl,
successUrl,
cancelUrl
).preparePayload(amount, phone);

contipay
.setAppMode("DEV") // LIVE as another option
.setPaymentMethod("redirect")
.process(payload)
.then((res) => {
console.log(res);
})
.catch((error) => {
console.error(error);
});
```

#### iii. Direct Payment Example

```javascript
const contipay = new Contipay("token-here", "secret-here");

const phone = "263782000340";

const payload = new DirectMethod(merchantCode, webhookUrl)
.setUpCustomer("Nigel", "Jaure", phone, "ZW", "[email protected]")
.setUpProvider("Ecocash", "EC")
.setUpAccountDetails(phone, "Nigel Jaure")
.setUpTransaction(amount, "USD")
.preparePayload();

contipay
.setAppMode("DEV")
.setPaymentMethod()
.process(payload)
.then((res) => {
console.log(res);
})
.catch((error) => {
console.error(error);
});
```

#### iv. Redirect Payment Example

```javascript
const contipay = new Contipay("token-here", "secret-here");

const phone = "263782000340";

const payload = new RedirectMethod(
merchantCode,
webhookUrl,
successUrl,
cancelUrl
)
.setUpCustomer("Nigel", "Jaure", phone, "ZW", "[email protected]")
.setUpTransaction(amount, "USD")
.preparePayload();

contipay
.setAppMode("DEV")
.setPaymentMethod("redirect")
.process(payload)
.then((res) => {
console.log(res);
})
.catch((error) => {
console.error(error);
});
```

## Additional Notes

- The `updateURL` method is optional and applicable only if the URL has changed. Use it to update the URLs accordingly. Here's how you can use it:

```javascript
const contipay = new Contipay("token-here", "secret-here");

// Update URLs if necessary
contipay.updateURL("dev-url", "live-url");

// Process payment with the updated URLs
contipay
.setAppMode("DEV") // LIVE as another option
.setPaymentMethod()
.process(payload)
.then((res) => {
console.log(res);
})
.catch((error) => {
console.error(error);
});
```

- Ensure to set the appropriate mode (`DEV` or `LIVE`) using the `setAppMode` method before processing payments.

- The provided examples cover basic scenarios, including direct and redirect payment methods, customer information setup, and transaction details.
106 changes: 106 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "contipay-js",
"version": "1.0.1",
"description": "ContiPay JS Client",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": {
"name": "Nigel Jaure",
"email": "[email protected]"
},
"repository": {
"type": "git",
"url": "https://github.com/njzw/contipay-js-client.git"
},
"license": "MIT",
"dependencies": {
"axios": "^1.6.7"
}
}
73 changes: 73 additions & 0 deletions src/contipay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const axios = require("axios");

class Contipay {
// Constructor to initialize token and secret
constructor(token, secret) {
this.token = token;
this.secret = secret;
this.url = ""; // Base URL for Contipay API
this.paymentMethod = "direct"; // Default payment method
this.acquireUrl = "acquire/payment"; // URL endpoint for payments
this.uatURL = "https://api2-test.contipay.co.zw"; // UAT (Development) URL
this.liveURL = "https://api-v2.contipay.co.zw"; // Live URL
this.client = null; // Axios HTTP client
}

// Method to set Contipay environment mode
setAppMode(mode = "DEV") {
// Set the URL based on the mode
this.url = mode === "DEV" ? this.uatURL : this.liveURL;
// Initialize the HTTP client
this.initHttpClient();
return this;
}

// Method to update URLs from the defaults
updateURL(devURL, liveURL) {
this.uatURL = devURL;
this.liveURL = liveURL;
return this;
}

// Method to set payment method
setPaymentMethod(method = "direct") {
// Set the payment method based on input
this.paymentMethod = method === "direct" ? "POST" : "PUT";
return this;
}

// Method to process payment
async process(payload) {
try {
// Make an HTTP request to Contipay API
const response = await this.client.request({
method: this.paymentMethod, // HTTP method
url: `/${this.acquireUrl}`, // API endpoint
auth: {
username: this.token,
password: this.secret,
},
headers: {
Accept: "application/json",
"Content-type": "application/json",
},
data: payload, // Payload data
});
// Return response data
return response.data;
} catch (error) {
// Return error message if request fails
return { status: "Error", message: error.message };
}
}

// Method to setup HTTP client
initHttpClient() {
// Create Axios instance with base URL
this.client = axios.create({
baseURL: this.url,
});
}
}

module.exports = Contipay;
Loading

0 comments on commit 9bcbeb0

Please sign in to comment.