Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcmoore committed Feb 12, 2018
1 parent 58c8de4 commit 3e90905
Show file tree
Hide file tree
Showing 18 changed files with 446 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tests/boot.php
vendor/*
composer\.lock
34 changes: 34 additions & 0 deletions ComodoCACert.cer
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-----BEGIN CERTIFICATE-----
MIIF2DCCA8CgAwIBAgIQTKr5yttjb+Af907YWwOGnTANBgkqhkiG9w0BAQwFADCB
hTELMAkGA1UEBhMCR0IxGzAZBgNVBAgTEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4G
A1UEBxMHU2FsZm9yZDEaMBgGA1UEChMRQ09NT0RPIENBIExpbWl0ZWQxKzApBgNV
BAMTIkNPTU9ETyBSU0EgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMTAwMTE5
MDAwMDAwWhcNMzgwMTE4MjM1OTU5WjCBhTELMAkGA1UEBhMCR0IxGzAZBgNVBAgT
EkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UEBxMHU2FsZm9yZDEaMBgGA1UEChMR
Q09NT0RPIENBIExpbWl0ZWQxKzApBgNVBAMTIkNPTU9ETyBSU0EgQ2VydGlmaWNh
dGlvbiBBdXRob3JpdHkwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIKAoICAQCR
6FSS0gpWsawNJN3Fz0RndJkrN6N9I3AAcbxT38T6KhKPS38QVr2fcHK3YX/JSw8X
pz3jsARh7v8Rl8f0hj4K+j5c+ZPmNHrZFGvnnLOFoIJ6dq9xkNfs/Q36nGz637CC
9BR++b7Epi9Pf5l/tfxnQ3K9DADWietrLNPtj5gcFKt+5eNu/Nio5JIk2kNrYrhV
/erBvGy2i/MOjZrkm2xpmfh4SDBF1a3hDTxFYPwyllEnvGfDyi62a+pGx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aZw4gSzQbzpgJHqZJx64SIDqZxubw5lT2yHh17zbqD5daWb
QOhTsiedSrnAdyGN/4fy3ryM7xfft0kL0fJuMAsaDk527RH89elWsn2/x20Kk4yl
0MC2Hb46TpSi125sC8KKfPog88Tk5c0NqMuRkrF8hey1FGlmDoLnzc7ILaZRfyHB
NVOFBkpdn627G190
-----END CERTIFICATE-----
27 changes: 27 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "adamcmoore/insurance-hub",
"description": "API wrapper for Insurance Hub, based on https://github.com/Insure-Tech/InsuranceHub.Client.Php",
"homepage": "https://smccoachhire.com",
"authors": [
{
"name": "Adam Moore",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.3.0"
},
"require-dev": {
"phpunit/phpunit": "^5"
},
"autoload": {
"psr-4": {
"InsuranceHub\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
}
}
16 changes: 16 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="tests/boot.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Offering">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
</phpunit>
58 changes: 58 additions & 0 deletions src/ApiClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
namespace InsuranceHub;

use InsuranceHub\Exception\ApiException;
use InsuranceHub\Exception\ApiAuthenticationException;
use InsuranceHub\Exception\ApiValidationException;

class ApiClient
{
public $url;
public $certificatePath;

public function __construct($serviceURL, $caCertificatePath) {
$this->url = $serviceURL;
$this->certificatePath = $caCertificatePath;
}

public function execute($request)
{
$authToken = new AuthToken($request->vendorId, $request->apiKey);

$ch = curl_init($this->url);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, $this->certificatePath);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'X-InsuranceHub-VendorId: '.$request->vendorId,
'X-InsuranceHub-AuthToken: '.$authToken->getToken()]);

$response = curl_exec($ch);

$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

switch ($httpCode) {
case 401:
throw new ApiAuthenticationException();
case 403:
throw new ApiAuthorisationException();
case 400:
$validationError = json_decode($response);

$validationMessages = implode(",", $validationError->validationMessages);

throw new ApiValidationException($validationMessages);
case 500:
$apiError = json_decode($response);
throw new ApiException($apiError->message);
}

curl_close($ch);

return array($httpCode, json_decode($response));
}
}
21 changes: 21 additions & 0 deletions src/AuthToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace InsuranceHub;

class AuthToken
{
protected $formattedVendorId;
protected $formattedApiKey;

public function __construct($vendorId, $apiKey)
{
$this->formattedVendorId = str_replace('-', '', strtolower($vendorId));
$this->formattedApiKey = str_replace('-', '', strtolower($apiKey));
}

public function getToken()
{
$date = gmdate('dmY');

return base64_encode(hash_hmac('sha1', $this->formattedVendorId.$date, $this->formattedApiKey, true));
}
}
10 changes: 10 additions & 0 deletions src/Exception/ApiAuthenticationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace InsuranceHub\Exception;

class ApiAuthenticationException extends Exception
{
public function errorMessage()
{
return "Unauthorised request. Check your Vendor ID and Api Key";
}
}
10 changes: 10 additions & 0 deletions src/Exception/ApiAuthorisationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace InsuranceHub\Exception;

class ApiAuthorisationException extends Exception
{
public function errorMessage()
{
return "You do not have access to this service. Please contact support if you think this is in error.";
}
}
11 changes: 11 additions & 0 deletions src/Exception/ApiException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
namespace InsuranceHub\Exception;

class ApiException extends Exception
{
public function errorMessage()
{
$error = "InsureHub API Exception : ".$this->getMessage();
return $error;
}
}
9 changes: 9 additions & 0 deletions src/Exception/ApiValidationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace InsuranceHub\Exception;

class ValidationException extends Exception
{
public function errorMessage() {
return "Invalid Request : ".$this->getMessage();
}
}
7 changes: 7 additions & 0 deletions src/Exception/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace InsuranceHub\Exception;

abstract class Exception extends \Exception
{
abstract protected function errorMessage();
}
86 changes: 86 additions & 0 deletions src/InsuranceHub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
namespace InsuranceHub;

use InsuranceHub\Exception\ApiException;
use InsuranceHub\Resource\Request;
use InsuranceHub\Resource\Result;
use InsuranceHub\Resource\Product;
use InsuranceHub\Resource\Sale;

class InsuranceHub
{
protected $vendorId;
protected $apiKey;
protected $offeringRequestURL;
protected $offeringResultURL;


public function __construct($vendorId, $apiKey, $offeringRequestURL, $offeringResultURL)
{
$this->vendorId = $vendorId;
$this->apiKey = $apiKey;
$this->offeringRequestURL = $offeringRequestURL;
$this->offeringResultURL = $offeringResultURL;
$this->certPath = getcwd()."/ComodoCACert.cer";;
}


public function getOffering($products, $reference = null)
{
$client = new ApiClient($this->offeringRequestURL, $this->certPath);
$request = new Request();
$request->vendorId = $this->vendorId;
$request->apiKey = $this->apiKey;
$request->products = $products;

if ($reference) {
$request->vendorRequestReference = $reference;
}

$offering = $client->execute($request)[1];

return $offering;
}


public function sendSaleResult($offering, $reference, $customerFirstName, $customerLastName)
{
return $this->sendResult($offering, $reference, $customerFirstName, $customerLastName, true);
}


public function sendNoSaleResult($offering, $reference, $customerFirstName, $customerLastName)
{
return $this->sendResult($offering, $reference, $customerFirstName, $customerLastName, false);
}


private function sendResult($offering, $reference, $customerFirstName, $customerLastName, $isSale)
{
$client = new ApiClient($this->offeringResultURL, $this->certPath);
$result = new Result();
$result->vendorId = $this->vendorId;
$result->apiKey = $this->apiKey;
$result->offeringId = $offering->id;
$result->vendorSaleReference = $reference;
$result->customerForename = $customerFirstName;
$result->customerSurname = $customerLastName;

foreach ($offering->productOfferings as $i => $product) {
$sale = new Sale();
$sale->productOfferingId = $product->id;
$sale->sold = $isSale;

$result->sales[$i] = $sale;
}

$responseCode = $client->execute($result)[0];

// check success reponse code recieved (2xx)
if ($responseCode >= 200 && $responseCode < 300) {
return true;
} else {
throw new ApiException('Sending offering Result returned HTTP status code '.$responseCode);
}
}
}
17 changes: 17 additions & 0 deletions src/Resource/Product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace InsuranceHub\Resource;

class Product implements \JsonSerializable
{
public $categoryCode;
public $price;
public $completionDate;

public function jsonSerialize() {
return [
'categoryCode' => $this->categoryCode,
'price' => $this->price,
'completionDate' => $this->completionDate->format(\DateTime::ISO8601),
];
}
}
20 changes: 20 additions & 0 deletions src/Resource/Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace InsuranceHub\Resource;

class Request implements \JsonSerializable
{
public $vendorId;
public $apiKey;
public $vendorRequestReference;
public $products;
public $premiumAsSummary;

public function jsonSerialize() {
return [
'vendorId' => $this->vendorId,
'vendorRequestReference' => $this->vendorRequestReference,
'products' => $this->products,
'premiumAsSummary' => $this->premiumAsSummary,
];
}
}
24 changes: 24 additions & 0 deletions src/Resource/Result.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace InsuranceHub\Resource;

class Result implements \JsonSerializable
{
public $vendorId;
public $apiKey;
public $offeringId;
public $vendorSaleReference;
public $customerForename;
public $customerSurname;
public $sales = [];

public function jsonSerialize() {
return [
'vendorId' => $this->vendorId,
'offeringId' => $this->offeringId,
'vendorSaleReference' => $this->vendorSaleReference,
'customerForename' => $this->customerForename,
'customerSurname' => $this->customerSurname,
'sales' => $this->sales,
];
}
}
15 changes: 15 additions & 0 deletions src/Resource/Sale.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace InsuranceHub\Resource;

class Sale implements \JsonSerializable
{
public $productOfferingId;
public $sold;

public function jsonSerialize() {
return [
'productOfferingId' => $this->productOfferingId,
'sold' => $this->sold,
];
}
}
Loading

0 comments on commit 3e90905

Please sign in to comment.