/** * Process the payment and return the result * * @access public * @param int $order_id * @return array */ function process_payment($order_id) { global $woocommerce; $order = new WC_Order($order_id); $token = $_POST['payfortToken']; try { if (empty($token)) { $error_msg = __('Please make sure your card details have been entered correctly.', 'woocommerce'); throw new Start_Error($error_msg); } $charge_description = $order->id . ": WooCommerce charge for " . $order->billing_email; $order_items = $order->get_items(); $order_items_array_full = array(); $user_info = wp_get_current_user(); $user_name = $user_info->user_login; $udata = get_userdata($user_info->ID); if (isset($udata->user_registered)) { $registered_at = date(DATE_ISO8601, strtotime($udata->user_registered)); } else { $registered_at = date(DATE_ISO8601, strtotime(date("Y-m-d H:i:s"))); } foreach ($order_items as $key => $items) { $itemClass = new WC_Product($items['product_id']); $order_items_array['title'] = $items['name']; $order_items_array['amount'] = round($itemClass->get_price(), 2) * $this->currency_multiplier[get_woocommerce_currency()]; $order_items_array['quantity'] = $items['qty']; array_push($order_items_array_full, $order_items_array); } $billing_address = array("first_name" => $order->billing_first_name, "last_name" => $order->billing_last_name, "country" => $order->billing_country, "city" => $order->billing_city, "address_1" => $order->billing_address_1, "address_2" => $order->billing_address_2, "phone" => $order->billing_phone, "postcode" => $order->billing_postcode); $shipping_address = array("first_name" => $order->shipping_first_name, "last_name" => $order->shipping_last_name, "country" => $order->shipping_country, "city" => $order->shipping_city, "address_1" => $order->shipping_address_1, "address_2" => $order->shipping_address_2, "phone" => $order->shipping_phone, "postcode" => $order->shipping_postcode); $shopping_cart_array = array('user_name' => $user_name, 'registered_at' => $registered_at, 'items' => $order_items_array_full, 'billing_address' => $billing_address, 'shipping_address' => $shipping_address); $charge_args = array('description' => $charge_description, 'card' => $token, 'currency' => strtoupper(get_woocommerce_currency()), 'email' => $order->billing_email, 'ip' => $_SERVER['REMOTE_ADDR'], 'amount' => $order->get_total() * $this->currency_multiplier[get_woocommerce_currency()], 'shopping_cart' => $shopping_cart_array, 'shipping_amount' => round($order->get_total_shipping(), 2) * $this->currency_multiplier[get_woocommerce_currency()], 'metadata' => array('reference_id' => $order_id)); if ($this->test_mode == 'yes') { Start::setApiKey($this->test_secret_key); } else { Start::setApiKey($this->live_secret_key); } $start_plugin_data = get_file_data('wp-content/plugins/payfort/woocommerce-payfort.php', array('Version'), 'plugin'); $woo_plugin_data = get_file_data('wp-content/plugins/woocommerce/woocommerce.php', array('Version'), 'plugin'); $userAgent = 'WooCommerce ' . $woo_plugin_data['0'] . ' / Start Plugin ' . $start_plugin_data['0']; Start::setUserAgent($userAgent); $charge = Start_Charge::create($charge_args); // No exceptions? Yaay, all done! $order->payment_complete(); return array('result' => 'success', 'redirect' => $this->get_return_url($order)); } catch (Start_Error $e) { // TODO: Can we get the extra params (so the error is more apparent)? // e.g. Instead of "request params are invalid", we get // "extras":{"amount":["minimum amount (in the smallest currency unit) is 185 for AED"] $error_code = $e->getErrorCode(); if ($error_code === "card_declined") { $message = __('Error: ', 'woothemes') . $e->getMessage() . " Please, try with another card"; } else { $message = __('Error: ', 'woothemes') . $e->getMessage(); } // If function should we use? if (function_exists("wc_add_notice")) { // Use the new version of the add_error method wc_add_notice($message, 'error'); } else { // Use the old version $woocommerce->add_error($message); } // we raise 'update_checkout' event for javscript // to remove card token WC()->session->set('refresh_totals', true); return array('result' => 'fail', 'redirect' => ''); } }
public function collectPayment(\Mage_Payment_Model_Info $payment, $amount, $capture = true) { $Currency = Mage::app()->getStore()->getBaseCurrencyCode(); require_once MAGENTO_ROOT . '/lib/Start/autoload.php'; # At the top of your PHP file $token = isset($_POST['payfortToken']) ? $_POST['payfortToken'] : false; $email = isset($_POST['payfortEmail']) ? $_POST['payfortEmail'] : false; if (!$token || !$email) { //this block will be executed if the order was authorized earlier and now trying to capture amount $token_array = $payment->getAdditionalInformation('token'); $token = $token_array['token']; $email = $token_array['email']; } if (!$token || !$email) { Mage::throwException('Invalid Token'); } $currency = !isset($Currency) ? 'AED' : $Currency; if (file_exists(MAGENTO_ROOT . '/data/currencies.json')) { $currency_json_data = json_decode(file_get_contents(MAGENTO_ROOT . '/data/currencies.json'), 1); $currency_multiplier = $currency_json_data[$currency]; } else { $currency_multiplier = 100; } $amount_in_cents = $amount * $currency_multiplier; $order = $payment->getOrder(); $order_items_array_full = array(); foreach ($order->getAllVisibleItems() as $value) { $order_items_array['title'] = $value->getName(); $order_items_array['amount'] = round($value->getPrice(), 2) * $currency_multiplier; $order_items_array['quantity'] = $value->getQtyOrdered(); array_push($order_items_array_full, $order_items_array); } $shipping_amount = $order->getShippingAmount(); $shipping_amount = $shipping_amount * $currency_multiplier; if (Mage::getSingleton('customer/session')->isLoggedIn()) { $customer = Mage::getSingleton('customer/session')->getCustomer(); $username = $customer->getName(); $registered_at = date(DATE_ISO8601, strtotime($customer->getCreatedAt())); } else { $username = "******"; $registered_at = date(DATE_ISO8601, strtotime(date("Y-m-d H:i:s"))); } $billing_data = $order->getBillingAddress()->getData(); if (is_object($order->getShippingAddress())) { $shipping_data = $order->getShippingAddress()->getData(); $shipping_address = array("first_name" => $shipping_data['firstname'], "last_name" => $shipping_data['lastname'], "country" => $shipping_data['country_id'], "city" => $shipping_data['city'], "address" => $shipping_data['customer_address'], "phone" => $shipping_data['telephone'], "postcode" => $shipping_data['postcode']); } else { $shipping_address = array(); } $billing_address = array("first_name" => $billing_data['firstname'], "last_name" => $billing_data['lastname'], "country" => $billing_data['country_id'], "city" => $billing_data['city'], "address" => $billing_data['customer_address'], "phone" => $billing_data['telephone'], "postcode" => $billing_data['postcode']); $shopping_cart_array = array('user_name' => $username, 'registered_at' => $registered_at, 'items' => $order_items_array_full, 'billing_address' => $billing_address, 'shipping_address' => $shipping_address); $orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId(); $charge_args = array('description' => "Magento charge for " . $email, 'card' => $token, 'currency' => $currency, 'email' => $email, 'ip' => $_SERVER['REMOTE_ADDR'], 'amount' => $amount_in_cents, 'capture' => $capture, 'shipping_amount' => $shipping_amount, 'shopping_cart' => $shopping_cart_array, 'metadata' => array('reference_id' => $orderId)); $ver = new Mage(); $version = $ver->getVersion(); $userAgent = 'Magento ' . $version . ' / Start Plugin ' . self::PLUGIN_VERSION; Start::setUserAgent($userAgent); $method = $payment->getMethodInstance(); if ($method->getConfigData('test_mode') == 1) { Start::setApiKey($method->getConfigData('test_secret_key')); } else { Start::setApiKey($method->getConfigData('live_secret_key')); } try { // Charge the token $charge = Start_Charge::create($charge_args); //need to process charge as success or failed $payment->setTransactionId($charge["id"]); if ($capture) { $payment->setIsTransactionClosed(1); } else { $payment->setIsTransactionClosed(0); } } catch (Start_Error $e) { $error_code = $e->getErrorCode(); if ($error_code === "card_declined") { $errorMsg = 'Charge was declined. Please, contact you bank for more information or use a different card.'; } else { $errorMsg = $e->getMessage(); } throw new Mage_Payment_Model_Info_Exception($errorMsg); } //need to process charge as success or failed }
public function send() { require_once './vendor/autoload.php'; if ($this->config->get('payfort_start_transaction')) { $capture = FALSE; } else { $capture = TRUE; } if ($this->config->get('payfort_start_test')) { $payfort_start_secret_api = $this->config->get('payfort_start_entry_test_secret_key'); } else { $payfort_start_secret_api = $this->config->get('payfort_start_entry_live_secret_key'); } $token = $_POST['payment_token']; $email = $_POST['payment_email']; $this->load->model('checkout/order'); $order_id = $this->session->data['order_id']; $order = $this->model_checkout_order->getOrder($this->session->data['order_id']); $order_description = "Charge for order"; $amount = $order['total']; if (file_exists(DIR_SYSTEM . '../data/currencies.json')) { $currency_json_data = json_decode(file_get_contents(HTTP_SERVER . 'data/currencies.json'), 1); $currency_multiplier = $currency_json_data[$order['currency_code']]; } else { $currency_multiplier = 100; } $amount_in_cents = $amount * $currency_multiplier; $version = "0.2"; $billing_address = array("first_name" => $order['payment_firstname'], "last_name" => $order['payment_lastname'], "country" => $order['payment_country'], "city" => $order['payment_city'], "address_1" => $order['payment_address_1'], "address_2" => $order['payment_address_2'], "phone" => $order['telephone'], "postcode" => $order['payment_postcode']); if ($this->cart->hasShipping()) { $shipping_address = array("first_name" => $order['shipping_firstname'], "last_name" => $order['shipping_lastname'], "country" => $order['shipping_country'], "city" => $order['shipping_city'], "address_1" => $order['shipping_address_1'], "address_2" => $order['shipping_address_2'], "phone" => $order['telephone'], "postcode" => $order['shipping_postcode']); } else { $shipping_address = $billing_address; } if ($order['customer_id'] != 0) { $this->load->model('account/customer'); $customer_info = $this->model_account_customer->getCustomer($this->customer->getId()); } $user_name = $order['customer_id'] == 0 ? "guest" : $customer_info['firstname']; $registered_at = $order['customer_id'] == 0 ? date(DATE_ISO8601, strtotime(date("Y-m-d H:i:s"))) : date(DATE_ISO8601, strtotime($customer_info['date_added'])); $products = $this->cart->getProducts(); $order_items_array_full = array(); foreach ($products as $key => $items) { $order_items_array['title'] = $items['name']; $order_items_array['amount'] = $items['price']; $order_items_array['quantity'] = $items['quantity']; array_push($order_items_array_full, $order_items_array); } $shopping_cart_array = array('user_name' => $user_name, 'registered_at' => $registered_at, 'items' => $order_items_array_full, 'billing_address' => $billing_address, 'shipping_address' => $shipping_address); $userAgent = 'Opencart ' . VERSION . ' / Start Plugin ' . $version; Start::setUserAgent($userAgent); Start::setApiKey($payfort_start_secret_api); $json = array(); try { $charge_args = array('description' => $order_description . ': ' . $order_id, 'card' => $token, 'currency' => $order['currency_code'], 'email' => $email, 'ip' => $_SERVER["REMOTE_ADDR"], 'amount' => $amount_in_cents, 'capture' => $capture, 'shopping_cart' => $shopping_cart_array, 'metadata' => array('reference_id' => $order_id)); $charge = Start_Charge::create($charge_args); $this->model_checkout_order->confirm($order_id, $this->config->get('config_order_status_id')); $this->model_checkout_order->update($order_id, $this->config->get('payfort_start_order_status_id'), 'Charge added: ' . $order_id, false); $json['success'] = $this->url->link('checkout/success'); } catch (Start_Error_Banking $e) { if ($e->getErrorCode() == "card_declined") { $json['error'] = "Card declined. Please use another card"; } else { $json['error'] = $e->getMessage(); } } $this->response->setOutput(json_encode($json)); }
$start_payments_secret_api = Tools::safeOutput(Configuration::get('PAYFORT_START_TEST_SECRET_KEY')); } else { $start_payments_secret_api = Tools::safeOutput(Configuration::get('PAYFORT_START_LIVE_SECRET_KEY')); } if (Tools::safeOutput(Configuration::get('PAYFORT_START_CAPTURE'))) { $capture = 0; } else { $capture = 1; } $order_description = "Charge for order"; $order_id = $_POST['x_invoice_num']; $email = $_POST['payment_email']; $amount = $_POST['amount']; include dirname(__FILE__) . '/vendor/payfort/start/Start.php'; $userAgent = 'Prestashop ' . _PS_VERSION_ . ' / Start Plugin ' . $payfortstart->version; Start::setUserAgent($userAgent); Start::setApiKey($start_payments_secret_api); $delivery_address = new Address(intval($cart->id_address_delivery)); $shipping_address = array("first_name" => $delivery_address->firstname, "last_name" => $delivery_address->lastname, "country" => $delivery_address->country, "city" => $delivery_address->city, "address_1" => $delivery_address->address1, "address_2" => $delivery_address->address2, "phone" => $delivery_address->phone, "postcode" => $delivery_address->postcode); $billing_address = array("first_name" => $invoiceAddress->firstname, "last_name" => $invoiceAddress->lastname, "country" => $invoiceAddress->country, "city" => $invoiceAddress->city, "address_1" => $invoiceAddress->address1, "address_2" => $invoiceAddress->address2, "phone" => $invoiceAddress->phone, "postcode" => $invoiceAddress->postcode); if (file_exists(dirname(__FILE__) . '/data/currencies.json')) { $currency_json_data = json_decode(file_get_contents(dirname(__FILE__) . '/data/currencies.json'), 1); $currency_multiplier = $currency_json_data[$currency->iso_code]; } else { $currency_multiplier = 100; } $amount_in_cents = $amount * $currency_multiplier; $customer = new Customer((int) $cart->id_customer); $registered_at = $customer->is_guest == 0 ? date(DATE_ISO8601, strtotime(date("Y-m-d H:i:s"))) : date(DATE_ISO8601, strtotime($customer->date_add)); $products = $cart->getProducts(true); $order_items_array_full = array();