HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/slenders/slenders.nl/app/Komma/Shop/Payment/PSPAdapters/AbstractPSPAdapter.php
<?php
namespace App\Komma\Shop\Payment\PSPAdapters;


use App\Komma\Shop\Orders\Models\Order;
use App\Komma\Shop\Payment\Events\TransactionChanged;
use App\Komma\Shop\Payment\Transaction;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\App;
use Symfony\Component\HttpFoundation\Response;

/**
 * Class AbstractPSPAdapter
 *
 * This adapter must be used to let an api script provided by the PSP
 * talk to KMS and vice versa.
 * It adapts their api script to conform to the workings of KMS and vice versa.
 *
 * This adapter also manipulates transactions
 *
 * @package App\Komma\Shop\Payment\PSPAdapters
 */
abstract class AbstractPSPAdapter
{
    /** @var string The current environment */
    protected $environment;

    /** @var string The name of the config key that represents the current psp that holds the current adapter. It is one of the keys in the array payment_service_providers from the payment configuration file */
    protected $pspName;

    public function __construct()
    {
        $this->environment = App::environment();
    }

    /**
     * Return a response that redirects the user to a page
     * where the user can select a payment method (credit card, iDEAL etc) and issuer (bank),
     * and complete the payment
     *
     * @param Transaction $transaction
     * @return RedirectResponse
     */
    abstract public function redirectForPayment(Transaction $transaction): RedirectResponse;

    /**
     * Creates a payment transaction for a certain order.
     *
     * @param Order $order
     * @return Transaction
     */
    abstract public function createTransaction(Order $order): Transaction;

    /**
     * Updates the payment status for a certain order payment transaction.
     *
     * Must be triggered when the PSP has an update for a certain payment transaction.
     * To indicate for example that a payment transaction was completed successfully.
     *
     * Important. When this method is done processing. It SHOULD call the saveTransactionAndNotify method.
     * This to tell listeners that a transaction was changed.
     * It SHOULD in general return a json response. But MAY also return a view after updating the transation.
     *
     * @param Order|null $order
     * @return Response
     */
    abstract public function processPSPResponse(Order $order = null): Response;

    /**
     * Convert a payment status from the psp to a payment status that the kms shop
     * understands
     *
     * @param $status
     * @return string
     */
    abstract public function adaptPSPTransactionStatusToKmsTransactionStatus($status): string;

    /**
     * Converts a payment status from the kms shop to a payment status that the psp understands
     *
     * @param $status
     * @return mixed
     */
    abstract public function adaptKmsTransactionStatusToPSPTransactionStatus(string $status);

    /**
     * Dispatches an TransactionChanged event to tell kms that a transaction was changed and what was changed.
     * And afterwards saves the transaction
     *
     * @param Transaction $transaction
     * @return Transaction
     */
    protected function saveTransactionAndNotify(Transaction $transaction): Transaction
    {
        $event = new TransactionChanged($transaction);
        $transaction->save(); //We save the transaction after creating the event, because the event collects the dirty attributes upon instantiation. When you save it before creating the event, this is not possible.
        event($event); //Dispatch the event so it will hit listeners elsewhere. That may trigger for example mails being send.

        return $transaction;
    }
}