Skip to content
This repository has been archived by the owner on Oct 4, 2019. It is now read-only.

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
alessiodionisi committed Jun 28, 2018
1 parent c54ed83 commit b536067
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 28 deletions.
53 changes: 47 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,66 @@
[![Packagist Version](https://img.shields.io/packagist/v/satispay/online-api-php-sdk.svg?style=flat-square)](https://packagist.org/packages/satispay/online-api-php-sdk)
[![Packagist Downloads](https://img.shields.io/packagist/dt/satispay/online-api-php-sdk.svg?style=flat-square)](https://packagist.org/packages/satispay/online-api-php-sdk)

## Composer Installation
## Installation
Run the following command:

```bash
composer require satispay/online-api-php-sdk
```

## Manual Installation
If you do not wish to use Composer, `require_once` the `init.php` file.
If you do not wish to use Composer, import the `init.php` file.

```php
require_once('/path/init.php');
```

## Getting Started
## Documentation
https://s3-eu-west-1.amazonaws.com/docs.online.satispay.com/index.html

## Authentication
Sign in to your [Dashboard](https://business.satispay.com) at [business.satispay.com](https://business.satispay.com), click "Negozi Online" in the menu on the left, and then click on "Genera un token di attivazione" to generate an activation token.

Use the activation token with the `authenticateWithToken` function to generate and exchange a pair of RSA keys.

Save the keys in your database or in a **safe place** not accesibile from your website.

```php
$api = new \SatispayOnline\Api();

// Authenticate and generate the keys
$api->authenticateWithToken("XXXXXX");

// Export the keys
$publicKey = $api->getPublicKey();
$privateKey = $api->getPrivateKey();
$keyId = $api->getKeyId();
$serverPublicKey = $api->getServerPublicKey();
```

## API Documentation
https://s3-eu-west-1.amazonaws.com/docs.online.satispay.com/index.html
To reuse the keys after authentication, pass them as an argument in the `\SatispayOnline\Api` constructor.

```php
$api = new \SatispayOnline\Api();

// Keys
$publicKey = "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhk...";
$privateKey = "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBg...";
$keyId = "ldg9sbq283og7ua1abpj989kbbm2g60us6f18c1sciq...";
$serverPublicKey = "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhk...";

// Pass keys to Api constructor
$api = new \SatispayOnline\Api([
"publicKey" => $publicKey,
"privateKey" => $privateKey,
"keyId" => $keyId,
"serverPublicKey" => $serverPublicKey
]);

// Test the authentication
try {
$api->testAuthentication();
} catch(\Exception $ex) {
echo $ex->message;
exit;
}
```
1 change: 1 addition & 0 deletions init.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
require_once(dirname(__FILE__) . "/lib/Api.php");

require_once(dirname(__FILE__) . "/lib/Amounts.php");
require_once(dirname(__FILE__) . "/lib/Charges.php");
require_once(dirname(__FILE__) . "/lib/Checkouts.php");
require_once(dirname(__FILE__) . "/lib/Refunds.php");
Expand Down
27 changes: 27 additions & 0 deletions lib/Amounts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace SatispayOnline;

class Amounts {
private $api;

/**
* Amounts constructor
* @param Api $api Api
*/
public function __construct($api) {
$this->api = $api;
}

/**
* Get amounts
* @param array $options Options
*/
public function get($options = []) {
$queryString = "";
if (!empty($options)) {
$queryString .= "?";
$queryString .= http_build_query($options);
}
return $this->api->request->get("/online/v1/amounts$queryString");
}
}
15 changes: 10 additions & 5 deletions lib/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Api {
private $keyId;
private $version = "2.0.0";

public $amounts;
public $charges;
public $checkouts;
public $refunds;
Expand Down Expand Up @@ -40,6 +41,7 @@ public function __construct($options = []) {
$this->keyId = $options["keyId"];
}

$this->amounts = new Amounts($this);
$this->charges = new Charges($this);
$this->checkouts = new Checkouts($this);
$this->refunds = new Refunds($this);
Expand All @@ -66,7 +68,8 @@ public function authenticateWithToken($token) {
"body" => [
"public_key" => $generatedPublicKey,
"token" => $token
]
],
"sign" => false
]);

$this->privateKey = $generatedPrivateKey;
Expand All @@ -77,15 +80,17 @@ public function authenticateWithToken($token) {

/**
* Test authentication keys
* @return object Test authentication response
*/
public function testAuthentication() {
return $this->request->post("/wally-services/protocol/tests", [
$result = $this->request->post("/wally-services/protocol/tests", [
"body" => [
"hello" => "world"
],
"sign" => true
]
]);

if ($result->authentication_key->role !== "ONLINE_SHOP") {
throw new \Exception("Invalid authentication");
}
}

/**
Expand Down
29 changes: 25 additions & 4 deletions lib/Charges.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public function __construct($api) {
*/
public function create($body) {
return $this->api->request->post("/online/v1/charges", [
"body" => $body,
"sign" => true
"body" => $body
]);
}

Expand All @@ -28,8 +27,30 @@ public function create($body) {
* @param string $id Charge id
*/
public function get($id) {
return $this->api->request->get("/online/v1/charges/$id", [
"sign" => true
return $this->api->request->get("/online/v1/charges/$id");
}

/**
* Get charges list
* @param array $options Options
*/
public function all($options = []) {
$queryString = "";
if (!empty($options)) {
$queryString .= "?";
$queryString .= http_build_query($options);
}
return $this->api->request->get("/online/v1/charges$queryString");
}

/**
* Update charge
* @param string $id Charge id
* @param array $body Charge body
*/
public function update($id, $body) {
return $this->api->request->put("/online/v1/charges", [
"body" => $body
]);
}
}
3 changes: 1 addition & 2 deletions lib/Checkouts.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public function __construct($api) {
*/
public function create($body) {
return $this->api->request->post("/online/v1/checkouts", [
"body" => $body,
"sign" => true
"body" => $body
]);
}
}
29 changes: 25 additions & 4 deletions lib/Refunds.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public function __construct($api) {
*/
public function create($body) {
return $this->api->request->post("/online/v1/refunds", [
"body" => $body,
"sign" => true
"body" => $body
]);
}

Expand All @@ -28,8 +27,30 @@ public function create($body) {
* @param string $id Refund id
*/
public function get($id) {
return $this->api->request->get("/online/v1/refunds/$id", [
"sign" => true
return $this->api->request->get("/online/v1/refunds/$id");
}

/**
* Get refunds list
* @param array $options Options
*/
public function all($options = []) {
$queryString = "";
if (!empty($options)) {
$queryString .= "?";
$queryString .= http_build_query($options);
}
return $this->api->request->get("/online/v1/refunds$queryString");
}

/**
* Update refund
* @param string $id Refund id
* @param array $body Refund body
*/
public function update($id, $body) {
return $this->api->request->put("/online/v1/refunds", [
"body" => $body
]);
}
}
47 changes: 45 additions & 2 deletions lib/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,44 @@ public function post($path, $options = []) {
return $this->request($requestOptions);
}

/**
* PUT request
* @param string $path Request path
* @param array $options Request options
*/
public function put($path, $options = []) {
$requestOptions = [
"path" => $path,
"method" => "PUT",
"body" => $options["body"]
];

if (!empty($options["sign"])) {
$requestOptions["sign"] = $options["sign"];
}

return $this->request($requestOptions);
}

/**
* PATCH request
* @param string $path Request path
* @param array $options Request options
*/
public function patch($path, $options = []) {
$requestOptions = [
"path" => $path,
"method" => "PATCH",
"body" => $options["body"]
];

if (!empty($options["sign"])) {
$requestOptions["sign"] = $options["sign"];
}

return $this->request($requestOptions);
}

/**
* Sign request
* @param array $options Sign request options
Expand Down Expand Up @@ -98,7 +136,7 @@ private function signRequest($options = []) {
* Execute request
* @param array $options Request options
*/
private function request($options = []) {
private function request($options = [ ]) {
$body = "";
$headers = [
"Accept: application/json",
Expand All @@ -116,7 +154,12 @@ private function request($options = []) {
array_push($headers, "Content-Length: ".strlen($body));
}

if (!empty($options["sign"]) && $options["sign"] === true) {
$sign = true;
if (!empty($options["sign"])) {
$sign = $options["sign"];
}

if ($sign) {
$signResult = $this->signRequest([
"body" => $body,
"method" => $method,
Expand Down
20 changes: 15 additions & 5 deletions lib/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public function __construct($api) {
*/
public function create($body) {
return $this->api->request->post("/online/v1/users", [
"body" => $body,
"sign" => true
"body" => $body
]);
}

Expand All @@ -28,8 +27,19 @@ public function create($body) {
* @param string $id User id
*/
public function get($id) {
return $this->api->request->get("/online/v1/charges/$id", [
"sign" => true
]);
return $this->api->request->get("/online/v1/users/$id");
}

/**
* Get users list
* @param array $options Options
*/
public function all($options = []) {
$queryString = "";
if (!empty($options)) {
$queryString .= "?";
$queryString .= http_build_query($options);
}
return $this->api->request->get("/online/v1/users$queryString");
}
}

0 comments on commit b536067

Please sign in to comment.