afterpay_language = apply_filters( 'afterpay_language', $afterpay_language ); $this->afterpay_currency = apply_filters( 'afterpay_currency', $afterpay_currency ); } /** * Payment form on checkout page * * @acces public * @return void */ public function payment_fields() { global $woocommerce; if ( 'yes' === $this->profile_tracking ) { // Get the woocommerce session for profile tracking. $session_id = $woocommerce->session->get_customer_id(); ?> testmode ) : ?>
testmode ) : ?> extra_information ) : ?>extra_information ); ?>
show_phone && ! isset( $_POST[ $this->id . '_phone' ] ) ) { wc_add_notice( __( 'Phone number is a required field', 'afterpay' ), 'error' ); return false; } if ( 'no' === $this->compatibility_germanized && 'yes' === $this->show_gender && ! isset( $_POST[ $this->id . '_gender' ] ) ) { wc_add_notice( __( 'Gender is a required field', 'afterpay' ), 'error' ); return false; } if ( 'yes' === $this->show_termsandconditions && ! isset( $_POST[ $this->id . '_terms' ] ) ) { wc_add_notice( __( 'Please accept the AfterPay terms.', 'afterpay' ), 'error' ); return false; } return true; } /** * Process the payment and return the result * * @access public * @param int $order_id Woocommerce order ID. * @return array **/ public function process_payment( $order_id ) { global $woocommerce; $_tax = new WC_Tax(); $order = wc_get_order( $order_id ); require_once __DIR__ . '/vendor/autoload.php'; // Create AfterPay object. $afterpay = new \Afterpay\Afterpay(); $afterpay->setRest(); // Get values from afterpay form on checkout page. // Set form fields per payment option. // Collect the dob for NL and BE. $afterpay_dob = ''; if ( in_array( $order->get_billing_country(), array( 'NL', 'BE' ), true ) ) { $afterpay_dob_day = isset( $_POST[ $this->id . '_date_of_birth_day' ] ) ? wc_clean( sanitize_text_field( wp_unslash( $_POST[ $this->id . '_date_of_birth_day' ] ) ) ) : ''; $afterpay_dob_month = isset( $_POST[ $this->id . '_date_of_birth_month' ] ) ? wc_clean( sanitize_text_field( wp_unslash( $_POST[ $this->id . '_date_of_birth_month' ] ) ) ) : ''; $afterpay_dob_year = isset( $_POST[ $this->id . '_date_of_birth_year' ] ) ? wc_clean( sanitize_text_field( wp_unslash( $_POST[ $this->id . '_date_of_birth_year' ] ) ) ) : ''; $afterpay_dob = $afterpay_dob_year . '-' . $afterpay_dob_month . '-' . $afterpay_dob_day . 'T00:00:00'; } // Get values from afterpay form on checkout page. // Set form fields per payment option. $afterpay_phone = isset( $_POST[ $this->id . '_phone' ] ) ? wc_clean( sanitize_text_field( wp_unslash( $_POST[ $this->id . '_phone' ] ) ) ) : $order->get_billing_phone(); $afterpay_gender = isset( $_POST[ $this->id . '_gender' ] ) ? wc_clean( sanitize_text_field( wp_unslash( $_POST[ $this->id . '_gender' ] ) ) ) : ''; // Set POST data for Installments profile and bankdetails. $afterpay_installment_profile = isset( $_POST[ $this->id . '_installmentplan' ] ) ? wc_clean( sanitize_text_field( wp_unslash( $_POST[ $this->id . '_installmentplan' ] ) ) ) : ''; $afterpay_installment_bankcode = isset( $_POST[ $this->id . '_bankcode' ] ) ? wc_clean( sanitize_text_field( wp_unslash( $_POST[ $this->id . '_bankcode' ] ) ) ) : ''; $afterpay_installment_bankaccount = isset( $_POST[ $this->id . '_bankaccount' ] ) ? wc_clean( sanitize_text_field( wp_unslash( $_POST[ $this->id . '_bankaccount' ] ) ) ) : ''; // Set POST data for Direct Debit bankdetails. $afterpay_bankcode = isset( $_POST[ $this->id . '_bankcode' ] ) ? wc_clean( sanitize_text_field( wp_unslash( $_POST[ $this->id . '_bankcode' ] ) ) ) : ''; $afterpay_bankaccount = isset( $_POST[ $this->id . '_bankaccount' ] ) ? wc_clean( sanitize_text_field( wp_unslash( $_POST[ $this->id . '_bankaccount' ] ) ) ) : ''; // Set POST data for profile tracking. $profile_tracking_id = isset( $_POST[ $this->id . '_session' ] ) ? wc_clean( sanitize_text_field( wp_unslash( $_POST[ $this->id . '_session' ] ) ) ) : ''; // Set POST data for social security number. $afterpay_ssn = isset( $_POST[ $this->id . '_ssn' ] ) ? wc_clean( sanitize_text_field( wp_unslash( $_POST[ $this->id . '_ssn' ] ) ) ) : ''; // Check if Customer Individual Score is enabled and if so set the code. $afterpay_cis_code = ''; if ( 'yes' === $this->customer_individual_score ) { // Check if coupons are used, and if it contains AfterPay CIS code. $afterpay_cis_code = '20'; if ( $order->get_used_coupons() && count( $order->get_used_coupons() ) > 0 ) { foreach ( $order->get_used_coupons() as $coupon_name ) { // Retrieving the coupon ID. $coupon_post_obj = get_page_by_title( $coupon_name, OBJECT, 'shop_coupon' ); $coupon_id = $coupon_post_obj->ID; // Get an instance of WC_Coupon object in an array(necesary to use WC_Coupon methods). $coupons_obj = new WC_Coupon( $coupon_id ); // Afterpay Coupon code. if ( $coupons_obj->meta_exists( 'afterpay_cis_code' ) ) { $afterpay_cis_code = $coupons_obj->get_meta( 'afterpay_cis_code' ); } } } } // Check if Germanized plugin is used for the gender / title. $germanized_billing_title = get_post_meta( $order_id, '_billing_title', true ); if ( 'yes' === $this->compatibility_germanized && null !== $germanized_billing_title ) { switch ( $germanized_billing_title ) { case '1': $afterpay_gender = 'M'; break; case '2': $afterpay_gender = 'V'; break; } } // Split address into House number and House extension. $afterpay_billing_address = $order->get_billing_address_1(); $afterpay_billing_address_2 = $order->get_billing_address_2(); $splitted_address = $this->split_afterpay_address( $afterpay_billing_address, true ); if ( '' !== $afterpay_billing_address_2 && '' === $splitted_address[1] ) { $afterpay_billing_address = $afterpay_billing_address . ' ' . $afterpay_billing_address_2; } $splitted_address = $this->split_afterpay_address( $afterpay_billing_address, true ); $afterpay_billing_address = $splitted_address[0]; $afterpay_billing_house_number = $splitted_address[1]; $afterpay_billing_house_extension = $splitted_address[2]; $afterpay_shipping_address = $order->get_shipping_address_1(); $afterpay_shipping_address_2 = $order->get_shipping_address_2(); $splitted_address = $this->split_afterpay_address( $afterpay_shipping_address, true ); if ( '' !== $afterpay_shipping_address_2 && '' === $splitted_address[1] ) { $afterpay_shipping_address = $afterpay_shipping_address . ' ' . $afterpay_shipping_address_2; } $splitted_address = $this->split_afterpay_address( $afterpay_shipping_address, true ); $afterpay_shipping_address = $splitted_address[0]; $afterpay_shipping_house_number = $splitted_address[1]; $afterpay_shipping_house_extension = $splitted_address[2]; // If special field is being used for housenumber then use that field. if ( '' !== $this->settings['use_custom_housenumber_field'] ) { $afterpay_billing_house_number = isset( $_POST[ 'billing_' . $this->settings['use_custom_housenumber_field'] ] ) ? wc_clean( sanitize_text_field( wp_unslash( $_POST[ 'billing_' . $this->settings['use_custom_housenumber_field'] ] ) ) ) : $afterpay_billing_house_number; $afterpay_shipping_house_number = isset( $_POST[ 'shipping_' . $this->settings['use_custom_housenumber_field'] ] ) ? wc_clean( sanitize_text_field( wp_unslash( $_POST[ 'shipping_' . $this->settings['use_custom_housenumber_field'] ] ) ) ) : $afterpay_shipping_house_number; } // If special field is being used for housenumber addition then use that field. if ( '' !== $this->settings['use_custom_housenumber_addition_field'] ) { $afterpay_billing_house_extension = isset( $_POST[ 'billing_' . $this->settings['use_custom_housenumber_addition_field'] ] ) ? wc_clean( sanitize_text_field( wp_unslash( $_POST[ 'billing_' . $this->settings['use_custom_housenumber_addition_field'] ] ) ) ) : $afterpay_billing_house_extension; $afterpay_shipping_house_extension = isset( $_POST[ 'shipping_' . $this->settings['use_custom_housenumber_addition_field'] ] ) ? wc_clean( sanitize_text_field( wp_unslash( $_POST[ 'shipping_' . $this->settings['use_custom_housenumber_addition_field'] ] ) ) ) : $afterpay_shipping_house_extension; } // Get connection mode. $afterpay_mode = $this->get_connection_mode(); $authorisation['apiKey'] = $this->settings['api_key']; // Create the order. // Cart Contents. if ( count( $order->get_items() ) > 0 ) { foreach ( $order->get_items() as $item ) { // Get product to retrieve sku or product id. $_product = $item->get_product(); // Get SKU or product id. if ( $_product->get_sku() ) { $sku = $_product->get_sku(); } else { $sku = $_product->get_id(); } $item_tax_category = $this->get_afterpay_tax_class( $order->get_line_total( $item, false ), $order->get_line_tax( $item ) ); $item_tax_amount = round( $order->get_line_tax( $item ) / $item['qty'], 4 ); // Apply_filters to item price so we can filter this if needed. $afterpay_item_price_including_tax = $order->get_item_total( $item, true ); $item_price = apply_filters( 'afterpay_item_price_including_tax', $afterpay_item_price_including_tax ); $item_price = round( $item_price * 100, 0 ); // Get the product url. $product_url = get_permalink( $_product->get_id() ); // Get the product image url, if not available return empty string. $image_url = wp_get_attachment_image_url( $_product->get_image_id(), 'full' ); if ( false === $image_url ) { $image_url = ''; } $afterpay->create_order_line( $sku, $item['name'], $item['qty'], $item_price, $item_tax_category, $item_tax_amount, null, // $googleProductCategoryId null, // $googleProductCategory $product_url, $image_url ); } } // Shipping. if ( $order->get_shipping_total() > 0 ) { // We manually calculate the shipping tax percentage here. $calculated_shipping_tax_percentage = ( $order->get_shipping_tax() / $order->get_shipping_total() ) * 100; $calculated_shipping_tax_decimal = ( $order->get_shipping_tax() / $order->get_shipping_total() ) + 1; $shipping_tax_rate = $this->get_afterpay_tax_class( $order->get_total_shipping(), $order->get_shipping_tax() ); // Apply_filters to Shipping so we can filter this if needed. $afterpay_shipping_price_including_tax = $order->get_shipping_total() * $calculated_shipping_tax_decimal; $shipping_price = apply_filters( 'afterpay_shipping_price_including_tax', $afterpay_shipping_price_including_tax ); $shipping_sku = __( 'Shipping', 'afterpay' ); $shipping_description = __( 'Shipping on order', 'afterpay' ); $shipping_price = round( $shipping_price * 100, 0 ); $shipping_tax = $order->get_shipping_tax(); $afterpay->create_order_line( $shipping_sku, $shipping_description, 1, $shipping_price, $shipping_tax_rate, $shipping_tax ); } $fees = $woocommerce->cart->get_fees(); if ( count( $fees ) > 0 ) { foreach ( $fees as $fee ) { $fee_sku = __( 'Service Fee', 'afterpay' ); $fee_description = $fee->name; $fee_price = round( ( $fee->amount + $fee->tax ) * 100 ); $afterpay->create_order_line( $fee_sku, $fee_description, 1, $fee_price, 1, $fee->tax ); } } $aporder['billtoaddress']['city'] = $order->get_billing_city(); $aporder['billtoaddress']['housenumber'] = $afterpay_billing_house_number; $aporder['billtoaddress']['housenumberaddition'] = $afterpay_billing_house_extension; $aporder['billtoaddress']['isocountrycode'] = $order->get_billing_country(); $aporder['billtoaddress']['postalcode'] = $order->get_billing_postcode(); $aporder['billtoaddress']['referenceperson']['email'] = $order->get_billing_email(); if ( '' !== $afterpay_gender ) { $aporder['billtoaddress']['referenceperson']['gender'] = $afterpay_gender; } if ( '' !== $afterpay_dob ) { $aporder['billtoaddress']['referenceperson']['dob'] = $afterpay_dob; } $aporder['billtoaddress']['referenceperson']['firstname'] = $order->get_billing_first_name(); $aporder['billtoaddress']['referenceperson']['isolanguage'] = $this->afterpay_language; $aporder['billtoaddress']['referenceperson']['lastname'] = $order->get_billing_last_name(); $aporder['billtoaddress']['referenceperson']['phonenumber'] = $afterpay_phone; $aporder['billtoaddress']['streetname'] = $afterpay_billing_address; // Check if social security number is set, if so put it in the billtoaddress. if ( '' !== $afterpay_ssn ) { $aporder['billtoaddress']['referenceperson']['ssn'] = $afterpay_ssn; } // Shipping address. if ( '' === $order->get_shipping_method() ) { // Use billing address if Shipping is disabled in Woocommerce. $aporder['shiptoaddress'] = $aporder['billtoaddress']; } else { $aporder['shiptoaddress']['city'] = $order->get_shipping_city(); $aporder['shiptoaddress']['housenumber'] = $afterpay_shipping_house_number; $aporder['shiptoaddress']['housenumberaddition'] = $afterpay_shipping_house_extension; $aporder['shiptoaddress']['isocountrycode'] = $order->get_shipping_country(); $aporder['shiptoaddress']['postalcode'] = $order->get_shipping_postcode(); $aporder['shiptoaddress']['referenceperson']['email'] = $order->get_billing_email(); if ( '' !== $afterpay_gender ) { $aporder['shiptoaddress']['referenceperson']['gender'] = $afterpay_gender; } if ( '' !== $afterpay_dob ) { $aporder['shiptoaddress']['referenceperson']['dob'] = $afterpay_dob; } $aporder['shiptoaddress']['referenceperson']['firstname'] = $order->get_shipping_first_name(); $aporder['shiptoaddress']['referenceperson']['isolanguage'] = $this->afterpay_language; $aporder['shiptoaddress']['referenceperson']['lastname'] = $order->get_shipping_last_name(); $aporder['shiptoaddress']['referenceperson']['phonenumber'] = $afterpay_phone; $aporder['shiptoaddress']['streetname'] = $afterpay_shipping_address; } $aporder['ordernumber'] = filter_var( $order->get_order_number(), FILTER_SANITIZE_NUMBER_INT ); $aporder['currency'] = $this->afterpay_currency; $aporder['ipaddress'] = $this->get_afterpay_client_ip(); // Check if direct debit data is set, if so add to the $aporder array. if ( '' !== $afterpay_bankcode ) { $aporder['directDebit']['bankCode'] = $afterpay_bankcode; $aporder['directDebit']['bankAccount'] = $afterpay_bankaccount; } // Check if installment data is set, if so add to the $aporder array. if ( '' !== $afterpay_installment_profile ) { $aporder['installment']['profileNo'] = $afterpay_installment_profile; $aporder['installment']['bankCode'] = $afterpay_installment_bankcode; $aporder['installment']['bankAccount'] = $afterpay_installment_bankaccount; // Unset direct debit data. unset( $aporder['directDebit'] ); } // Check if profile tracking data is set, if so add to the $aporder array. if ( '' !== $profile_tracking_id ) { $aporder['profileTrackingId'] = $profile_tracking_id; } // Check if customerIndividualScore is set, if so add to the $aporder array. if ( '' !== $afterpay_cis_code ) { $aporder['customerIndividualScore'] = $afterpay_cis_code; } // Chef if the Merchant ID is set, if so add to the $aporder array. if ( '' !== $this->merchantid ) { $aporder['apiMerchantId'] = $this->merchantid; } try { // Transmit all the specified data, from the steps above, to afterpay. $afterpay->set_order( $aporder, $this->order_type ); $afterpay->do_request( $authorisation, $afterpay_mode ); $this->send_afterpay_debug_mail( $afterpay ); // Retreive response. if ( isset( $afterpay->order_result->return->statusCode ) ) { switch ( $afterpay->order_result->return->statusCode ) { case 'A': // If capturing is enabled, and way of capture is set. // to automatically after authorization, then capture the full order. if ( isset( $this->settings['captures'] ) && 'yes' === $this->settings['captures'] && isset( $this->settings['captures_way'] ) && 'auto_after_authorization' === $this->settings['captures_way'] ) { $order->add_order_note( __( 'AfterPay payment completed.', 'afterpay' ) ); // Capture payment. $this->capture_afterpay_payment( null, $order ); } if ( isset( $this->settings['captures'] ) && 'yes' === $this->settings['captures'] && isset( $this->settings['captures_way'] ) && 'auto_after_authorization' !== $this->settings['captures_way'] ) { // Add note that the order is not captured yet. $order->add_order_note( __( 'AfterPay capture needed, since the Capture mode was set to(Based on Woocommerce Status) when the order was placed.', 'afterpay' ) ); } // Payment complete. $order->payment_complete(); // Remove cart. $woocommerce->cart->empty_cart(); // Return thank you redirect. return array( 'result' => 'success', 'redirect' => $this->get_return_url( $order ), ); case 'P': $order->add_order_note( __( 'AfterPay payment pending.', 'afterpay' ) ); // Payment complete. $order->update_status( 'on-hold', __( 'Awaiting AfterPay payment', 'afterpay' ) ); // Remove cart. $woocommerce->cart->empty_cart(); // Return thank you redirect. return array( 'result' => 'success', 'redirect' => $afterpay->order_result->return->extrafields->valueField, ); case 'W': // Order is denied, store it in a database. $order->add_order_note( __( 'AfterPay payment denied.', 'afterpay' ) ); $message = isset( $afterpay->order_result->return->messages->message ) ? $afterpay->order_result->return->messages->message : ''; // Check if description failure isset, else set rejectCode to return default rejection message. if ( isset( $afterpay->order_result->return->riskCheckMessages ) && isset( $afterpay->order_result->return->riskCheckMessages[0]->customerFacingMessage ) && isset( $afterpay->order_result->return->riskCheckMessages[0]->code ) ) { // Get the rejection message from REST. $message = $afterpay->order_result->return->riskCheckMessages[0]->customerFacingMessage; // If the rejection is an address correction, show the address correction. if ( in_array( $afterpay->order_result->return->riskCheckMessages[0]->code, array( '200.101', '200.103', '200.104' ), true ) && isset( $afterpay->order_result->return->customer->addressList[0] ) && is_object( $afterpay->order_result->return->customer->addressList[0] ) ) { $new_address = $afterpay->order_result->return->customer->addressList[0]; $message = $afterpay->order_result->return->riskCheckMessages[0]->customerFacingMessage; $message .= '