function testCreateSuccess()
 {
     $data = array("amount" => 1050, "currency" => "usd", "email" => "*****@*****.**", "card" => array("number" => "4242424242424242", "exp_month" => 11, "exp_year" => 2016, "cvc" => "123"), "description" => "Charge for test@example.com");
     $result = Start_Charge::create($data);
     $expected = array("id" => "ch_3c513b0dfdc110b11b4091e2cbf6dc23", "amount" => "1050", "currency" => "usd", "description" => "Charge for test@example.com", "state" => "captured", "email" => "*****@*****.**", "ip" => "", "statement_descriptor" => "", "account_id" => "acc_xxx", "captured_amount" => 1050, "refunded_amount" => 0, "failure_reason" => "", "failure_code" => "", "created_at" => "2014-08-14T16:20:53.451+03:00", "updated_at" => "2014-08-14T16:20:53.451+03:00", "object" => "charge", "card" => array());
     $this->assertEquals(sort(array_keys($expected)), sort(array_keys($result)));
     $this->assertNull($result['failure_code']);
 }
 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_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
     $order_description = "Charge for order";
     $amount = $order_info['total'];
     $amount_in_cents = $amount * 100;
     $charge_args = array('description' => $order_description . ': ' . $order_id, 'card' => $token, 'currency' => $order_info['currency_code'], 'email' => $email, 'ip' => $_SERVER["REMOTE_ADDR"], 'amount' => $amount_in_cents, 'capture' => $capture);
     Start::setApiKey($payfort_start_secret_api);
     $json = array();
     try {
         $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));
 }
 /**
  * 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' => '');
     }
 }
 function testMetadata()
 {
     $data = array("amount" => 1050, "currency" => "usd", "email" => "*****@*****.**", "card" => $this->cardForSuccess, "description" => "Charge for test@example.com", "metadata" => array("reference_id" => "1234567890", "tag" => "new"));
     $result = Start_Charge::create($data);
     $this->assertEquals($result["metadata"], array("reference_id" => "1234567890", "tag" => "new"));
 }
 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));
 }
Exemple #6
0
 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
 }
 function testInvalidMonth()
 {
     $data = array("amount" => 1050, "currency" => "usd", "card" => array("number" => "4242424242424242", "exp_month" => 15, "exp_year" => 2015, "cvc" => "123"), "description" => "Charge for test@example.com");
     try {
         $result = Start_Charge::create($data);
     } catch (Start_Error_Request $e) {
         $this->assertEquals('unprocessable_entity', $e->getErrorCode());
     }
 }
Exemple #8
0
<?php

require_once "config.php";
# Read the fields that were automatically submitted by beautiful.js
$token = $_POST["startToken"];
$email = $_POST["startEmail"];
# Setup the Start object with your private API key
Start::setApiKey($api_keys["secret_key"]);
# Process the charge
try {
    $charge = Start_Charge::create(array("amount" => $amount_in_cents, "currency" => $currency, "card" => $token, "email" => $email, "ip" => $_SERVER["REMOTE_ADDR"], "description" => "Charge Description"));
    echo "<h1>Successfully charged 10.00 AED</h1>";
    echo "<p>Charge ID: " . $charge["id"] . "</p>";
    echo "<p>Charge State: " . $charge["state"] . "</p>";
} catch (Start_Error $e) {
    $error_code = $e->getErrorCode();
    $error_message = $e->getMessage();
    /* depending on $error_code we can show different messages */
    if ($error_code === "card_declined") {
        echo "<h1>Charge was declined</h1>";
    } else {
        echo "<h1>Charge was not processed</h1>";
    }
    echo "<p>" . $error_message . "</p>";
}
?>

<a href="index.php">Try Again!</a>
 function setUp()
 {
     $data = array("amount" => 1050, "currency" => "usd", "email" => "*****@*****.**", "card" => $this->cardForSuccess, "description" => "Charge for test@example.com", "capture" => false);
     $this->charge = Start_Charge::create($data);
     $this->assertEquals($this->charge["state"], "authorized");
 }
 function testList()
 {
     $all = Start_Charge::all();
     $this->assertNotEmpty($all["charges"]);
 }
Exemple #11
0
 /**
  * List all created customers
  *
  * @return array list of customers
  * @throws Start_Error_Parameters if any of the parameters is invalid
  * @throws Start_Error_Authentication if the API Key is invalid
  * @throws Start_Error if there is a general error in the API endpoint
  * @throws Exception for any other errors
  */
 public static function all()
 {
     $return_data = Start_Charge::make_request("customer_list");
     return $return_data;
 }
}
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'];
$charge_args = array('description' => $order_description . ': ' . $order_id, 'card' => $_POST['payment_token'], 'currency' => $currency->iso_code, 'email' => $email, 'ip' => $_SERVER["REMOTE_ADDR"], 'amount' => $amount * 100, 'capture' => $capture);
include dirname(__FILE__) . '/vendor/payfort/start/Start.php';
Start::setApiKey($start_payments_secret_api);
$json = array();
try {
    $charge = Start_Charge::create($charge_args);
    $url = 'index.php?controller=order-confirmation&';
    if (_PS_VERSION_ < '1.5') {
        $url = 'order-confirmation.php?';
    }
    $payfortstart->validateOrder((int) $cart->id, Configuration::get('PAYFORT_START_HOLD_REVIEW_OS'), (double) $amount, "payfort start", "message", NULL, NULL, false, $customer->secure_key);
    $auth_order = new Order($payfortstart->currentOrder);
    Tools::redirect($url . 'id_module=' . (int) $payfortstart->id . '&id_cart=' . (int) $cart->id . '&key=' . $auth_order->secure_key);
} catch (Start_Error_Banking $e) {
    if ($e->getErrorCode() == "card_declined") {
        $error_message = "Card declined. Please use another card";
    } else {
        $error_message = $e->getMessage();
    }
    $checkout_type = Configuration::get('PS_ORDER_PROCESS_TYPE') ? 'order-opc' : 'order';
    $url = _PS_VERSION_ >= '1.5' ? 'index.php?controller=' . $checkout_type . '&' : $checkout_type . '.php?';
 /**
  * @expectedException Start_Error_Request
  */
 function testCardException()
 {
     Start::setApiKey('test_sec_k_2b99b969196bece8fa7fd');
     $data = array("amount" => 1050, "currency" => "usd", "card" => array("number" => "4141414141414141", "exp_month" => 11, "exp_year" => 2016, "cvc" => "123"), "description" => "Charge for test@example.com");
     Start_Charge::create($data);
 }