| 1 |
<?php |
| 2 |
|
| 3 |
namespace Imoje\Payment; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use JsonSchema\Validator; |
| 7 |
|
| 8 |
/** |
| 9 |
* Class Validate |
| 10 |
* |
| 11 |
* @package Imoje\Payment |
| 12 |
*/ |
| 13 |
class Validate |
| 14 |
{ |
| 15 |
|
| 16 |
/** |
| 17 |
* @param string $data |
| 18 |
* |
| 19 |
* @return bool |
| 20 |
* @throws Exception |
| 21 |
*/ |
| 22 |
public static function notification($data) |
| 23 |
{ |
| 24 |
|
| 25 |
$schema = [ |
| 26 |
'type' => 'object', |
| 27 |
'properties' => [ |
| 28 |
|
| 29 |
'transaction' => [ |
| 30 |
'type' => 'object', |
| 31 |
'properties' => [ |
| 32 |
|
| 33 |
'amount' => [ |
| 34 |
'type' => 'integer', |
| 35 |
'minimum' => 0, |
| 36 |
'exclusiveMinimum' => true, |
| 37 |
], |
| 38 |
'currency' => [ |
| 39 |
'type' => 'string', |
| 40 |
'enum' => array_values(Util::getSupportedCurrencies()), |
| 41 |
], |
| 42 |
'status' => [ |
| 43 |
'type' => 'string', |
| 44 |
'enum' => array_values(Util::getTransactionStatuses()), |
| 45 |
], |
| 46 |
'orderId' => [ |
| 47 |
'type' => 'string', |
| 48 |
], |
| 49 |
|
| 50 |
'serviceId' => [ |
| 51 |
'type' => 'string', |
| 52 |
], |
| 53 |
'type' => [ |
| 54 |
'type' => 'string', |
| 55 |
'enum' => [ |
| 56 |
'sale', |
| 57 |
'refund', |
| 58 |
], |
| 59 |
], |
| 60 |
], |
| 61 |
'required' => [ |
| 62 |
'amount', |
| 63 |
'currency', |
| 64 |
'status', |
| 65 |
'orderId', |
| 66 |
'serviceId', |
| 67 |
'type', |
| 68 |
], |
| 69 |
], |
| 70 |
|
| 71 |
], |
| 72 |
'required' => [ |
| 73 |
'transaction', |
| 74 |
], |
| 75 |
|
| 76 |
]; |
| 77 |
|
| 78 |
return self::validate($data, $schema, 'notification'); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* @param string $data |
| 83 |
* @param array $schema |
| 84 |
* @param string $schemaType |
| 85 |
* |
| 86 |
* @return bool |
| 87 |
* @throws Exception |
| 88 |
*/ |
| 89 |
private static function validate($data, $schema, $schemaType) |
| 90 |
{ |
| 91 |
|
| 92 |
$data = json_decode($data); |
| 93 |
|
| 94 |
$validator = new Validator(); |
| 95 |
$validator->validate($data, json_decode(json_encode($schema))); |
| 96 |
|
| 97 |
if($validator->isValid()) { |
| 98 |
return true; |
| 99 |
} |
| 100 |
|
| 101 |
$errors = [ |
| 102 |
'schema' => $schemaType, |
| 103 |
]; |
| 104 |
|
| 105 |
foreach($validator->getErrors() as $error) { |
| 106 |
$errors[$error['property']] = $error['message']; |
| 107 |
} |
| 108 |
|
| 109 |
throw new Exception(json_encode($errors)); |
| 110 |
} |
| 111 |
} |
| 112 |
|