Files
exchange-2.0/app/Classes/Modules/Billplzs/DataTransferObjects/BillplzXSignatureObject.php
T

104 lines
2.6 KiB
PHP

<?php
namespace App\Classes\Modules\Billplzs\DataTransferObjects;
use Illuminate\Http\Request;
use App\Classes\General\Interfaces\DataTransferObject;
class BillplzXSignatureObject implements DataTransferObject
{
/** @var string */
private $billPlzId;
/** @var array */
private $billPlzConstructArray;
/** @var string */
private $billPlzConstructString;
/** @var string */
private $billPlzComputedXSignature;
/** @var Request */
private $request;
/** @var string */
private $requestXSignature;
public function __construct(Request $request)
{
$this->request = $request;
$this->billPlzId = $request->id ? $request->id : $request->{'billplz[id]'};
$this->requestXSignature = $request->x_signature ? $request->x_signature : $request->{'billplz[x_signature]'};
$this->_constructBillplzArray()->_natSortBillplzArray()->_constructBillplzString()->_computeBillplzXSignature();
}
private function _constructBillplzArray(){
foreach($this->request->all() as $key => $value){
if($key != 'x_signature' && $key != 'billplz[x_signature]'){
$key = str_replace(']', '', str_replace('[', '', $key));
$this->billPlzConstructArray[] = $key.$value;
}
}
return $this;
}
private function _natCaseSortBillplzArray(){
natcasesort($this->billPlzConstructArray);
return $this;
}
private function _natSortBillplzArray(){
natsort($this->billPlzConstructArray);
return $this;
}
private function _constructBillplzString(){
$this->billPlzConstructString = implode('|', $this->billPlzConstructArray);
return $this;
}
private function _computeBillplzXSignature(){
$this->billPlzComputedXSignature = hash_hmac('sha256', $this->billPlzConstructString, config('billplz.x_signature_key'));
return $this;
}
public function getBillPlzId(): string
{
return $this->billPlzId;
}
/**
* @return array
*/
public function getBillPlzConstructArray(): array
{
return $this->billPlzConstructArray;
}
/**
* @return string
*/
public function getBillPlzConstructString(): string
{
return $this->billPlzConstructString;
}
/**
* @return string
*/
public function getBillPlzComputedXSignature(): string
{
return $this->billPlzComputedXSignature;
}
public function isValidSignature(): bool
{
return $this->billPlzComputedXSignature == $this->requestXSignature ? true : false;
}
}