Exemplo n.º 1
0
require_once 'crypto.php';
require_once 'setup.php';
echo "preparing payment init data ...\n\n";
$merchantId = $_POST['merchant_id'];
$orderNo = $_POST['order_no'];
$totalAmount = $_POST['total_amount'];
$shippingAmount = $_POST['shipping_amount'];
$returnUrl = $_POST['return_url'];
$goods_desc = $_POST['goods_desc'];
$description = $_POST['description'];
$customerId = $_POST['customer_id'];
$returnMethodPOST = "yes";
$closePayment = false;
$merchantData = null;
$dttm = (new DateTime())->format("YmdHis");
$cart = createCartData($goods_desc, $totalAmount, $shippingAmount);
echo "preparing cart data:\n";
echo json_encode($cart, JSON_PRETTY_PRINT + JSON_UNESCAPED_SLASHES + JSON_UNESCAPED_UNICODE) . "\n\n";
$data = createPaymentInitData($merchantId, $orderNo, $dttm, $totalAmount, $returnUrl, $cart, $description, $customerId, $privateKey, $privateKeyPassword, $closePayment, $merchantData, $returnMethodPOST);
echo "prepared payment/init request:\n";
echo json_encode($data, JSON_PRETTY_PRINT + JSON_UNESCAPED_SLASHES + JSON_UNESCAPED_UNICODE) . "\n\n";
echo "processing payment/init request ...\n\n";
$ch = curl_init($url . NativeApiMethod::$init);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Accept: application/json;charset=UTF-8'));
$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'payment/init failed, reason: ' . curl_error($ch);
Exemplo n.º 2
0
 public function save_order_process($order_id)
 {
     $order = new WC_Order($order_id);
     $this->monetWebPay->log->write('orderNumber ' . $order->get_order_number());
     $partsOforderNumber = $this->exploderOrderNumber($order->get_order_number());
     $this->monetWebPay->log->write('parts of orderNumber ' . $partsOforderNumber[0] . ', ' . $partsOforderNumber[1]);
     $orderNo = $order->get_order_number();
     $location = $this->get_return_url($order);
     $customer = WC()->customer;
     $dttm = (new DateTime())->format("YmdHis");
     $data;
     $returnUrl = plugins_url('returnUrl.php?orderNumber=' . $order_id, __FILE__);
     $this->monetWebPay->log->write('Checking order ' . $orderNo . ", orderId " . $order_id);
     $row = $this->monetWebPay->selectTransaction($partsOforderNumber[0]);
     // kontrola jestli existuje objednavka
     $this->monetWebPay->log->write('after select');
     $cart = createCartData(WC()->cart, $order->get_total(), $this->firstCartItemDesc, $this->secondCartItemDesc);
     $this->monetWebPay->log->write('cart created');
     $paymentId = $row['payId'];
     $paymentStatus = $row['paymentStatus'];
     $this->monetWebPay->log->write('loaded paymentStatus: ' . $paymentStatus . ' PayId: ' . $paymentId);
     // pokud neni dosud platba inicializovana anebo byla zrusena nebo zamitnuta (payId null) anebo se zmenil kosik, provedeme payment/init
     if (is_null($paymentId) || !is_null($row['cart']) && $row['cart'] !== json_encode($cart)) {
         $this->monetWebPay->log->write('payment not initialized OR payment cancelled or declined OR detected cart changes');
         $urlGate = $this->urlGate . NativeApiMethod::$init;
         $this->monetWebPay->log->write('payment/init, url: ' . $urlGate);
         if (!is_null($row['paymentStatus']) && ($row['paymentStatus'] == PaymentStatus::$canceled || $row['paymentStatus'] == PaymentStatus::$declined)) {
             $this->monetWebPay->log->write('payment cancelled or declined, setting up cart from db: ' . $row['cart']);
             $cart = json_decode($row['cart'], true);
         }
         $data = createPaymentInitData($this->merchantId, $partsOforderNumber[0], $dttm, $order->get_total(), $returnUrl, $cart, "Objednavka " . $order->get_order_number(), $order->get_user_id(), $this->privateKey, $this->privateKeyPassword, $this->moneyTransfer, null, $this->returnMethodPOST);
         $this->monetWebPay->log->write('payment/init data: ' . json_encode($data));
         $ch = curl_init($urlGate);
         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
         curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
         curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Accept: application/json;charset=UTF-8'));
         $result = curl_exec($ch);
         if (curl_errno($ch)) {
             $this->msg['message'] = 'Inicializace platby na platební bráně se nezdařila, důvod: ' . curl_error($ch);
             $this->msg['class'] = 'error';
             wc_add_notice(__($this->msg['message'], 'monet') . $error_message, 'error');
             return;
         }
         curl_close($ch);
         $this->monetWebPay->log->write('payment/init result: ' . $result);
         $result_array = json_decode($result, true);
         if (is_null($result_array['resultCode'])) {
             $this->msg['message'] = 'Přesměrování na platební bránu se nezdařilo';
             $this->msg['class'] = 'error';
             wc_add_notice(__($this->msg['message'], 'monet') . $error_message, 'error');
             return;
         }
         $response = $this->prepareResponse($result_array);
         if ($this->monetWebPay->verifyPaymentInitSignature($response, $this->publicKey, "payment/init verify") == false) {
             $this->msg['message'] = 'Inicializace platby na platební bráně se nezdařila, nepodařilo se ověřit podpis odpovědi';
             $this->msg['class'] = 'error';
             wc_add_notice(__($this->msg['message'], 'monet') . $error_message, 'error');
             return;
         }
         if ($result_array['resultCode'] != '0') {
             $this->msg['message'] = 'Inicializace platby na platební bráně se nezdařila, důvod: ' . $result_array['resultMessage'];
             $this->msg['class'] = 'error';
             wc_add_notice(__($this->msg['message'], 'monet') . $error_message, 'error');
             return;
         }
         $paymentId = $result_array['payId'];
         if (is_null($row['paymentStatus'])) {
             $this->monetWebPay->insertTransaction($response, $partsOforderNumber[0], $data['cart']);
         } else {
             $this->monetWebPay->updateTransaction($partsOforderNumber[0], $response, $data['cart']);
         }
     }
     $dttm = (new DateTime())->format("YmdHis");
     $urlProccessPart = createPaymentProcessUrl($this->merchantId, $paymentId, $dttm, $this->privateKey, $this->privateKeyPassword);
     $processUrlGate = $this->urlGate . NativeApiMethod::$process . $urlProccessPart;
     $this->monetWebPay->log->write('executing payment/process, url: ' . $processUrlGate);
     header('Location: ' . $processUrlGate);
 }