public function handleWebHook($input)
{
$accountGateway = $this->accountGateway;
$accountId = $accountGateway->account_id;
foreach (array_keys($input) as $key) {
if ('_id' == substr($key, -3)) {
$objectType = substr($key, 0, -3);
$objectId = $input[$key];
break;
}
}
if (!isset($objectType)) {
throw new Exception('Could not find object id parameter');
}
if ($objectType == 'credit_card') {
$paymentMethod = PaymentMethod::scope(false, $accountId)->where('source_reference', '=', $objectId)->first();
if (!$paymentMethod) {
throw new Exception('Unknown payment method');
}
$wepay = Utils::setupWePay($accountGateway);
$source = $wepay->request('credit_card', ['client_id' => WEPAY_CLIENT_ID, 'client_secret' => WEPAY_CLIENT_SECRET, 'credit_card_id' => intval($objectId)]);
if ($source->state == 'deleted') {
$paymentMethod->delete();
} else {
//$this->paymentService->convertPaymentMethodFromWePay($source, null, $paymentMethod)->save();
}
return 'Processed successfully';
} elseif ($objectType == 'account') {
$config = $accountGateway->getConfig();
if ($config->accountId != $objectId) {
throw new Exception('Unknown account');
}
$wepay = Utils::setupWePay($accountGateway);
$wepayAccount = $wepay->request('account', ['account_id' => intval($objectId)]);
if ($wepayAccount->state == 'deleted') {
$accountGateway->delete();
} else {
$config->state = $wepayAccount->state;
$accountGateway->setConfig($config);
$accountGateway->save();
}
return ['message' => 'Processed successfully'];
} elseif ($objectType == 'checkout') {
$payment = Payment::scope(false, $accountId)->where('transaction_reference', '=', $objectId)->first();
if (!$payment) {
throw new Exception('Unknown payment');
}
$wepay = Utils::setupWePay($accountGateway);
$checkout = $wepay->request('checkout', ['checkout_id' => intval($objectId)]);
if ($checkout->state == 'refunded') {
$payment->recordRefund();
} elseif (!empty($checkout->refund) && !empty($checkout->refund->amount_refunded) && $checkout->refund->amount_refunded - $payment->refunded > 0) {
$payment->recordRefund($checkout->refund->amount_refunded - $payment->refunded);
}
if ($checkout->state == 'captured') {
$payment->markComplete();
} elseif ($checkout->state == 'cancelled') {
$payment->markCancelled();
} elseif ($checkout->state == 'failed') {
$payment->markFailed();
}
return 'Processed successfully';
} else {
return 'Ignoring event';
}
}