Merge pull request #4781 from beganovich/v5-2701-checkout-autobill

(v5) Checkout.com auto-billing
This commit is contained in:
Benjamin Beganović 2021-01-29 12:01:54 +01:00 committed by GitHub
commit 84f7e14d11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 115 additions and 19 deletions

View File

@ -81,7 +81,7 @@ trait Utilities
return redirect()->route('client.payments.show', ['payment' => $this->getParent()->encodePrimaryKey($payment->id)]);
}
public function processUnsuccessfulPayment(Payment $_payment)
public function processUnsuccessfulPayment(Payment $_payment, $throw_exception = true)
{
PaymentFailureMailer::dispatch(
$this->getParent()->client,
@ -103,7 +103,9 @@ trait Utilities
$this->getParent()->client
);
throw new PaymentFailed($_payment->status, $_payment->http_code);
if ($throw_exception) {
throw new PaymentFailed($_payment->status, $_payment->http_code);
}
}
private function processPendingPayment(Payment $_payment)

View File

@ -12,18 +12,24 @@
namespace App\PaymentDrivers;
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
use App\Http\Requests\Payments\PaymentWebhookRequest;
use App\Jobs\Mail\PaymentFailureMailer;
use App\Jobs\Util\SystemLogger;
use App\Models\ClientGatewayToken;
use App\Models\Company;
use App\Models\GatewayType;
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\PaymentHash;
use App\Models\PaymentType;
use App\Models\SystemLog;
use App\PaymentDrivers\CheckoutCom\CreditCard;
use App\PaymentDrivers\CheckoutCom\Utilities;
use App\Utils\Traits\SystemLogTrait;
use Checkout\CheckoutApi;
use Checkout\Library\Exceptions\CheckoutHttpException;
use Checkout\Models\Payments\IdSource;
use Checkout\Models\Payments\Refund;
use Exception;
@ -96,8 +102,8 @@ class CheckoutComPaymentDriver extends BaseDriver
public function init()
{
$config = [
'secret' => $this->company_gateway->getConfigField('secretApiKey'),
'public' => $this->company_gateway->getConfigField('publicApiKey'),
'secret' => $this->company_gateway->getConfigField('secretApiKey'),
'public' => $this->company_gateway->getConfigField('publicApiKey'),
'sandbox' => $this->company_gateway->getConfigField('testMode'),
];
@ -108,7 +114,7 @@ class CheckoutComPaymentDriver extends BaseDriver
/**
* Process different view depending on payment type
* @param int $gateway_type_id The gateway type
* @param int $gateway_type_id The gateway type
* @return string The view string
*/
public function viewForType($gateway_type_id)
@ -132,7 +138,7 @@ class CheckoutComPaymentDriver extends BaseDriver
/**
* Payment View
*
* @param array $data Payment data array
* @param array $data Payment data array
* @return view The payment view
*/
public function processPaymentView(array $data)
@ -143,7 +149,7 @@ class CheckoutComPaymentDriver extends BaseDriver
/**
* Process the payment response
*
* @param Request $request The payment request
* @param Request $request The payment request
* @return view The payment response view
*/
public function processPaymentResponse($request)
@ -188,7 +194,95 @@ class CheckoutComPaymentDriver extends BaseDriver
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
{
// ..
$amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total;
$invoice = Invoice::whereIn('id', $this->transformKeys(array_column($payment_hash->invoices(), 'invoice_id')))->first();
if ($invoice) {
$description = "Invoice {$invoice->number} for {$amount} for client {$this->client->present()->name()}";
} else {
$description = "Payment with no invoice for amount {$amount} for client {$this->client->present()->name()}";
}
$this->init();
$method = new IdSource($cgt->token);
$payment = new \Checkout\Models\Payments\Payment($method, $this->client->getCurrencyCode());
$payment->amount = $this->convertToCheckoutAmount($amount, $this->client->getCurrencyCode());
$payment->reference = $cgt->meta->last4 . '-' . now();
$request = new PaymentResponseRequest();
$request->setMethod('POST');
$request->request->add(['payment_hash' => $payment_hash->hash]);
//$this->setPaymentHash($payment_hash);
try {
$response = $this->gateway->payments()->request($payment);
if ($response->status == 'Authorized') {
$this->confirmGatewayFee($request);
$data = [
'payment_method' => $response->source['id'],
'payment_type' => PaymentType::parseCardType(strtolower($response->source['scheme'])),
'amount' => $amount,
'transaction_reference' => $response->id,
];
$payment = $this->createPayment($data, \App\Models\Payment::STATUS_COMPLETED);
SystemLogger::dispatch(
['response' => $response, 'data' => $data],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_SUCCESS,
SystemLog::TYPE_CHECKOUT,
$this->client
);
return $payment;
}
if ($response->status == 'Declined') {
$this->unWindGatewayFees($payment_hash);
PaymentFailureMailer::dispatch(
$this->client, $response->response_summary,
$this->client->company,
$amount
);
$message = [
'server_response' => $response,
'data' => $payment_hash->data,
];
SystemLogger::dispatch(
$message,
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_CHECKOUT,
$this->client
);
return false;
}
} catch (\Exception | CheckoutHttpException $e) {
$this->unWindGatewayFees($payment_hash);
$message = $e instanceof CheckoutHttpException
? $e->getBody()
: $e->getMessage();
$data = [
'status' => '',
'error_type' => '',
'error_code' => $e->getCode(),
'param' => '',
'message' => $message,
];
SystemLogger::dispatch($data, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_CHECKOUT, $this->client);
}
}
public function processWebhookRequest(PaymentWebhookRequest $request, Payment $payment = null)