/** * 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); }
/** * Set up the DoExpressCheckoutPayment request * * @link https://developer.paypal.com/docs/classic/express-checkout/integration-guide/ECGettingStarted/#id084RN060BPF * @link https://developer.paypal.com/webapps/developer/docs/classic/api/merchant/DoExpressCheckoutPayment_API_Operation_NVP/ * * @since 3.0.0 * @param \WC_Order $order order object * @param string $type */ private function do_payment(WC_Order $order, $type) { $this->set_method('DoExpressCheckoutPayment'); // set base params $this->add_parameters(array('TOKEN' => $order->paypal_express_token, 'PAYERID' => !empty($order->paypal_express_payer_id) ? $order->paypal_express_payer_id : null, 'BUTTONSOURCE' => 'WooThemes_Cart', 'RETURNFMFDETAILS' => 1)); $order_subtotal = $i = 0; $order_items = array(); // add line items foreach ($order->get_items() as $item) { $product = new WC_Product($item['product_id']); $order_items[] = array('NAME' => SV_WC_Helper::str_truncate(html_entity_decode($product->get_title(), ENT_QUOTES, 'UTF-8'), 127), 'DESC' => $this->get_item_description($item, $product), 'AMT' => $order->get_item_subtotal($item), 'QTY' => !empty($item['qty']) ? absint($item['qty']) : 1, 'ITEMURL' => $product->get_permalink()); $order_subtotal += $item['line_total']; } // add fees foreach ($order->get_fees() as $fee) { $order_items[] = array('NAME' => SV_WC_Helper::str_truncate($fee['name'], 127), 'AMT' => $fee['line_total'], 'QTY' => 1); $order_subtotal += $fee['line_total']; } if (SV_WC_Plugin_Compatibility::is_wc_version_gte_2_3()) { // WC 2.3+, no after-tax discounts if ($order->get_total_discount() > 0) { $order_items[] = array('NAME' => __('Total Discount', WC_Paypal_Express::TEXT_DOMAIN), 'QTY' => 1, 'AMT' => -$order->get_total_discount()); } } else { // WC 2.2 or lesser // add cart discounts as line item if ($order->get_cart_discount() > 0) { $order_items[] = array('NAME' => __('Cart Discount', WC_Paypal_Express::TEXT_DOMAIN), 'QTY' => 1, 'AMT' => -$order->get_cart_discount()); } // add order discounts as line item if ($order->get_order_discount() > 0) { $order_items[] = array('NAME' => __('Order Discount', WC_Paypal_Express::TEXT_DOMAIN), 'QTY' => 1, 'AMT' => -$order->get_order_discount()); } } $total_discount = SV_WC_Plugin_Compatibility::is_wc_version_gte_2_3() ? 0 : $order->get_order_discount(); // order subtotal includes pre-tax discounts in 2.3 if ($this->skip_line_items($order)) { $item_names = array(); foreach ($order_items as $item) { $item_names[] = sprintf('%s x %s', $item['NAME'], $item['QTY']); } // add a single item for the entire order $this->add_line_item_parameters(array('NAME' => sprintf(__('%s - Order', WC_PayPal_Express::TEXT_DOMAIN), get_option('blogname')), 'DESC' => SV_WC_Helper::str_truncate(html_entity_decode(implode(', ', $item_names), ENT_QUOTES, 'UTF-8'), 127), 'AMT' => $order_subtotal - $total_discount + $order->get_cart_tax(), 'QTY' => 1), 0); // add order-level parameters // - Do not sent the TAXAMT due to rounding errors $this->add_payment_parameters(array('AMT' => $order->get_total(), 'CURRENCYCODE' => $order->get_order_currency(), 'ITEMAMT' => $order_subtotal - $total_discount + $order->get_cart_tax(), 'SHIPPINGAMT' => $order->get_total_shipping() + $order->get_shipping_tax(), 'INVNUM' => $order->paypal_express_invoice_prefix . SV_WC_Helper::str_to_ascii(ltrim($order->get_order_number(), _x('#', 'hash before the order number', WC_PayPal_Express::TEXT_DOMAIN))), 'PAYMENTACTION' => $type, 'PAYMENTREQUESTID' => $order->id)); } else { // add individual order items foreach ($order_items as $item) { $this->add_line_item_parameters($item, $i++); } // add order-level parameters $this->add_payment_parameters(array('AMT' => $order->get_total(), 'CURRENCYCODE' => $order->get_order_currency(), 'ITEMAMT' => $order_subtotal - $total_discount, 'SHIPPINGAMT' => $order->get_total_shipping(), 'TAXAMT' => $order->get_total_tax(), 'INVNUM' => $order->paypal_express_invoice_prefix . SV_WC_Helper::str_to_ascii(ltrim($order->get_order_number(), _x('#', 'hash before the order number', WC_PayPal_Express::TEXT_DOMAIN))), 'PAYMENTACTION' => $type, 'PAYMENTREQUESTID' => $order->id)); } }
/** * Get line items to send to paypal * * @param WC_Order $order * @return array on success, or false when it is not possible to send line items */ private function get_line_items($order) { // Do not send lines for tax inclusive prices if ('yes' === get_option('woocommerce_calc_taxes') && 'yes' === get_option('woocommerce_prices_include_tax')) { return false; } // Do not send lines when order discount is present, or too many line items in the order. if ($order->get_order_discount() > 0 || sizeof($order->get_items()) + sizeof($order->get_fees()) >= 9) { return false; } $item_loop = 0; $args = array(); $args['tax_cart'] = $order->get_total_tax(); // Products if (sizeof($order->get_items()) > 0) { foreach ($order->get_items() as $item) { if (!$item['qty']) { continue; } $item_loop++; $product = $order->get_product_from_item($item); $item_name = $item['name']; $item_meta = new WC_Order_Item_Meta($item['item_meta']); if ($meta = $item_meta->display(true, true)) { $item_name .= ' ( ' . $meta . ' )'; } $args['item_name_' . $item_loop] = $this->paypal_item_name($item_name); $args['quantity_' . $item_loop] = $item['qty']; $args['amount_' . $item_loop] = $order->get_item_subtotal($item, false); if ($args['amount_' . $item_loop] < 0) { return false; // Abort - negative line } if ($product->get_sku()) { $args['item_number_' . $item_loop] = $product->get_sku(); } } } // Discount if ($order->get_cart_discount() > 0) { $args['discount_amount_cart'] = round($order->get_cart_discount(), 2); } // Fees if (sizeof($order->get_fees()) > 0) { foreach ($order->get_fees() as $item) { $item_loop++; $args['item_name_' . $item_loop] = $this->paypal_item_name($item['name']); $args['quantity_' . $item_loop] = 1; $args['amount_' . $item_loop] = $item['line_total']; if ($args['amount_' . $item_loop] < 0) { return false; // Abort - negative line } } } // Shipping Cost item - paypal only allows shipping per item, we want to send shipping for the order if ($order->get_total_shipping() > 0) { $item_loop++; $args['item_name_' . $item_loop] = $this->paypal_item_name(sprintf(__('Shipping via %s', 'woocommerce'), $order->get_shipping_method())); $args['quantity_' . $item_loop] = '1'; $args['amount_' . $item_loop] = number_format($order->get_total_shipping(), 2, '.', ''); } return $args; }
/** * ConfirmPayment * * Finalizes the checkout with PayPal's DoExpressCheckoutPayment API * * @FinalPaymentAmt (double) Final payment amount for the order. */ function ConfirmPayment($FinalPaymentAmt) { /* * Display message to user if session has expired. */ if (sizeof(WC()->cart->get_cart()) == 0) { wc_add_notice(sprintf(__('Sorry, your session has expired. <a href=%s>Return to homepage →</a>', 'paypal-for-woocommerce'), '"' . home_url() . '"'), "error"); } /* * Check if the PayPal class has already been established. */ if (!class_exists('Angelleye_PayPal')) { require_once 'lib/angelleye/paypal-php-library/includes/paypal.class.php'; } /* * Create PayPal object. */ $PayPalConfig = array('Sandbox' => $this->testmode == 'yes' ? TRUE : FALSE, 'APIUsername' => $this->api_username, 'APIPassword' => $this->api_password, 'APISignature' => $this->api_signature); $PayPal = new Angelleye_PayPal($PayPalConfig); /* * Get data from WooCommerce object */ if (!empty($this->confirm_order_id)) { $order = new WC_Order($this->confirm_order_id); $invoice_number = preg_replace("/[^0-9,.]/", "", $order->get_order_number()); if ($order->customer_note) { $customer_notes = wptexturize($order->customer_note); } $shipping_first_name = $order->shipping_first_name; $shipping_last_name = $order->shipping_last_name; $shipping_address_1 = $order->shipping_address_1; $shipping_address_2 = $order->shipping_address_2; $shipping_city = $order->shipping_city; $shipping_state = $order->shipping_state; $shipping_postcode = $order->shipping_postcode; $shipping_country = $order->shipping_country; } // Prepare request arrays $DECPFields = array('token' => urlencode($this->get_session('TOKEN')), 'payerid' => urlencode($this->get_session('payer_id')), 'returnfmfdetails' => '', 'giftmessage' => $this->get_session('giftmessage'), 'giftreceiptenable' => $this->get_session('giftreceiptenable'), 'giftwrapname' => $this->get_session('giftwrapname'), 'giftwrapamount' => $this->get_session('giftwrapamount'), 'buyermarketingemail' => '', 'surveyquestion' => '', 'surveychoiceselected' => '', 'allowedpaymentmethod' => ''); $Payments = array(); $Payment = array('amt' => number_format($FinalPaymentAmt, 2, '.', ''), 'currencycode' => get_woocommerce_currency(), 'shippingdiscamt' => '', 'insuranceoptionoffered' => '', 'handlingamt' => '', 'desc' => '', 'custom' => '', 'invnum' => $this->invoice_id_prefix . $invoice_number, 'notifyurl' => '', 'shiptoname' => $shipping_first_name . ' ' . $shipping_last_name, 'shiptostreet' => $shipping_address_1, 'shiptostreet2' => $shipping_address_2, 'shiptocity' => $shipping_city, 'shiptostate' => $shipping_state, 'shiptozip' => $shipping_postcode, 'shiptocountrycode' => $shipping_country, 'shiptophonenum' => '', 'notetext' => $this->get_session('customer_notes'), 'allowedpaymentmethod' => '', 'paymentaction' => 'Sale', 'paymentrequestid' => '', 'sellerpaypalaccountid' => '', 'sellerid' => '', 'sellerusername' => '', 'sellerregistrationdate' => '', 'softdescriptor' => ''); $PaymentOrderItems = array(); $ctr = 0; $ITEMAMT = 0; if (sizeof($order->get_items()) > 0) { foreach ($order->get_items() as $values) { $_product = $order->get_product_from_item($values); $qty = absint($values['qty']); $sku = $_product->get_sku(); $values['name'] = html_entity_decode($values['name'], ENT_NOQUOTES, 'UTF-8'); if ($_product->product_type == 'variation') { if (empty($sku)) { $sku = $_product->parent->get_sku(); } $item_meta = new WC_Order_Item_Meta($values['item_meta']); $meta = $item_meta->display(true, true); if (!empty($meta)) { $values['name'] .= " - " . str_replace(", \n", " - ", $meta); } } /* * Set price based on tax option. */ if (get_option('woocommerce_prices_include_tax') == 'yes') { $product_price = $order->get_item_subtotal($values, true, false); } else { $product_price = $order->get_item_subtotal($values, false, true); } $Item = array('name' => $values['name'], 'desc' => '', 'amt' => $product_price, 'number' => $sku, 'qty' => $qty, 'taxamt' => '', 'itemurl' => '', 'itemcategory' => '', 'itemweightvalue' => '', 'itemweightunit' => '', 'itemheightvalue' => '', 'itemheightunit' => '', 'itemwidthvalue' => '', 'itemwidthunit' => '', 'itemlengthvalue' => '', 'itemlengthunit' => '', 'ebayitemnumber' => '', 'ebayitemauctiontxnid' => '', 'ebayitemorderid' => '', 'ebayitemcartid' => ''); array_push($PaymentOrderItems, $Item); $ITEMAMT += $product_price * $values['qty']; } /** * Add custom Woo cart fees as line items */ foreach (WC()->cart->get_fees() as $fee) { $Item = array('name' => $fee->name, 'desc' => '', 'amt' => number_format($fee->amount, 2, '.', ''), 'number' => $fee->id, 'qty' => 1, 'taxamt' => '', 'itemurl' => '', 'itemcategory' => '', 'itemweightvalue' => '', 'itemweightunit' => '', 'itemheightvalue' => '', 'itemheightunit' => '', 'itemwidthvalue' => '', 'itemwidthunit' => '', 'itemlengthvalue' => '', 'itemlengthunit' => '', 'ebayitemnumber' => '', 'ebayitemauctiontxnid' => '', 'ebayitemorderid' => '', 'ebayitemcartid' => ''); /** * The gift wrap amount actually has its own parameter in * DECP, so we don't want to include it as one of the line * items. */ if ($Item['number'] != 'gift-wrap') { array_push($PaymentOrderItems, $Item); $ITEMAMT += $fee->amount * $Item['qty']; } $ctr++; } /* * Get discounts */ if ($order->get_cart_discount() > 0) { foreach (WC()->cart->get_coupons('cart') as $code => $coupon) { $Item = array('name' => 'Cart Discount', 'number' => $code, 'qty' => '1', 'amt' => '-' . number_format(WC()->cart->coupon_discount_amounts[$code], 2, '.', '')); array_push($PaymentOrderItems, $Item); } $ITEMAMT -= $order->get_cart_discount(); } if ($order->get_order_discount() > 0) { foreach (WC()->cart->get_coupons('order') as $code => $coupon) { $Item = array('name' => 'Order Discount', 'number' => $code, 'qty' => '1', 'amt' => '-' . number_format(WC()->cart->coupon_discount_amounts[$code], 2, '.', '')); array_push($PaymentOrderItems, $Item); } $ITEMAMT -= $order->get_order_discount(); } /* * Set shipping and tax values. */ if (get_option('woocommerce_prices_include_tax') == 'yes') { $shipping = $order->get_total_shipping() + $order->get_shipping_tax(); $tax = 0; } else { $shipping = $order->get_total_shipping(); $tax = $order->get_total_tax(); } /* * Now that we have all items and subtotals * we can fill in necessary values. */ $Payment['itemamt'] = number_format($ITEMAMT, 2, '.', ''); // Required if you specify itemized L_AMT fields. Sum of cost of all items in this order. /* * Set tax */ if ($tax > 0) { $Payment['taxamt'] = number_format($tax, 2, '.', ''); // Required if you specify itemized L_TAXAMT fields. Sum of all tax items in this order. } /* * Set shipping */ if ($shipping > 0) { $Payment['shippingamt'] = number_format($shipping, 2, '.', ''); // Total shipping costs for this order. If you specify SHIPPINGAMT you mut also specify a value for ITEMAMT. } } $Payment['order_items'] = $PaymentOrderItems; array_push($Payments, $Payment); $UserSelectedOptions = array('shippingcalculationmode' => '', 'insuranceoptionselected' => '', 'shippingoptionisdefault' => '', 'shippingoptionamount' => '', 'shippingoptionname' => ''); $PayPalRequestData = array('DECPFields' => $DECPFields, 'Payments' => $Payments); // Pass data into class for processing with PayPal and load the response array into $PayPalResult $PayPalResult = $PayPal->DoExpressCheckoutPayment($PayPalRequestData); /* * Log API result */ $this->add_log('Test Mode: ' . $this->testmode); $this->add_log('Endpoint: ' . $this->API_Endpoint); $PayPalRequest = isset($PayPalResult['RAWREQUEST']) ? $PayPalResult['RAWREQUEST'] : ''; $PayPalResponse = isset($PayPalResult['RAWRESPONSE']) ? $PayPalResult['RAWRESPONSE'] : ''; $this->add_log('Request: ' . print_r($PayPal->NVPToArray($PayPal->MaskAPIResult($PayPalRequest)), true)); $this->add_log('Response: ' . print_r($PayPal->NVPToArray($PayPal->MaskAPIResult($PayPalResponse)), true)); /* * Error handling */ if ($PayPal->APICallSuccessful($PayPalResult['ACK'])) { $this->remove_session('TOKEN'); } /* * Return the class library result array. */ return $PayPalResult; }