public function verify_address( $order, $shipment ) { $this->get_auth(); if( isset( $_POST['address'] ) ) { $order->set_address( $_POST['address'], 'shipping' ); } $params = array( 'Authenticator' => $this->authenticator, 'Address' => $this->get_shipping_address( $order ), ); $check = $this->stamps->CleanseAddress( $params ); if( is_soap_fault( $check ) ) { slp_ajax_functions::error_handler( __FUNCTION__, __CLASS__, __LINE__, $check->faultstring ); } else { if( ! $check->AddressMatch ) { if( $check->CityStateZipOK ) { $valid = false; $message .= 'The City, State, & ZipCode entered are valid; however, no match was found for the street address. Please check the street address for errors.'; } else { $valid = false; $message .= 'The address provided is not valid. Please confirm address information below and resubmit.'; } } else { $valid = true; $message = 'Please review updated address below and click continue to proceed.'; } $temp = array( 'address_1' => $check->Address->Address1, 'address_2' => $check->Address->Address2, 'city' => $check->Address->City, 'state' => $check->Address->State, 'country' => isset( $check->Address->Country ) ? $check->Address->Country : $order->shipping_country, ); if( isset( $check->Address->ZIPCode ) ) { $temp['postcode'] = $check->Address->ZIPCode . ' - ' . $check->Address->ZIPCodeAddOn ; } else if( isset( $check->Address->PostalCode ) ) { $temp['postcode'] = $check->Address->PostalCode; } else { $temp['postcode'] = ''; } $address[] = $temp; $xml = array( 'address' => $address, 'valid' => $valid, 'message' => $message ); slp_ajax_functions::verify_address( $xml, $order, $shipment ); } }
public function verify_address( $order, $shipment ) { $countries = new WC_Countries(); if( ! in_array( $order->shipping_country, $this->domestic ) ) { $xml = array( 'message' => 'UPS does not currently support validation for international addresses. Please ensure the address below is correct and click continue to proceed to complete customs form.', ); } else { $state = strlen( $order->shipping_state ) > 2 ? array_search( ucwords( strtolower( $order->shipping_state ) ), $countries->get_states( $order->shipping_country ) ) : $order->shipping_state; $name = $order->shipping_first_name . ' ' . $order->shipping_last_name ; $request = $this->xml_header(); $request .= "<AddressValidationRequest xml:lang='en-US'> <Request> <TransactionReference> <CustomerContext>Customer Context</CustomerContext> <XpciVersion>1.0</XpciVersion> </TransactionReference> <RequestAction>XAV</RequestAction> <RequestOption>1</RequestOption> </Request> <AddressKeyFormat> <ConsigneeName>$name</ConsigneeName> <AddressLine>$order->shipping_address_1</AddressLine>"; if( ! empty( $order->shipping_address_2 ) ) $request .= " <AddressLine>$order->shipping_address_2</AddressLine>"; $request .= " <PoliticalDivision2>$order->shipping_city</PoliticalDivision2> <PoliticalDivision1>$state</PoliticalDivision1> <PostcodePrimaryLow>$order->shipping_postcode</PostcodePrimaryLow> <CountryCode>$order->shipping_country</CountryCode> </AddressKeyFormat> </AddressValidationRequest>"; $request = str_replace( array( "\n", "\r" ), '', $request ); //send, receive and parse xml response $response = $this->xml_request( $request, $this->endpoints['production'] . 'XAV' ); $count = 1; if( isset( $response->AmbiguousAddressIndicator ) ) { $message = 'An exact match could not be found. Please select an address option below and click continue to proceed.'; foreach( $response->AddressKeyFormat as $candidate ) { $address = array( 'city' => (string)$candidate->PoliticalDivision2, 'state' => (string)$candidate->PoliticalDivision1, 'country' => (string)$candidate->CountryCode, 'postcode' => (string)$candidate->PostcodePrimaryLow . ' - ' . (string)$candidate->PostcodeExtendedLow ); foreach( $candidate->AddressLine as $line ) { $address["address_$count"] = (string)$line; ++$count; } $addresses[] = $address; } $address = $addresses; } else if( isset( $response->ValidAddressIndicator ) ) { $message = 'The address was matched. Please review updated address below and click continue to proceed.'; $block = array( 'city' => (string)$response->AddressKeyFormat->PoliticalDivision2, 'state' => (string)$response->AddressKeyFormat->PoliticalDivision1, 'country' => (string)$response->AddressKeyFormat->CountryCode, 'postcode' => (string)$response->AddressKeyFormat->PostcodePrimaryLow . ' - ' . $response->AddressKeyFormat->PostcodeExtendedLow ); foreach( $response->AddressKeyFormat->AddressLine as $line ) { $block["address_$count"] = (string)$line; ++$count; } $address[] = $block; } else if( isset( $response->NoCandidatesIndicator ) ) { $valid = false; $message .= 'The address provided is not valid. Please confirm address information below and resubmit.'; } $xml = array( 'address' => $address, //'valid' => $valid, 'message' => $message ); } slp_ajax_functions::verify_address( $xml, $order, $shipment );; }