Last active
November 6, 2018 13:25
-
-
Save eaponiente/fc53a7bda50b0431ff61043fdead4a1d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function webhook(Request $request) | |
{ | |
app('log')->debug('RECEIVED CALL BACK FROM HYPERPAY'); | |
// get all data passed by foodics | |
$d = json_decode(json_encode($request->all()),true); | |
// log in the logfile | |
app('log')->info(print_r($d,true)); | |
$mailer = new SupportMailer(); | |
$code = $d['payload']['merchantTransactionId']; | |
// get order | |
$order = Order::where('code',$code) | |
->first(); | |
if(!$order || $d['type'] === 'REGISTRATION') { | |
app('log')->info('PAYMENT TYPE WRONG:'.json_encode($d,true)); | |
return $this->response->noContent(); | |
} | |
// transaction failure status codes | |
$failedStatusCodes = preg_match('/^(800\.[17]00|800\.800\.[123])/', $d['payload']['result']['code']); | |
if((bool) $failedStatusCodes) { | |
app('log')->info('HYPERPAY TRANSACTION FAILED:'.json_encode($d,true)); | |
// return ok | |
return response()->json(['ok' => [ | |
'status' => true, | |
'message' => 'Transaction failed' | |
]], 200); | |
} | |
app('log')->info('TRANSACTION WEBHOOK DESCRIPTION:' . $d['payload']['result']['description']); | |
if(starts_with($d['payload']['result']['code'], '800.100')) { | |
return response()->json(['ok' => [ | |
'status' => true, | |
'message' => $d['payload']['result']['description'] | |
]], 200); | |
} | |
switch($d['payload']['result']['code']){ | |
case '000.000.000': | |
app('log')->info('TRANSACTION SUCCEED:'.json_encode($d,true)); | |
// send payment email | |
$mailer->sendPaymentMail($order,$d['payload']); | |
$status = new OrderOrderStatus(); | |
$status->order_id = $order->id; | |
$status->order_status_id = 35; | |
$status->save(); | |
// dispatch order | |
dispatch(new NewOrderJob($order)); | |
// return ok | |
return response()->json(['ok' => [ | |
'status' => true, | |
'message' => 'Transaction succeeded' | |
]], 200); | |
break; | |
case '000.000.100': | |
app('log')->info('SUCCESSFUL REQUEST:'.json_encode($d,true)); | |
// return ok | |
return response()->json(['ok' => [ | |
'status' => true, | |
'message' => 'Successful Request' | |
]], 200); | |
break; | |
case '000.100.110': | |
app('log')->info('Request successfully processed in \'Merchant in Integrator Test Mode\':'.json_encode($d,true)); | |
// return ok | |
return response()->json(['ok' => [ | |
'status' => true, | |
'message' => 'Request successfully processed in \'Merchant in Integrator Test Mode\'' | |
]], 200); | |
break; | |
case '000.100.112': | |
app('log')->info('Request successfully processed in \'Merchant in Integrator Test Mode\':'.json_encode($d,true)); | |
// return ok | |
return response()->json(['ok' => [ | |
'status' => true, | |
'message' => 'Request successfully processed in \'Merchant in Integrator Test Mode\'' | |
]], 200); | |
break; | |
case '000.200.000': | |
app('log')->info('Transaction pending: '.json_encode($d,true)); | |
// return ok | |
return response()->json(['ok' => [ | |
'status' => true, | |
'message' => 'Transaction pending' | |
]], 200); | |
break; | |
case '000.200.100': | |
app('log')->info('successfully created checkout:'.json_encode($d,true)); | |
// return ok | |
return response()->json(['ok' => [ | |
'status' => true, | |
'message' => 'Successfully created checkout' | |
]], 200); | |
break; | |
} | |
$order->payments()->save(new Payment([ | |
'method' => 'card', | |
'amount' => ((int)$d['payload']['amount']), | |
'payment_reference_number' => $d['payload']['id'], | |
'status' => 'success' | |
])); | |
return $this->response->noContent(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment