1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 00:41:34 +02:00
invoiceninja/app/PaymentDrivers/MolliePaymentDriver.php

196 lines
5.0 KiB
PHP
Raw Normal View History

2021-07-23 13:47:01 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\PaymentDrivers;
2021-07-29 15:13:38 +02:00
use App\Http\Requests\Gateways\Mollie\Mollie3dsRequest;
2021-07-23 13:47:01 +02:00
use App\Http\Requests\Payments\PaymentWebhookRequest;
2021-07-31 13:26:00 +02:00
use App\Jobs\Util\SystemLogger;
2021-07-23 13:47:01 +02:00
use App\Models\ClientGatewayToken;
use App\Models\GatewayType;
use App\Models\Payment;
use App\Models\PaymentHash;
use App\Models\SystemLog;
2021-07-23 14:43:32 +02:00
use App\PaymentDrivers\Mollie\CreditCard;
2021-07-23 13:47:01 +02:00
use App\Utils\Traits\MakesHash;
2021-08-02 14:53:44 +02:00
use Illuminate\Support\Facades\Validator;
use Mollie\Api\Exceptions\ApiException;
2021-07-23 14:43:32 +02:00
use Mollie\Api\MollieApiClient;
2021-07-23 13:47:01 +02:00
class MolliePaymentDriver extends BaseDriver
{
use MakesHash;
/**
* @var boolean
*/
public $refundable = true;
/**
* @var true
*/
public $token_billing = true;
/**
* @var true
*/
public $can_authorise_credit_card = true;
/**
2021-07-23 14:43:32 +02:00
* @var MollieApiClient
2021-07-23 13:47:01 +02:00
*/
public $gateway;
/**
* @var mixed
*/
public $payment_method;
/**
* @var string[]
*/
public static $methods = [
GatewayType::CREDIT_CARD => CreditCard::class,
];
2021-07-23 14:43:32 +02:00
const SYSTEM_LOG_TYPE = SystemLog::TYPE_MOLLIE;
2021-07-23 13:47:01 +02:00
2021-07-23 14:43:32 +02:00
public function init(): self
2021-07-23 13:47:01 +02:00
{
2021-07-23 14:43:32 +02:00
$this->gateway = new MollieApiClient();
$this->gateway->setApiKey(
$this->company_gateway->getConfigField('apiKey'),
);
2021-07-23 13:47:01 +02:00
return $this;
}
public function gatewayTypes(): array
{
$types = [];
$types[] = GatewayType::CREDIT_CARD;
return $types;
}
public function setPaymentMethod($payment_method_id)
{
$class = self::$methods[$payment_method_id];
2021-07-23 14:43:32 +02:00
2021-07-23 13:47:01 +02:00
$this->payment_method = new $class($this);
2021-07-23 14:43:32 +02:00
2021-07-23 13:47:01 +02:00
return $this;
}
public function authorizeView(array $data)
{
return $this->payment_method->authorizeView($data);
}
public function authorizeResponse($request)
{
return $this->payment_method->authorizeResponse($request);
}
public function processPaymentView(array $data)
{
return $this->payment_method->paymentView($data);
}
public function processPaymentResponse($request)
{
return $this->payment_method->paymentResponse($request);
}
public function refund(Payment $payment, $amount, $return_client_response = false)
{
return $this->payment_method->yourRefundImplementationHere();
}
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
{
return $this->payment_method->yourTokenBillingImplmentation();
}
2021-08-02 14:53:44 +02:00
public function processWebhookRequest(PaymentWebhookRequest $request)
2021-07-23 13:47:01 +02:00
{
2021-08-02 14:53:44 +02:00
$validator = Validator::make($request->all(), [
'id' => ['required', 'starts_with:tr'],
]);
if ($validator->fails()) {
return response()->json($validator->errors(), 422);
}
$this->init();
$codes = [
'open' => Payment::STATUS_PENDING,
'canceled' => Payment::STATUS_CANCELLED,
'pending' => Payment::STATUS_PENDING,
'expired' => Payment::STATUS_CANCELLED,
'failed' => Payment::STATUS_FAILED,
'paid' => Payment::STATUS_COMPLETED,
];
try {
$payment = $this->gateway->payments->get($request->id);
$record = Payment::where('transaction_reference', $payment->id)->firstOrFail();
$record->status_id = $codes[$payment->status];
$record->save();
return response()->json([], 200);
} catch(ApiException $e) {
return response()->json(['message' => $e->getMessage(), 'gatewayStatusCode' => $e->getCode()], 500);
}
2021-07-23 13:47:01 +02:00
}
2021-07-31 13:26:00 +02:00
2021-07-29 15:13:38 +02:00
public function process3dsConfirmation(Mollie3dsRequest $request)
{
$this->init();
$this->setPaymentHash($request->getPaymentHash());
try {
$payment = $this->gateway->payments->get($request->getPaymentId());
return (new CreditCard($this))->processSuccessfulPayment($payment);
2021-07-31 13:26:00 +02:00
} catch (\Mollie\Api\Exceptions\ApiException $e) {
2021-07-29 15:13:38 +02:00
return (new CreditCard($this))->processUnsuccessfulPayment($e);
}
}
2021-07-31 13:26:00 +02:00
public function detach(ClientGatewayToken $token)
{
$this->init();
try {
$this->gateway->mandates->revokeForId($token->gateway_customer_reference, $token->token);
} catch (\Mollie\Api\Exceptions\ApiException $e) {
SystemLogger::dispatch(
[
'server_response' => $e->getMessage(),
'data' => request()->all(),
],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_MOLLIE,
$this->client,
$this->client->company
);
}
}
2021-07-23 13:47:01 +02:00
}