Ejemplo n.º 1
0
 public function initContent($cart, $user, $shopname)
 {
     session_start();
     unset($_SESSION['log_id']);
     $_SESSION['log_id'] = time();
     $db = Db::getInstance();
     $token = Tools::getValue('paymillToken');
     $payment = Tools::getValue('payment');
     $validPayments = array();
     if (Configuration::get('PIGMBH_PAYMILL_DEBIT')) {
         $validPayments[] = 'debit';
     }
     if (Configuration::get('PIGMBH_PAYMILL_CREDITCARD')) {
         $validPayments[] = 'creditcard';
     }
     if (empty($token)) {
         $this->log('No paymill token was provided. Redirect to payments page.', null);
         Tools::redirect('order.php?step=1&paymillerror=1&paymillpayment=' . $payment);
     } elseif (!in_array($payment, $validPayments)) {
         $this->log('The selected Paymentmethod is not valid.', $payment);
         Tools::redirect('order.php?step=1&paymillerror=1&paymillpayment=' . $payment);
     }
     $this->log('Start processing payment with token', $token);
     $paymentProcessor = new Services_Paymill_PaymentProcessor(Configuration::get('PIGMBH_PAYMILL_PRIVATEKEY'), "https://api.paymill.com/v2/");
     $currency = Currency::getCurrency((int) $cart->id_currency);
     $iso_currency = $currency['iso_code'];
     $paymentProcessor->setAmount($_SESSION['pigmbhPaymill']['authorizedAmount']);
     $paymentProcessor->setPreAuthAmount($_SESSION['pigmbhPaymill']['authorizedAmount']);
     $paymentProcessor->setToken($token);
     $paymentProcessor->setCurrency(strtolower($iso_currency));
     $paymentProcessor->setName($user["lastname"] . ', ' . $user["firstname"]);
     $paymentProcessor->setEmail($user["email"]);
     $paymentProcessor->setDescription(" ");
     $paymentProcessor->setLogger($this);
     $paymentProcessor->setSource(Configuration::get('PIGMBH_PAYMILL_VERSION') . "_prestashop_" . _PS_VERSION_);
     if ($payment == 'creditcard') {
         $userData = $db->getRow('SELECT `clientId`,`paymentId` FROM `pigmbh_paymill_creditcard_userdata` WHERE `userId`=' . $user["id_customer"]);
     } elseif ($payment == 'debit') {
         $userData = $db->getRow('SELECT `clientId`,`paymentId` FROM `pigmbh_paymill_directdebit_userdata` WHERE `userId`=' . $user["id_customer"]);
     }
     $paymentProcessor->setClientId(!empty($userData['clientId']) ? $userData['clientId'] : null);
     if ($token === "dummyToken") {
         $paymentProcessor->setPaymentId(!empty($userData['paymentId']) ? $userData['paymentId'] : null);
     }
     $result = $paymentProcessor->processPayment();
     $this->log('Payment processing resulted in', $result ? 'Success' : 'Fail');
     $paymill = new PigmbhPaymill();
     // finish the order if payment was sucessfully processed
     if ($result === true) {
         $customer = new Customer((int) $cart->id_customer);
         if ($payment === 'debit') {
             $days = Configuration::get('PIGMBH_PAYMILL_DEBIT_DAYS');
             if (!is_numeric($days)) {
                 $days = '7';
             }
             $paymentText = $paymill->l('ELV /SEPA Debit Date: ') . date('Y-m-d', strtotime("+{$days} day"));
         } else {
             $paymentText = $paymill->l('Credit Card');
         }
         $_SESSION['piPaymentText'] = $paymentText;
         $this->saveUserData($paymentProcessor->getClientId(), $paymentProcessor->getPaymentId(), (int) $cart->id_customer);
         $orderID = $paymill->validateOrder((int) $cart->id, Configuration::get('PIGMBH_PAYMILL_ORDERSTATE'), $cart->getOrderTotal(true, Cart::BOTH), $paymentText, null, array(), null, false, $customer->secure_key);
         $_SESSION['piOrderId'] = $orderID;
         $this->updatePaymillTransaction($paymentProcessor->getTransactionId(), 'OrderID: ' . $orderID . ' - Name:' . $user["lastname"] . ', ' . $user["firstname"]);
         Tools::redirect('order-confirmation.php?key=' . $customer->secure_key . '&id_cart=' . (int) $cart->id . '&id_module=' . (int) $paymill->id . '&id_order=' . (int) $paymill->currentOrder);
     } else {
         $errorMessage = $paymill->errorCodeMapping($paymentProcessor->getErrorCode());
         $this->log('ErrorCode', $errorMessage);
         Tools::redirect('order.php?paymillpayment=' . $payment . '&step=3&paymillerror=1&errorCode=' . $paymentProcessor->getErrorCode());
     }
 }
<?php

include_once dirname(__FILE__) . '/../../config/config.inc.php';
include_once dirname(__FILE__) . '/paymill/v2/lib/Services/Paymill/Transactions.php';
include_once dirname(__FILE__) . '/pigmbhpaymill.php';
$request = json_decode(@file_get_contents('php://input'), true);
if (validateNotification($request)) {
    $orderId = getOrderIdFromNotification($request['event']['event_resource']['transaction']['description']);
    $paymill = new PigmbhPaymill();
    $paymill->updateOrderState($orderId);
    echo "OK";
}
// **** FUNCTIONS ****
function validateNotification($notification)
{
    if (isset($notification) && !empty($notification)) {
        // Check eventtype
        if (isset($notification['event']['event_type'])) {
            if ($notification['event']['event_type'] == 'refund.succeeded') {
                $id = null;
                if (isset($notification['event']['event_resource']['transaction']['id'])) {
                    $id = $notification['event']['event_resource']['transaction']['id'];
                }
                $transactionObject = new Services_Paymill_Transactions(Configuration::get('PIGMBH_PAYMILL_PRIVATEKEY'), 'https://api.paymill.com/v2/');
                $result = $transactionObject->getOne($id);
                return $result['id'] === $id;
            }
        }
    }
    return false;
}