<?php namespace MyFatoorah\Library; use Exception; class  MyFatoorahWebhook extends MyFatoorah{public static function processWebhookRequest($secretKey,$logger=__DIR__.'/myfatoorah_webhook.log'){MyFatoorah::$loggerObj=$logger;MyFatoorah::log('MyFatoorah WebHook New Request');if(!$secretKey){$msg='Store needs to be configured.';MyFatoorah::log($msg);throw new Exception($msg);}list($mfVersion,$signature)=self::getMfHeaders();$body=file_get_contents('php://input');MyFatoorah::log('MyFatoorah WebHook Body: '.$body);$request=json_decode($body,true);if(empty($request['Data'])){$msg='Wrong data.';MyFatoorah::log($msg);throw new Exception($msg);}if(self::{"checkSignatureValidation$mfVersion"}($request,$secretKey,$signature)){return $request;}$msg='Validation error.';MyFatoorah::log($msg);throw new Exception($msg);}protected static function getMfHeaders(){$apache=(array) apache_request_headers();$headers=array_change_key_case($apache);if(empty($headers['myfatoorah-signature'])||empty($headers['myfatoorah-webhook-version'])){throw new Exception('Wrong request.');}$mfVersion=strtolower($headers['myfatoorah-webhook-version']);if($mfVersion!='v1'&&$mfVersion!='v2'){throw new Exception('Wrong version.');}return[$mfVersion,$headers['myfatoorah-signature']];}protected static function checkSignatureValidationV1($request,$secretKey,$signature){if(!isset($request['EventType'])||!isset($request['Event'])){throw new Exception('Worng event.');}return MyFatoorah::isSignatureValid($request['Data'],$secretKey,$signature,$request['EventType']);}protected static function checkSignatureValidationV2($request,$secretKey,$signature){if(!isset($request['Event']['Code'])||!isset($request['Event']['Name'])){throw new Exception('Worng event.');}$dataModel=self::getV2DataModel($request['Event']['Code'],$request['Data']);return self::checkSignatureValidation($dataModel,$secretKey,$signature);}private static function getV2DataModel($code,$data){$dataModels=[1=>function()use($data){return['Invoice.Id'=>$data['Invoice']['Id'],'Invoice.Status'=>$data['Invoice']['Status'],'Transaction.Status'=>$data['Transaction']['Status'],'Transaction.PaymentId'=>$data['Transaction']['PaymentId'],'Invoice.ExternalIdentifier'=>$data['Invoice']['ExternalIdentifier'],];},2=>function()use($data){return['Refund.Id'=>$data['Refund']['Id'],'Refund.Status'=>$data['Refund']['Status'],'Amount.ValueInBaseCurrency'=>$data['Amount']['ValueInBaseCurrency'],'ReferencedInvoice.Id'=>$data['ReferencedInvoice']['Id'],];},3=>function()use($data){return['Deposit.Reference'=>$data['Deposit']['Reference'],'Deposit.ValueInBaseCurrency'=>$data['Deposit']['ValueInBaseCurrency'],'Deposit.NumberOfTransactions'=>$data['Deposit']['NumberOfTransactions'],];},4=>function()use($data){return['Supplier.Code'=>$data['Supplier']['Code'],'KycDecision.Status'=>$data['KycDecision']['Status'],];},5=>function()use($data){return['Recurring.Id'=>$data['Recurring']['Id'],'Recurring.Status'=>$data['Recurring']['Status'],'Recurring.InitialInvoiceId'=>$data['Recurring']['InitialInvoiceId'],];}];if(!isset($dataModels[$code])){throw new Exception('Worng event.');}return $dataModels[$code]();}public static function checkforWebHook2ProcessMessage($webhook,$order){if($order['myfatoorah_invoiceId']!=$webhook['Invoice']['Id']){return('Wrong invoice.');}if($order['myfatoorah_status']=='Paid'){return('Order already Paid');}if($order['myfatoorah_paymentId']==$webhook['Transaction']['PaymentId']){return"Transaction already {$webhook['Transaction']['Status']}.";}return false;}public static function mapWebhook2Status($status){$statuses=['SUCCESS'=>'Paid','FAILED'=>'Failed','CANCELED'=>'Expired','PENDING'=>'Pending',];return $statuses[$status]?? null;}}