Handle payment success/failure with WePay

This commit is contained in:
= 2021-06-20 09:57:19 +10:00
parent e63a7f9875
commit 886e006de7
2 changed files with 75 additions and 4 deletions

View File

@ -65,9 +65,9 @@ class SystemLog extends Model
const TYPE_CHECKOUT = 304;
const TYPE_AUTHORIZE = 305;
const TYPE_CUSTOM = 306;
const TYPE_BRAINTREE = 307;
const TYPE_WEPAY = 309;
const TYPE_BRAINTREE = 307;
const TYPE_QUOTA_EXCEEDED = 400;
const TYPE_UPSTREAM_FAILURE = 401;

View File

@ -14,7 +14,12 @@ namespace App\PaymentDrivers\WePay;
use App\Exceptions\PaymentFailed;
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
use App\Jobs\Mail\PaymentFailureMailer;
use App\Jobs\Util\SystemLogger;
use App\Models\GatewayType;
use App\Models\Payment;
use App\Models\PaymentType;
use App\Models\SystemLog;
use App\PaymentDrivers\WePayPaymentDriver;
class CreditCard
@ -131,8 +136,6 @@ class CreditCard
// USD, CAD, and GBP.
nlog($request->all());
// charge the credit card
nlog($this->wepay_payment_driver->wepay);
$response = $this->wepay_payment_driver->wepay->request('checkout/create', array(
'account_id' => $this->wepay_payment_driver->company_gateway->getConfigField('accountId'),
'amount' => $this->wepay_payment_driver->payment_hash->data->amount_with_fee,
@ -147,16 +150,32 @@ class CreditCard
)
));
print_r($response);
/* Merge all data and store in the payment hash*/
$state = [
'server_response' => $response,
'payment_hash' => $request->payment_hash,
];
$state = array_merge($state, $request->all());
$this->wepay_payment_driver->payment_hash->data = array_merge((array) $this->wepay_payment_driver->payment_hash->data, $state);
$this->wepay_payment_driver->payment_hash->save();
if(in_array($response->state, ['authorized', 'captured'])){
//success
nlog("success");
$payment_status = ($response->status == 'authorized') ? Payment::STATUS_COMPLETED : Payment:STATUS_PENDING;
$this->processSuccessfulPayment($response, $payment_status);
}
if(in_array($response->state, ['released', 'cancelled', 'failed', 'expired'])){
//some type of failure
nlog("failure");
$payment_status = ($response->status == 'cancelled') ? Payment::STATUS_CANCELLED : Payment:STATUS_FAILED;
$this->processUnSuccessfulPayment($response, $payment_status);
}
}
@ -228,6 +247,58 @@ https://developer.wepay.com/api/api-calls/checkout
}
*/
private function processSuccessfulPayment($response, $payment_status)
{
$data = [
'payment_type' => PaymentType::CREDIT_CARD_OTHER,
'amount' => $response->gross,
'transaction_reference' => $response->checkout_id,
'gateway_type_id' => GatewayType::CREDIT_CARD,
];
$payment = $this->wepay_payment_driver->createPayment($data, $payment_status);
SystemLogger::dispatch(
['response' => $this->wepay_payment_driver->payment_hash->data->server_response, 'data' => $data],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_SUCCESS,
SystemLog::TYPE_WEPAY,
$this->wepay_payment_driver->client,
$this->wepay_payment_driver->client->company,
);
return redirect()->route('client.payments.show', ['payment' => $this->wepay_payment_driver->encodePrimaryKey($payment->id)]);
}
private function processUnSuccessfulPayment($response, $payment_status)
{
PaymentFailureMailer::dispatch($this->wepay_payment_driver->client, $response->state, $this->wepay_payment_driver->client->company, $response->gross);
PaymentFailureMailer::dispatch(
$this->wepay_payment_driver->client,
$response,
$this->wepay_payment_driver->client->company,
$response->gross
);
$message = [
'server_response' => $response,
'data' => $this->wepay_payment_driver->payment_hash->data,
];
SystemLogger::dispatch(
$message,
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_WEPAY,
$this->wepay_payment_driver->client,
$this->wepay_payment_driver->client->company,
);
throw new PaymentFailed('Failed to process the payment.', 500);
}
private function storePaymentMethod($response, $payment_method_id)
{