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/ZelfVerkopen/zelfverkopen.nl/app/KommaApp/Icepay/Api/ApiController.php
<?php

namespace App\KommaApp\Icepay\Api;

/**
 * @author      Komma <info@komma.pro>
 * @copyright   (c) 2012-2016, Komma
 */

use App\Http\Controllers\Controller;
use App\KommaApp\Customers\Models\Customer;
use App\KommaApp\Icepay\Paybymail\PaybymailService;
use App\KommaApp\Orders\Kms\OrderService;
use App\KommaApp\Orders\Models\Order;
use App\KommaApp\Orders\Models\OrderPayment;
use App\Mail\PaymentLinkMail;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ApiController extends Controller
{
    /** @var ICEPAY $icepayApi */
    private $icepayApi;

    /** @var OrderService $orderService */
    private $orderService;

    /** @var  $payByMailService */
    private $payByMailService;

    /**
     * ApiController constructor.
     */
    public function __construct()
    {
        $this->setupIcePayApi();
        $this->orderService = new OrderService();
        $this->payByMailService = new PaybymailService();
    }


    private function setupIcePayApi()
    {
        $this->icepayApi = new ICEPAY(config('icepay.icepay.merchant_id'), config('icepay.icepay.secret_code'));
    }

    /**
     * Method to be called for icepay. Must be linked to the success url
     *
     * @param Request $request
     */
    public function postback(Request $request)
    {
        \Log::debug('[-------------------------------- Icepay ----------------------------------]');
        \Log::debug('Icepay postback received:');

//        return []; //For fooling icepay.

        //Only handle the postback if it came from icepay
        if(!$this->icepayApi->OnPostback()) {
            \Log::debug('Icepay postback was not valid');
            abort(403);
        }

        $model = new PostbackModel((array) $this->icepayApi->GetPostback());
        \Log::debug('Model postback data: ');
        \Log::debug($model);

        \Log::debug('Icepay postback status: '.$model->getStatus());
        switch ( $model->getStatus() )
        {
            case "OK": // Successful payment
                \Log::debug('A payment was successfull. Setting order status in sale');
                $this->orderService->orderHasBeenPayed($model->getOrderId());
                $this->orderService->updateOrder($model->getOrderId(), Order::ORDER_STATUS_IN_SALE);
                $this->orderService->sendPaymentReceivedMail($model->getOrderId());
                break;
            case "OPEN": // Payment is not yet completed
                \Log::debug('A payment is not yet completed. Settting order to awaiting payment');
                $this->orderService->updateOrder($model->getOrderId(), Order::ORDER_STATUS_AWAITING_PAYMENT);
                break;
            case "ERR": // Error happened
                \Log::debug('A payment error occured. Setting payment status to in processing with payment fails count: '.$model->getPaymentFailsCount());
                $this->orderService->updateOrder($model->getOrderId(), Order::ORDER_STATUS_PROCESSING, $model->getPaymentFailsCount());
                $this->orderService->deletePaymentsForOrder($model->getOrderId());
                break;
            case "REFUND": // Merchant did a refund
                \Log::debug('Merchant refunded the payment');
                $this->sendPostbackErrorMail($model, $model->getOrderId());
                break;
            case "CBACK": // Charge back by end-user
                \Log::debug('Charge back by end-user');
                $this->sendPostbackErrorMail($model, $model->getOrderId());
                break;
        }

        \Log::debug('[-------------------------------- /Icepay ----------------------------------]');

        return response('', 200);
    }

    public function testOrderMail(){
        $this->orderService->testOrderMail('ZELF-2278');
    }

    /**
     * @param Request $request
     */
    public function redirectToSuccessPage(Request $request) {
//        if($this->icepayApi->OnSuccess()) redirect(config('icepay.icepay.successUrlTo'));
        if($this->icepayApi->OnSuccess()) return redirect()->route('startSale.payment.success');
        abort(403);
    }

    /**
     * @param Request $request
     */
    public function redirectToErrorPage(Request $request) {
//        if($this->icepayApi->OnError()) redirect(config('icepay.icepay.errorUrlTo'));
        if($this->icepayApi->OnError()) return redirect()->route('startSale.payment.error');
        abort(403);
    }

    /**
     * For ajax request.
     *
     * @param Request $request
     * @param Order $order
     * @return JsonResponse
     */
    public function generatePaymentUrl(Request $request, Order $order)
    {
        if(!$request->ajax()) abort(403, 'Only ajax requests are allowed');
        
        /** @var Customer $customer */
        $customer = $order->customer()->first();
        if(!$customer) abort(500, 'Order does not have a customer');


        $amountToPayInCents = $this->orderService->getPaymentAmountInCents($order);

        $amountToPayInEuros = round($amountToPayInCents / 100, 2, PHP_ROUND_HALF_UP);

        $url = $this->payByMailService->createPaymentURL($customer->gender, __('icepay.paybymail.salutation'), $customer->last_name, $customer->email, $amountToPayInEuros, $order->order_id,false);
        if(!$url) abort(500, 'generating payment url failed');

        $orderPayment = new OrderPayment();
        $orderPayment->url = $url;
        $orderPayment->order()->associate($order);
        $orderPayment->save();

        $products = $this->orderService->getOrderProducts($order);

        \Mail::send(new PaymentLinkMail($customer,  $url, $products));
        \Log::debug('sent PaymentLinkMail for order: '.$order->order_id);

        return new JsonResponse(['url' => $url]);
    }

    private function sendPostbackErrorMail(PostbackModel $postbackModel, $orderId)
    {
        \Mail::send('site.emails.postbackError', [
            'postbackData'  => (array) $postbackModel,
            'orderId'    => $orderId,
        ], function ($message) use ($orderId) {

            $message->from(\Config::get('mail.from.address'), \Config::get('mail.from.name'));
            $message->to(\Config::get('mail.admin.address'));

            // Set subject
            $message->subject('IcePay postback error order: '.$orderId);

        });
    }
}