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/pietvanmierlo/stempelbv.nl/app/Komma/Shop/Payment/PSPAdapters/Komma.php
<?php
namespace App\Komma\Shop\Payment\PSPAdapters;

use App\Komma\Globalization\RegionInfo;
use App\Komma\Shop\Orders\Models\Order;
use App\Komma\Shop\Orders\OrderStatus;
use App\Komma\Shop\Payment\TransactionStatus;
use App\Komma\Shop\Payment\Transaction;
use App\Komma\Users\Models\KmsUser;
use Carbon\Carbon;
use Illuminate\Http\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;

/**
 * Class Komma
 *
 * Fake PSP adapter
 *
 * @package App\Komma\Shop\Payment\PSPAdapters
 */
class Komma extends AbstractPSPAdapter
{
    /**
     * Komma constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $this->pspName = 'komma';
        $this->shopName = config('payment.payment_service_providers.'. $this->pspName . '.' . $this->environment . '.shop_name');
    }

    /**
     * Creates a payment transaction for a certain order.
     *
     * @param Order $order
     * @return Transaction
     * @throws \Exception
     */
    public function createTransaction(Order $order): Transaction
    {
        /** @var KmsUser $customer */
        $customer = $order->customer()->first();
        $shopRegionInfo = new RegionInfo('NL'); //Shop was build with euros in mind and the Dutch language as the primary one

        $transaction = new Transaction();
        $transaction->psp = $this->pspName;
        $transaction->status = TransactionStatus::OPEN;

        //A real adapter would update fields like this:
        $transaction->psp_id = $order->id;
        $transaction->payment_link = route('fakepsp', ['order' => $order]);
        $transaction->payment_method = null;
        $transaction->status = TransactionStatus::PAYMENT_PENDING;
        $transaction->expire_date = Carbon::now()->addMinutes(15)->toDateTimeString();
        $transaction->payment_date = null;

        $transaction->order()->associate($order);
        $transaction->ip = request()->ip();
        $transaction->currency_iso_4217_code = $shopRegionInfo->getISOCurrencySymbol();
        $transaction->amount = $order->total_price;
        $transaction->save();

        return $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
     */
    public function processPSPResponse(Order $order = null): Response
    {
        $status = \Input::get('status');

        //In a production environment for a real adapter you must never accept only the order to update the transaction.
        //The psp must provide some kind of id that you can use in combination with the order to guarantee that the update is legit

        if(!$order) {
            \Log::error('Komma Adapter: Komma wanted to update a transaction without specifying its order.');
            return response()->json(['Please provide a order'], 400);
        }
        \Log::debug('Komma Adapter: Komma called kms with a status update for order with id: '.$order->id.'. Updating transaction....');

        $transactionQuery = Transaction::where('order_id', '=', $order->id);
        $transaction = $transactionQuery->first();

        if(!$transaction) {
            if(!$order)\Log::debug('Komma Adapter: Could not find transaction. order id: '.$order->id.'. psp: '.$this->pspName);
            else \Log::debug('Komma Adapter: Could not find transaction. order id: '.$order->id.'. psp: '.$this->pspName);
            return response()->json(['The transaction / payment could not be found'], 404);
        }

        $transaction->status = $status;
        $transaction->payment_method = "KommaPal";
        if($status == TransactionStatus::PAYMENT_PAID) $transaction->payment_date = Carbon::now()->toDateTimeString();

        $this->saveTransactionAndNotify($transaction);

        \Log::debug('Komma Adapter: Transaction with order id "'.$order->id.'" successfully updated! Also telling that to the Fake PSP with http status 200. Transaction status: '.$transaction->status);

        switch ($transaction->status) {
            case TransactionStatus::PAYMENT_PAID:
                return redirect()->to(route('transaction.view.accepted', ['order' => $order]));
            default:
                return redirect()->to(route('transaction.view.exception', ['order' => null]));
        }
    }


    /**
     * 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
     */
    public function redirectForPayment(Transaction $transaction): RedirectResponse
    {
        $transaction->status = TransactionStatus::PAYMENT_PENDING;
        $transaction->save();
        $transaction->order->status = OrderStatus::PENDING;
        $transaction->order->save();

        return response()->redirectTo($transaction->payment_link, 303); //303 forces a GET redirect according to Komma.
    }

    /**
     * Convert a payment status from the psp to a payment status that the kms shop
     * understands
     *
     * @param $status
     * @return string
     */
    public function adaptPSPTransactionStatusToKmsTransactionStatus($status): string
    {
        //Not used in the fake komma adapter
    }

    /**
     * Converts a payment status from the kms shop to a payment status that the psp understands
     *
     * @param $status
     * @return mixed
     */
    public function adaptKmsTransactionStatusToPSPTransactionStatus(string $status)
    {
        //Not used in the fake komma adapter
    }
}