$url = "https://www.hygglig.com/"; } //Store data $merchantid = strtoupper($options['merchantid']); $sharedSecret = strtoupper($options['secret']); // Get an instance of the created order $order = new WC_Order($order_id); //Update of Hygglig if (isset($storedGet['token'])) { $updateCheckoutAPI = $url . 'Checkout/api/Checkout/UpdateOrder'; if (sizeof($woocommerce->cart->get_cart()) > 0) { $cart = array(); $totalAmount = 0; WC()->session->set('chosen_payment_method', 'hygglig_checkout'); //Recalc all totals $order->set_total(WC()->cart->shipping_total, 'shipping'); $order->set_total(WC()->cart->get_cart_discount_total(), 'cart_discount'); $order->set_total(WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax'); $order->set_total(WC()->cart->tax_total, 'tax'); $order->set_total(WC()->cart->shipping_tax_total, 'shipping_tax'); $order->set_total(WC()->cart->total); // Cart Contents if (sizeof($order->get_items()) > 0) { foreach ($order->get_items() as $item) { if ($item['qty']) { $_product = $order->get_product_from_item($item); // We manually calculate the tax percentage here if ($_product->is_taxable() && $order->get_line_tax($item) > 0) { // Calculate tax percentage $item_tax_percentage = round($order->get_item_tax($item, false) / $order->get_item_total($item, false, false), 2) * 100; } else {
/** * Create an order. Error codes: * 520 - Cannot insert order into the database. * 521 - Cannot get order after creation. * 522 - Cannot update order. * 525 - Cannot create line item. * 526 - Cannot create fee item. * 527 - Cannot create shipping item. * 528 - Cannot create tax item. * 529 - Cannot create coupon item. * * @throws Exception * @param $data Posted data. * @return int|WP_ERROR */ public function create_order($data) { // Give plugins the opportunity to create an order themselves. if ($order_id = apply_filters('woocommerce_create_order', null, $this)) { return $order_id; } try { $order_id = absint(WC()->session->get('order_awaiting_payment')); $cart_hash = md5(json_encode(wc_clean(WC()->cart->get_cart_for_session())) . WC()->cart->total); /** * If there is an order pending payment, we can resume it here so * long as it has not changed. If the order has changed, i.e. * different items or cost, create a new order. We use a hash to * detect changes which is based on cart items + order total. */ if ($order_id && ($order = wc_get_order($order_id)) && $order->has_cart_hash($cart_hash) && $order->has_status(array('pending', 'failed'))) { // Action for 3rd parties. do_action('woocommerce_resume_order', $order_id); // Remove all items - we will re-add them later. $order->remove_order_items(); } else { $order = new WC_Order(); } foreach ($data as $key => $value) { if (is_callable(array($order, "set_{$key}"))) { $order->{"set_{$key}"}($value); } } $order->set_created_via('checkout'); $order->set_cart_hash($cart_hash); $order->set_customer_id(apply_filters('woocommerce_checkout_customer_id', get_current_user_id())); $order->set_currency(get_woocommerce_currency()); $order->set_prices_include_tax('yes' === get_option('woocommerce_prices_include_tax')); $order->set_customer_ip_address(WC_Geolocation::get_ip_address()); $order->set_customer_user_agent(wc_get_user_agent()); $order->set_customer_note(isset($data['order_comments']) ? $data['order_comments'] : ''); $order->set_payment_method($data['payment_method']); $order->set_shipping_total(WC()->cart->shipping_total); $order->set_discount_total(WC()->cart->get_cart_discount_total()); $order->set_discount_tax(WC()->cart->get_cart_discount_tax_total()); $order->set_cart_tax(WC()->cart->tax_total); $order->set_shipping_tax(WC()->cart->shipping_tax_total); $order->set_total(WC()->cart->total); $this->create_order_line_items($order); $this->create_order_fee_lines($order); $this->create_order_shipping_lines($order); $this->create_order_tax_lines($order); $this->create_order_coupon_lines($order); $order_id = $order->save(); // Let plugins add their own meta data. do_action('woocommerce_checkout_update_order_meta', $order_id, $data); return $order_id; } catch (Exception $e) { return new WP_Error('checkout-error', $e->getMessage()); } }
/** * Charge Payment * Method ini digunakan untuk mendapatkan link halaman pembayaran Veritrans * dengan mengirimkan JSON yang berisi data transaksi */ function charge_payment($order_id) { global $woocommerce; $order_items = array(); $cart = $woocommerce->cart; $order = new WC_Order($order_id); // add discount // WC()->cart->add_discount( 'veritrans' ); $cart->add_discount('veritrans'); $order->add_coupon('veritrans', WC()->cart->get_coupon_discount_amount('veritrans'), WC()->cart->get_coupon_discount_tax_amount('veritrans')); $order->set_total(WC()->cart->shipping_total, 'shipping'); $order->set_total(WC()->cart->get_cart_discount_total(), 'cart_discount'); $order->set_total(WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax'); $order->set_total(WC()->cart->tax_total, 'tax'); $order->set_total(WC()->cart->shipping_tax_total, 'shipping_tax'); $order->set_total(WC()->cart->total); // $order->add_coupon('veritrans',10000); // end of add discount Veritrans_Config::$isProduction = $this->environment == 'production' ? true : false; Veritrans_Config::$serverKey = Veritrans_Config::$isProduction ? $this->server_key_v2_production : $this->server_key_v2_sandbox; Veritrans_Config::$is3ds = true; Veritrans_Config::$isSanitized = $this->enable_sanitization == 'yes' ? true : false; $params = array('transaction_details' => array('order_id' => $order_id, 'gross_amount' => 0), 'vtweb' => array()); $enabled_payments = array(); // if ($this->enable_credit_card == 'yes'){ // $enabled_payments[] = 'credit_card'; // } // check enabled payment if ($this->enable_credit_card == 'yes') { $params['vtweb']['enabled_payments'] = 'credit_card'; } if ($this->enable_permata_va == 'yes') { $params['vtweb']['enabled_payments'] = 'bank_transfer'; } // add bin filter $bins = $this->bin_filter; $bins = explode(',', $bins); $params['vtweb']['credit_card_bins'] = $bins; $customer_details = array(); $customer_details['first_name'] = $order->billing_first_name; $customer_details['last_name'] = $order->billing_last_name; $customer_details['email'] = $order->billing_email; $customer_details['phone'] = $order->billing_phone; $billing_address = array(); $billing_address['first_name'] = $order->billing_first_name; $billing_address['last_name'] = $order->billing_last_name; $billing_address['address'] = $order->billing_address_1; $billing_address['city'] = $order->billing_city; $billing_address['postal_code'] = $order->billing_postcode; $billing_address['phone'] = $order->billing_phone; $billing_address['country_code'] = strlen($this->convert_country_code($order->billing_country) != 3) ? 'IDN' : $this->convert_country_code($order->billing_country); $customer_details['billing_address'] = $billing_address; $customer_details['shipping_address'] = $billing_address; if (isset($_POST['ship_to_different_address'])) { $shipping_address = array(); $shipping_address['first_name'] = $order->shipping_first_name; $shipping_address['last_name'] = $order->shipping_last_name; $shipping_address['address'] = $order->shipping_address_1; $shipping_address['city'] = $order->shipping_city; $shipping_address['postal_code'] = $order->shipping_postcode; $shipping_address['phone'] = $order->billing_phone; $shipping_address['country_code'] = strlen($this->convert_country_code($order->shipping_country) != 3) ? 'IDN' : $this->convert_country_code($order->billing_country); $customer_details['shipping_address'] = $shipping_address; } $params['customer_details'] = $customer_details; //error_log(print_r($params,true)); $items = array(); if (sizeof($order->get_items()) > 0) { foreach ($order->get_items() as $item) { if ($item['qty']) { $product = $order->get_product_from_item($item); $veritrans_item = array(); $veritrans_item['id'] = $item['product_id']; $veritrans_item['price'] = $order->get_item_subtotal($item, false); $veritrans_item['quantity'] = $item['qty']; $veritrans_item['name'] = $item['name']; $items[] = $veritrans_item; } } } // Shipping fee if ($order->get_total_shipping() > 0) { $items[] = array('id' => 'shippingfee', 'price' => $order->get_total_shipping(), 'quantity' => 1, 'name' => 'Shipping Fee'); } // Tax if ($order->get_total_tax() > 0) { $items[] = array('id' => 'taxfee', 'price' => $order->get_total_tax(), 'quantity' => 1, 'name' => 'Tax'); } // Discount if ($order->get_cart_discount() > 0) { $items[] = array('id' => 'totaldiscount', 'price' => $order->get_cart_discount() * -1, 'quantity' => 1, 'name' => 'Total Discount'); } // Fees if (sizeof($order->get_fees()) > 0) { $fees = $order->get_fees(); $i = 0; foreach ($fees as $item) { $items[] = array('id' => 'itemfee' . $i, 'price' => $item['line_total'], 'quantity' => 1, 'name' => $item['name']); $i++; } } //calculate gross amount $total_amount = 0; // error_log('print r items[]' . print_r($items,true)); //debugan foreach ($items as $item) { $total_amount += $item['price'] * $item['quantity']; // error_log('|||| Per item[]' . print_r($item,true)); //debugan } $params['transaction_details']['gross_amount'] = $total_amount; // error_log('bni'.$this->enable_bni); // error_log('mandiri'.$this->enable_mandiri); // if($this->enable_bni == 'yes' || $this->enable_mandiri == 'yes') if (false) { $installment_terms = array(); $payment_options = array('installment' => array('required' => true, 'installment_terms' => new stdClass(), 'offline_installment_terms' => array())); // $term_bni = $this->bni_terms; // error_log('term bni '.$term_bni); // $term_bni_array = explode(',' , $term_bni); // if($term_bni == "yes" || $term_bni_array != null) // { // $installment_terms['bni'] = $term_bni_array; // } // $term_mandiri = $this->mandiri_terms; // error_log('term mandiri '.$term_mandiri); // $term_mandiri_array = explode(',' , $term_mandiri); // if($term_mandiri == "yes" || $term_mandiri_array != null) // { // $installment_terms['mandiri'] = $term_mandiri_array; // } $term = $this->installment_terms; error_log('============installment_terms ' . $term); $term_array = explode(',', $term); } // sift through the entire item to ensure that currency conversion is applied if (get_woocommerce_currency() != 'IDR') { foreach ($items as &$item) { $item['price'] = $item['price'] * $this->to_idr_rate; } unset($item); $params['transaction_details']['gross_amount'] *= $this->to_idr_rate; } $params['item_details'] = $items; // if($params['transaction_details']['gross_amount'] >= $this->min_amount) // { // $payment_options['installment']['offline_installment_terms'] = $term_array; // $params['vtweb']['payment_options'] = $payment_options; // } $woocommerce->cart->empty_cart(); error_log(print_r($params, TRUE)); // error_log(json_encode($params)); return Veritrans_VtWeb::getRedirectionUrl($params); }
/** * Test: get_remaining_refund_amount */ function test_get_remaining_refund_amount() { $object = new WC_Order(); $object->set_total(400); $id = $object->save(); wc_create_refund(array('order_id' => $id, 'amount' => '100', 'line_items' => array())); $this->assertEquals(300, $object->get_remaining_refund_amount()); }
/** * Create an order. Error codes: * 520 - Cannot insert order into the database. * 521 - Cannot get order after creation. * 522 - Cannot update order. * 525 - Cannot create line item. * 526 - Cannot create fee item. * 527 - Cannot create shipping item. * 528 - Cannot create tax item. * 529 - Cannot create coupon item. * @throws Exception * @return int|WP_ERROR */ public function create_order() { global $wpdb; // Give plugins the opportunity to create an order themselves if ($order_id = apply_filters('woocommerce_create_order', null, $this)) { return $order_id; } try { // Start transaction if available wc_transaction_query('start'); // Insert or update the post data $order_id = absint(WC()->session->order_awaiting_payment); $cart_hash = md5(json_encode(wc_clean(WC()->cart->get_cart_for_session())) . WC()->cart->total); /** * If there is an order pending payment, we can resume it here so * long as it has not changed. If the order has changed, i.e. * different items or cost, create a new order. We use a hash to * detect changes which is based on cart items + order total. */ if ($order_id && ($order = wc_get_order($order_id)) && $order->has_cart_hash($cart_hash) && $order->has_status(array('pending', 'failed'))) { // Action for 3rd parties. do_action('woocommerce_resume_order', $order_id); // Remove all items - we will re-add them later. $order->remove_order_items(); /** * Not resuming - lets create a new order object. */ } else { $order = new WC_Order(); } $order->set_created_via('checkout'); $order->set_cart_hash($cart_hash); $order->set_customer_id($this->customer_id); $order->set_currency(get_woocommerce_currency()); $order->set_prices_include_tax('yes' === get_option('woocommerce_prices_include_tax')); $order->set_customer_ip_address(WC_Geolocation::get_ip_address()); $order->set_customer_user_agent(wc_get_user_agent()); $order->set_customer_note(isset($this->posted['order_comments']) ? $this->posted['order_comments'] : ''); $order->set_payment_method($this->payment_method); $order->set_shipping_total(WC()->cart->shipping_total); $order->set_discount_total(WC()->cart->get_cart_discount_total()); $order->set_discount_tax(WC()->cart->get_cart_discount_tax_total()); $order->set_cart_tax(WC()->cart->tax_total); $order->set_shipping_tax(WC()->cart->shipping_tax_total); $order->set_total(WC()->cart->total); // Billing and shipping addresses if ($address_keys = array_merge(array_keys($this->checkout_fields['billing']), array_keys($this->checkout_fields['shipping']))) { foreach ($address_keys as $key) { if (is_callable(array($order, "set_{$key}"))) { $order->{"set_{$key}"}($this->get_posted_address_data(str_replace(array('billing_', 'shipping_'), '', $key), strstr($key, 'billing_') ? 'billing' : 'shipping')); } } } // Add line items. foreach (WC()->cart->get_cart() as $cart_item_key => $values) { $product = $values['data']; $item = new WC_Order_Item_Product(array('quantity' => $values['quantity'], 'name' => $product ? $product->get_title() : '', 'tax_class' => $product ? $product->get_tax_class() : '', 'product_id' => $product && isset($product->id) ? $product->id : 0, 'variation_id' => $product && isset($product->variation_id) ? $product->variation_id : 0, 'variation' => $values['variation'], 'subtotal' => $values['line_subtotal'], 'total' => $values['line_total'], 'subtotal_tax' => $values['line_subtotal_tax'], 'total_tax' => $values['line_tax'], 'taxes' => $values['line_tax_data'])); $item->set_backorder_meta(); // Set this to pass to legacy actions @todo remove in future release $item->legacy_values = $values; $item->legacy_cart_item_key = $cart_item_key; $order->add_item($item); } // Add fees foreach (WC()->cart->get_fees() as $fee_key => $fee) { $item = new WC_Order_Item_Fee(array('name' => $fee->name, 'tax_class' => $fee->taxable ? $fee->tax_class : 0, 'total' => $fee->amount, 'total_tax' => $fee->tax, 'taxes' => array('total' => $fee->tax_data))); // Set this to pass to legacy actions @todo remove in future release $item->legacy_fee = $fee; $item->legacy_fee_key = $fee_key; $order->add_item($item); } // Store shipping for all packages foreach (WC()->shipping->get_packages() as $package_key => $package) { if (isset($package['rates'][$this->shipping_methods[$package_key]])) { $shipping_rate = $package['rates'][$this->shipping_methods[$package_key]]; $item = new WC_Order_Item_Shipping(array('method_title' => $shipping_rate->label, 'method_id' => $shipping_rate->id, 'total' => wc_format_decimal($shipping_rate->cost), 'taxes' => $shipping_rate->taxes, 'meta_data' => $shipping_rate->get_meta_data())); // Set this to pass to legacy actions @todo remove in future release $item->legacy_package_key = $package_key; $order->add_item($item); } } // Store tax rows foreach (array_keys(WC()->cart->taxes + WC()->cart->shipping_taxes) as $tax_rate_id) { if ($tax_rate_id && apply_filters('woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated') !== $tax_rate_id) { $order->add_item(new WC_Order_Item_Tax(array('rate_id' => $tax_rate_id, 'tax_total' => WC()->cart->get_tax_amount($tax_rate_id), 'shipping_tax_total' => WC()->cart->get_shipping_tax_amount($tax_rate_id), 'rate_code' => WC_Tax::get_rate_code($tax_rate_id), 'label' => WC_Tax::get_rate_label($tax_rate_id), 'compound' => WC_Tax::is_compound($tax_rate_id)))); } } // Store coupons foreach (WC()->cart->get_coupons() as $code => $coupon) { $item = new WC_Order_Item_Coupon(array('code' => $code, 'discount' => WC()->cart->get_coupon_discount_amount($code), 'discount_tax' => WC()->cart->get_coupon_discount_tax_amount($code))); $order->add_item($item); } // Save the order $order_id = $order->save(); // Update user meta $this->update_customer_data(); // Let plugins add their own meta data do_action('woocommerce_checkout_update_order_meta', $order_id, $this->posted); // If we got here, the order was created without problems! wc_transaction_query('commit'); } catch (Exception $e) { // There was an error adding order data! wc_transaction_query('rollback'); return new WP_Error('checkout-error', $e->getMessage()); } return $order_id; }
/** * Successful Payment! **/ function successful_request($posted) { $order = new WC_Order((int) $posted["wooorderid"]); $var = ""; if (strlen($this->md5key) > 0) { foreach ($posted as $key => $value) { if ($key != "hash") { $var .= $value; } } $genstamp = md5($var . $this->md5key); if ($genstamp != $posted["hash"]) { echo "MD5 error"; error_log('MD5 check failed for ePay callback with order_id:' . $posted["wooorderid"]); status_header(500); exit; } } if ($order->has_status('pending')) { // Payment completed $order->add_order_note(__('Callback completed', 'woocommerce-gateway-epay-dk')); if ($this->settings["addfeetoorder"] == "yes") { $order_fee = new stdClass(); $order_fee->id = 'epay_fee'; $order_fee->name = __('Fee', 'woocommerce-gateway-epay-dk'); $order_fee->amount = isset($posted['txnfee']) ? floatval($posted['txnfee'] / 100) : 0; $order_fee->taxable = false; $order_fee->tax = 0; $order_fee->tax_data = array(); $order->add_fee($order_fee); $order->set_total($order->order_total + floatval($posted['txnfee'] / 100)); } $order->payment_complete(); update_post_meta((int) $posted["wooorderid"], 'Transaction ID', $posted["txnid"]); update_post_meta((int) $posted["wooorderid"], 'Card no', $posted["cardno"]); if (isset($posted["subscriptionid"])) { update_post_meta((int) $posted["wooorderid"], 'Subscription ID', $posted["subscriptionid"]); } } echo "OK"; status_header(200); exit; }