Skip to content

Commit

Permalink
Release of PHP SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
AnonWalletNet committed Jul 18, 2022
0 parents commit 29ae367
Show file tree
Hide file tree
Showing 5 changed files with 313 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 AnonWalletNet

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
80 changes: 80 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# anonwallet-php

Official PHP SDK of AnonWallet.Net Anonymous Payment Gateway

## Install guide
You can install anonwallet plugin using composer in your project using:

```
composer require anonwallet/anonwallet-php
```

## Usage
Include namespace of package wherever you want to use this library

```
include_once './vendor/autoload.php';
use AnonWallet\AnonWallet;
$api_key = 'Your merchant API Key';
$obj = new AnonWallet($api_key);
```

## Get Balance of specific coin

```
$currency = 'BTC'; //If currency parameter is not specified, default currency is BTC
$balance = $obj->balance($currency);
```

## Get balances of all coins

```
$balances = $obj->balances();
```

## Generate Callback Address

```
$currency = 'BTC'; //If currency parameter is not specified, default currency is BTC
$forward_address = '1Btcdemoforwardaddress'; // If forward address is specified, the funds are automatically send to this address after the transaction is confirmed
$ipn_url = 'Yourdomain.com/handle_payment'; // If IPN url is specified, notifications of payment status changes is sent to this domain
$label = 'John Doe'; //Specify label for your internal system tracking
$address = $obj->callback_address($currency, $forward_address, $ipn_url, $label);
```

## Create new Invoice

```
$currency = 'BTC'; //If currency parameter is not specified, default currency is BTC
$amount = '0.01'; //Numeric double amount required to be received on the invoice
$forward_address = '1Btcdemoforwardaddress'; // If forward address is specified, the funds are automatically send to this address after the transaction is confirmed
$ipn_url = 'Yourdomain.com/handle_payment'; //If IPN url is specified, notifications of payment status changes is sent to this domain
$invoice_id = '112'; // Your internal system tracking invoice id
$hosted_invoice = true; // true or false, if boolean true is specified, the invoice will have a hosted payment page on AnonWallet gateway
$product_title = 'My product title';
$product_description = 'My product description';
$success_url = 'Yourdomain.com/success'; //URL where the buyer will redirect when it cancel the hosted invoice
$cancel_url = 'Yourdomain.com/cancel'; //URL where the buyer will redirect when it cancel the hosted invoice
$buyer_email = 'buyer@email.com' //Specify the buyer email
$invoice = $obj->create_invoice($currency, $amount, $forward_address, $ipn_url, $invoice_id, $hosted_invoice, $product_title, $product_description, $success_url, $cancel_url, $buyer_email);
```

## Create new Withdrawal request

```
$currency = 'BTC'; //If currency parameter is not specified, default currency is BTC
$amount = '0.01'; // Numeric double amount to be send from your account
$address = '1Btcdemowithdrawaladdress'; //The receiver address
$ipn_url = 'Yourdomain.com/handle_withdraw'; // URL where
$withdraw = $obj->create_withdrawal($currency, $amount, $address, $ipn_url);
```

**You can find more references on our API Documentation (https://anonwallet.readme.io)**
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "anonwallet/anonwallet-php",
"description": "Official PHP SDK for AnonWallet anonymous cryptocurrency payment gateway",
"type": "library",
"keywords": ["anonwallet", "php", "anonuymous", "cryptocurrency", "payment gateway", "api"],
"homepage": "https://anonwallet.net/",
"license": "MIT",
"autoload": {
"psr-4": {
"AnonWallet": "src/"
}
},
"authors": [
{
"name": "AnonWallet",
"email": "support@anonwallet.net"
}
],
"require": {
"php": ">=5.3.0"
},
"minimum-stability": "dev",
"prefer-stable": true
}
187 changes: 187 additions & 0 deletions src/AnonWallet/AnonWallet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<?php
namespace AnonWallet;

class AnonWallet {

/**
*
* @var string endpoint of api
*/
private $url = 'https://anonwallet.net/api/';
/**
*
* @var string api of version
*/
private $version = 'v1';
/**
*
* @var string currency, BTC is default
*/
private $default_currency = 'BTC';
/**
*
* @var string api key from anonwallet payment gateway
*/
private $api_key;

public function __construct($api_key) {

if(isset($api_key)) {
$this->api_key = $api_key;
} else {
throw new \Exception('Api key is required');
}

if(!function_exists(curl_version)) {
throw new \Exception('php-curl is not enabled. You need to install it to use this plugin!');
}

$this->currency = $default_currency;
}

/**
*
* Get balance of specified coin from your wallet
* Example string BTC, BCH, LTC, DOGE
* @return json response with success or error message
*/
public function balance($currency = '') {
$url = $this->url.$this->version.'/balance';

$payload = [
'currency'=>(isset($currency)) ? $currency : $this->currency
];

$res = $this->curl_call($url, $payload);
return $res;
}

/**
*
* Get balances of all coins from your wallet
* @return json response with success or error message
*/
public function balances() {
$url = $this->url.$this->version.'/balances';
$res = $this->curl_call($url);
return $res;
}

/**
*
* Generate new callback address of specific coin, default is BTC
* @return json response with success or error message
*/
public function callback_address($currency, $forward_address = '', $ipn_url = '', $label = '') {
$url = $this->url.$this->version.'/callback_address';

$payload = [
'currency'=>(isset($currency)) ? $currency : $this->currency,
'forward_address' => $forward_address,
'ipn_url' => $ipn_url,
'label' => $label
];

$res = $this->curl_call($url, $payload);
return $res;
}

/**
*
* Generate an invoice with callback address and hosted payment page
* @return json response with success or error message
*/
public function create_invoice($currency, $amount, $forward_address = '', $ipn_url = '', $invoice_id = '', $hosted_invoice = false, $product_title = '', $product_description = '', $cancel_url = '', $success_url = '', $buyer_email = '') {
$url = $this->url.$this->version.'/create_invoice';

if($amount == 0) {
throw new \Exception('The amount cannot be zero or negative number');
}

if(!is_numeric($amount)) {
throw new \Exception('The amount should be numeric double');
}

$payload = [
'amount'=>$amount,
'currency'=>(isset($currency)) ? $currency : $this->currency,
'forward_address'=>$forward_address,
'ipn_url'=>$ipn_url,
'invoice_id'=>$invoice_id,
'hosted_invoice'=>$hosted_invoice,
'product_title'=>$product_title,
'product_description'=>$product_description,
'cancel_url'=>$cancel_url,
'success_url'=>$success_url,
'buyer_email'=>$buyer_email
];

$res = $this->curl_call($url, $payload);
return $res;
}

/**
*
* Create a withdrawal request from your wallet
* @return json response with success or error message
*/
public function create_withdrawal($amount, $currency, $address, $ipn_url = '') {
$url = $this->url.$this->version.'/create_withdrawal';

if($amount == 0) {
throw new \Exception('The amount cannot be zero or negative number');
}

if(!is_numeric($amount)) {
throw new \Exception('The amount should be numeric double');
}

if(!isset($address)) {
throw new \Exception('The receiver address is required');
}

$payload = [
'amount'=>$amount,
'currency'=>(isset($currency)) ? $currency : $this->currency,
'address'=>$address,
'ipn_url'=>$ipn_url
];

$res = $this->call($url, $payload);
return $res;
}

/**
*
* @param string $url
* @param array $post parameters
* @return array()
*/
public function curl_call($url, $payload = '') {

$curl = curl_init();

curl_setopt_array($curl, [
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"Authorization: Bearer ".$this->api_key.""
],
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_URL => "".$url."",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
]);

$response = curl_exec($curl);
$error = curl_error($curl);

curl_close($curl);

if($error) {
return json_decode($error);
} else {
return json_decode($response);
}
}

}

0 comments on commit 29ae367

Please sign in to comment.