/** * Generate form * @param $order_id * @return string */ function generate_pagantis_form($order_id) { //Logs $this->log->add('pagantis', 'Acceso al pago con Paga+Tarde'); $order = new WC_Order($order_id); //Total Amount $transaction_amount = number_format((double) $order->get_total(), 2, '.', ''); $transaction_amount = str_replace('.', '', $transaction_amount); $transaction_amount = floatval($transaction_amount); // Product Description $products = WC()->cart->cart_contents; $i = 1; $products_form = ""; $description = ""; foreach ($products as $product) { $products_form .= "\r\n <input name='items[" . $i . "][description]' type='hidden' value='" . $product['data']->post->post_title . ' (' . $product['quantity'] . ')' . "'>\r\n <input name='items[" . $i . "][quantity]' type='hidden' value='" . $product['quantity'] . "'>\r\n <input name='items[" . $i . "][amount]' type='hidden' value='" . round(($product['data']->price + $product['line_tax']) * $product['quantity'], 2) . "'>\r\n "; $description[] = $product['data']->post->post_title . ' (' . $product['quantity'] . ')'; $i++; } $shipping = $order->get_items('shipping'); foreach ($shipping as $shipping_method) { if ($shipping_method['cost'] > 0) { $products_form .= "\r\n <input name='items[" . $i . "][description]' type='hidden' value='" . __('Gastos de envio', 'pagantis') . "'>\r\n <input name='items[" . $i . "][quantity]' type='hidden' value='1'>\r\n <input name='items[" . $i . "][amount]' type='hidden' value='" . $shipping_method['cost'] . "'>\r\n "; $description[] = 'Gastos de envio'; $i++; } } $account_id = ""; $dataToEncode = ""; $this->ok_url = $this->get_return_url($order); $this->ko_url = htmlspecialchars_decode(WC_Cart::get_checkout_url()); $this->cancelled_url = htmlspecialchars_decode(WC_Cart::get_checkout_url()); //Test Environment Selected if ($this->environment == self::TEST_ENVIRONMENT) { $this->key = $this->test_key; $dataToEncode = $this->test_key . $this->test_account . $order_id . $transaction_amount . $this->currency . $this->ok_url . $this->ko_url . $this->callback_url . $this->discount . $this->cancelled_url; $account_id = $this->test_account; } else { $this->key = $this->real_key; $dataToEncode = $this->real_key . $this->real_account . $order_id . $transaction_amount . $this->currency . $this->ok_url . $this->ko_url . $this->callback_url . $this->discount . $this->cancelled_url; $account_id = $this->real_account; } $signature = hash('sha512', $dataToEncode); $form_url = $this->paga_mastarde_url; $adressInfo = explode("/", $order->billing_city); $customer = new WC_Customer(); $countries = new WC_Countries(); $address = $customer->get_address() . " " . $customer->get_address_2(); $city = $customer->get_city(); $postcode = $customer->get_postcode(); $states = $countries->get_states(); $state = $states[$order->shipping_country][$customer->get_state()]; $saddress = $customer->shipping_address_1 . " " . $customer->shipping_address_2; $scity = $customer->shipping_city; $spostcode = $customer->shipping_postcode; $sstate = $states[$order->shipping_country][$customer->shipping_state]; //Create Form $buyer_name = $order->billing_first_name . " " . $order->billing_last_name; $pagantis_form_fields = "\r\n <input name='order_id' type='hidden' value='" . $order_id . "' />\r\n <input name='amount' type='hidden' value='" . $transaction_amount . "' />\r\n <input name='currency' type='hidden' value='" . $this->currency . "' />\r\n\r\n <input name='ok_url' type='hidden' value='" . $this->ok_url . "' />\r\n <input name='nok_url' type='hidden' value='" . $this->ko_url . "' />\r\n <input name='cancelled_url' type='hidden' value='" . $this->cancelled_url . "' />\r\n\r\n <input name='locale' type='hidden' value='" . $this->lang . "' />\r\n\r\n <input name='full_name' type='hidden' value='" . $buyer_name . "'>\r\n <input name='email' type='hidden' value='" . $order->billing_email . "'>\r\n\r\n " . $products_form . "\r\n\r\n <!-- firma de la operación -->\r\n <input name='account_id' type='hidden' value='" . $account_id . "' />\r\n <input name='signature' type='hidden' value='" . $signature . "' />\r\n\r\n <!-- discount -->\r\n <input name='discount[full]' type='hidden' value='" . $this->discount . "' />\r\n <input name='iframe' type='hidden' value='" . $this->iframe . "' />\r\n\r\n <!-- callback url -->\r\n <input name='callback_url' type='hidden' value='" . $this->callback_url . "' />\r\n\r\n <input name='description' type='hidden' value='" . implode(',', $description) . "' />\r\n\r\n <!-- address -->\r\n <input name='address[street]' type='hidden' value='" . $address . "'>\r\n <input name='address[city]' type='hidden' value='" . $city . "'>\r\n <input name='address[province]' type='hidden' value='" . $state . "'>\r\n <input name='address[zipcode]' type='hidden' value='" . $postcode . "'>\r\n\r\n <input name='shipping[street]' type='hidden' value='" . $saddress . "'>\r\n <input name='shipping[city]' type='hidden' value='" . $scity . "'>\r\n <input name='shipping[province]' type='hidden' value='" . $sstate . "'>\r\n <input name='shipping[zipcode]' type='hidden' value='" . $spostcode . "'>\r\n\r\n <!-- phone -->\r\n\r\n <input name='mobile_phone' type='hidden' value='" . $order->billing_phone . "'>\r\n "; $return_form = '<form action="' . $form_url . '" method="post" id="pagantis_payment_form"> ' . $pagantis_form_fields . ' <input type="submit" class="button-alt" id="submit_vme_payment_form" value="' . __('Realizar el pago', 'pagantis') . '" /> </form>'; if ($this->iframe == 'true') { wp_enqueue_style('style', esc_url(plugins_url('pages/assets/css/iframe.css', __FILE__))); $return_form .= '<div id="myModal" class="paylater_modal"> <!-- Modal content --> <div class="paylater_modal-content"> <span id="paylater_close">x</span> <iframe id="iframe-pagantis" name="iframe-pagantis" style="width:100%;height:600px;display:block"></iframe> </div> </div>'; $return_form .= '<script type="text/javascript"> el = document.getElementById("submit_vme_payment_form"); el.addEventListener("click", function (e){ e.preventDefault(); document.getElementById("pagantis_payment_form").setAttribute("target", "iframe-pagantis"); document.getElementById("pagantis_payment_form").submit(); document.getElementById("iframe-pagantis").style.display = "block"; document.getElementById("myModal").style.display = "block"; }); var closeModal = function closeModal(evt) { evt.preventDefault(); document.getElementById("myModal").style.display = "none"; }; var elements = document.querySelectorAll("#paylater_close, #myModal"); Array.prototype.forEach.call(elements, function(el){ el.addEventListener("click", closeModal); }); document.addEventListener("DOMContentLoaded", function(event) { document.getElementById("pagantis_payment_form").setAttribute("target", "iframe-pagantis"); document.getElementById("pagantis_payment_form").submit(); document.getElementById("iframe-pagantis").style.display = "block"; document.getElementById("myModal").style.display = "block"; }); </script>'; } else { $return_form .= "<script type='text/javascript'>\r\n jQuery(document).ready(function() {\r\n jQuery('#submit_vme_payment_form').click();\r\n });\r\n </script>"; } return $return_form; }
function get_proceed_link($parameter) { $id = $parameter['id']; $wc = new WC_Cart(); return add_query_arg(array('add-to-cart' => $id), $wc->get_checkout_url()); }
/** * Process the payment * * @param integer $order_id */ public function process_payment($order_id) { global $wpdb; $order = new WC_Order($order_id); // fill in the product cart inside the product_info parameter $products = $order->get_items(); foreach ($products as $product) { $product_info = $product_info . $product['name'] . ","; } $this->ok_url = $this->get_return_url($order); $this->ko_url = htmlspecialchars_decode(WC_Cart::get_checkout_url()); $this->callback_url = esc_url(get_site_url() . '/index.php/wc-api/WC_GetFinancing/'); $gf_data = array('amount' => $order->order_total, 'product_info' => $product_info, 'first_name' => $order->billing_first_name, 'last_name' => $order->billing_last_name, 'shipping_address' => array('street1' => $order->shipping_address_1 . " " . $order->shipping_address_2, 'city' => $order->shipping_city, 'state' => $order->shipping_state, 'zipcode' => $order->shipping_postcode), 'billing_address' => array('street1' => $order->billing_address_1 . " " . $order->billing_address_2, 'city' => $order->billing_city, 'state' => $order->billing_state, 'zipcode' => $order->billing_postcode), 'email' => $order->billing_email, 'phone' => $order->billing_phone, 'success_url' => $this->ok_url, 'failure_url' => $this->ko_url, 'postback_url' => $this->callback_url, 'merchant_loan_id' => (string) $order->get_order_number(), 'version' => '1.9', 'software_name' => 'woocommerce', 'software_version' => 'woocommerce ' . WC_VERSION); $body_json_data = json_encode($gf_data); $header_auth = base64_encode($this->username . ":" . $this->password); if ($this->environment == "test") { $url_to_post = $this->gateway_url_stage; } else { $url_to_post = $this->gateway_url_prod; } $url_to_post .= '/merchant/' . $this->merchant_id . '/requests'; $post_args = array('body' => $body_json_data, 'timeout' => 60, 'blocking' => true, 'sslverify' => false, 'headers' => array('Content-Type' => 'application/json', 'Authorization' => 'Basic ' . $header_auth, 'Accept' => 'application/json')); //echo '<pre>' . print_r($post_args, true) . '</pre>'; $gf_response = wp_remote_post($url_to_post, $post_args); //echo '<br><br><pre>' . print_r($gf_response, true) . '</pre>'; //die(); if (is_wp_error($gf_response)) { wc_add_notice('GetFinancing cannot process your order. Please try again or select a different payment method.', 'error'); return array('result' => 'fail', 'redirect' => ''); } if (isset($gf_response['body'])) { $response_body = json_decode($gf_response['body']); } else { wc_add_notice('GetFinancing cannot process your order. Please try again or select a different payment method.', 'error'); return array('result' => 'fail', 'redirect' => ''); } if (isset($response_body->href) == false || empty($response_body->href) == ture) { wc_add_notice('GetFinancing cannot process your order. Please try again or select a different payment method.', 'error'); return array('result' => 'fail', 'redirect' => ''); } // If we are here that means that the gateway give us a "created" status. // then we can create the order in hold status. global $woocommerce; $order = new WC_Order($order_id); //insert merchant_transaction_id <-> order_id relation $table_name = $wpdb->prefix . 'getfinancing'; $wpdb->insert($table_name, array('order_id' => $order_id, 'merchant_transaction_id' => $response_body->inv_id)); // order is 'pending' by default // we add a note to the order $order->add_order_note('Waiting to finish GetFinancing process!'); // Store gf process url in session. WC()->session->getfinancing_process_url = $response_body->href; // Adds the token to the order. update_post_meta($order->id, 'getfinancing_custid', $response_body->customer_id); return array('result' => 'success', 'redirect' => $order->get_checkout_payment_url(true)); }