| 1 |
<?php |
| 2 |
|
| 3 |
namespace Payplug\PayplugWoocommerce\Gateway; |
| 4 |
|
| 5 |
// Exit if accessed directly |
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
use libphonenumber\PhoneNumberFormat; |
| 11 |
use libphonenumber\PhoneNumberType; |
| 12 |
use libphonenumber\PhoneNumberUtil; |
| 13 |
use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper; |
| 14 |
|
| 15 |
class PayplugAddressData { |
| 16 |
|
| 17 |
const DELIVERY_TYPE_BILLING = 'BILLING'; |
| 18 |
const DELIVERY_TYPE_VERIFIED = 'VERIFIED'; |
| 19 |
const DELIVERY_TYPE_NEW = 'NEW'; |
| 20 |
const DELIVERY_TYPE_SHIP_TO_STORE = 'SHIP_TO_STORE'; |
| 21 |
const DELIVERY_TYPE_DIGITAL_GOODS = 'DIGITAL_GOODS'; |
| 22 |
const DELIVERY_TYPE_TRAVEL_OR_EVENT = 'TRAVEL_OR_EVENT'; |
| 23 |
const DELIVERY_TYPE_OTHER = 'OTHER'; |
| 24 |
|
| 25 |
public static $address_fields = [ |
| 26 |
'address1', |
| 27 |
'address2', |
| 28 |
'postcode', |
| 29 |
'city', |
| 30 |
'state', |
| 31 |
'country' |
| 32 |
]; |
| 33 |
|
| 34 |
/** |
| 35 |
* @var array |
| 36 |
*/ |
| 37 |
protected $billing; |
| 38 |
|
| 39 |
/** |
| 40 |
* @var array |
| 41 |
*/ |
| 42 |
protected $shipping; |
| 43 |
|
| 44 |
/** |
| 45 |
* Get address data from order. |
| 46 |
* |
| 47 |
* @param $order |
| 48 |
* |
| 49 |
* @return PayplugAddressData |
| 50 |
* @throws \InvalidArgumentException |
| 51 |
*/ |
| 52 |
public static function from_order( $order ) { |
| 53 |
if ( ! is_a( $order, '\WC_Order' ) ) { |
| 54 |
throw new \InvalidArgumentException( |
| 55 |
sprintf( 'Expected WC_Order object, got %s', get_class( $order ) ) |
| 56 |
); |
| 57 |
} |
| 58 |
|
| 59 |
$address_data = new PayplugAddressData(); |
| 60 |
$address_data->prepare_address_data( $order ); |
| 61 |
|
| 62 |
return $address_data; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Check if an address has already been used by a customer |
| 67 |
* |
| 68 |
* @param int $customer |
| 69 |
* @param string $hash |
| 70 |
* |
| 71 |
* @return bool |
| 72 |
*/ |
| 73 |
public static function address_already_used( $customer, $hash ) { |
| 74 |
$hash_list = self::get_customer_addresses_hash( $customer ); |
| 75 |
|
| 76 |
return ! empty( $hash_list ) && in_array( $hash, $hash_list ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Get the list of hash of all addresses used by the customer |
| 81 |
* |
| 82 |
* @param int $customer |
| 83 |
* |
| 84 |
* @return array |
| 85 |
*/ |
| 86 |
public static function get_customer_addresses_hash( $customer ) { |
| 87 |
$hash_list = get_user_meta( $customer, 'payplug_addresses_hash', true ); |
| 88 |
|
| 89 |
return is_array( $hash_list ) ? $hash_list : []; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Update the list of hash of all addresses used by the customer |
| 94 |
* |
| 95 |
* @param int $customer |
| 96 |
* @param array $hash_list |
| 97 |
* |
| 98 |
* @return bool |
| 99 |
*/ |
| 100 |
public static function update_customer_addresses_hash( $customer, $hash_list ) { |
| 101 |
$result = update_user_meta( $customer, 'payplug_addresses_hash', $hash_list ); |
| 102 |
|
| 103 |
return false !== $result; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Hash an address |
| 108 |
* |
| 109 |
* @param array $address |
| 110 |
* |
| 111 |
* @return string |
| 112 |
*/ |
| 113 |
public static function hash_address( $address ) { |
| 114 |
|
| 115 |
$to_hash = ''; |
| 116 |
foreach ( self::$address_fields as $field ) { |
| 117 |
if ( empty( $address[ $field ] ) ) { |
| 118 |
continue; |
| 119 |
} |
| 120 |
|
| 121 |
$to_hash .= strtolower( remove_accents( $address[ $field ] ) ); |
| 122 |
} |
| 123 |
|
| 124 |
return md5( $to_hash ); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Get billing data. |
| 129 |
* |
| 130 |
* @return array|null |
| 131 |
*/ |
| 132 |
public function get_billing() { |
| 133 |
return ( ! empty( $this->billing ) ) ? $this->billing : null; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Get shipping data. |
| 138 |
* |
| 139 |
* @return array|null |
| 140 |
*/ |
| 141 |
public function get_shipping() { |
| 142 |
return ( ! empty( $this->shipping ) ) ? $this->shipping : null; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Prepare data for billing and shipping. |
| 147 |
* |
| 148 |
* @param \WC_Order $order |
| 149 |
*/ |
| 150 |
protected function prepare_address_data( $order ) { |
| 151 |
|
| 152 |
$customer = PayplugWoocommerceHelper::is_pre_30() ? $order->customer_user : $order->get_customer_id(); |
| 153 |
|
| 154 |
$billing_first_name = PayplugWoocommerceHelper::is_pre_30() ? $order->billing_first_name : $order->get_billing_first_name(); |
| 155 |
$billing_last_name = PayplugWoocommerceHelper::is_pre_30() ? $order->billing_last_name : $order->get_billing_last_name(); |
| 156 |
$billing_email = PayplugWoocommerceHelper::is_pre_30() ? $order->billing_email : $order->get_billing_email(); |
| 157 |
$billing_address1 = PayplugWoocommerceHelper::is_pre_30() ? $order->billing_address_1 : $order->get_billing_address_1(); |
| 158 |
$billing_address2 = PayplugWoocommerceHelper::is_pre_30() ? $order->billing_address_2 : $order->get_billing_address_2(); |
| 159 |
$billing_postcode = PayplugWoocommerceHelper::is_pre_30() ? $order->billing_postcode : $order->get_billing_postcode(); |
| 160 |
$billing_city = PayplugWoocommerceHelper::is_pre_30() ? $order->billing_city : $order->get_billing_city(); |
| 161 |
$billing_country = PayplugWoocommerceHelper::is_pre_30() ? $order->billing_country : $order->get_billing_country(); |
| 162 |
if ( ! PayplugWoocommerceHelper::is_country_supported( $billing_country ) ) { |
| 163 |
$billing_country = PayplugWoocommerceHelper::get_default_country(); |
| 164 |
} |
| 165 |
|
| 166 |
$this->billing = [ |
| 167 |
'first_name' => $this->limit_length( $billing_first_name ), |
| 168 |
'last_name' => $this->limit_length( $billing_last_name ), |
| 169 |
'email' => $this->limit_length( $billing_email, 255 ), |
| 170 |
'address1' => $this->limit_length( $billing_address1, 255 ), |
| 171 |
'postcode' => $this->limit_length( $billing_postcode, 16 ), |
| 172 |
'city' => $this->limit_length( $billing_city ), |
| 173 |
'country' => $this->limit_length( $billing_country, 2 ), |
| 174 |
]; |
| 175 |
|
| 176 |
if ( ! empty( $billing_address2 ) ) { |
| 177 |
$this->billing['address2'] = $this->limit_length( $billing_address2, 255 ); |
| 178 |
} |
| 179 |
|
| 180 |
// We need to know if current cart contain products we will be shipped to the customer |
| 181 |
// to set the correct `delivery_type` value |
| 182 |
if ( WC()->cart->needs_shipping() ) { |
| 183 |
$has_shipping_address = PayplugWoocommerceHelper::is_pre_30() |
| 184 |
? $order->shipping_address_1 |
| 185 |
: $order->get_shipping_address_1(); |
| 186 |
|
| 187 |
if ( $has_shipping_address && ! wc_ship_to_billing_address_only() ) { |
| 188 |
$shipping_first_name = PayplugWoocommerceHelper::is_pre_30() ? $order->shipping_first_name : $order->get_shipping_first_name(); |
| 189 |
$shipping_last_name = PayplugWoocommerceHelper::is_pre_30() ? $order->shipping_last_name : $order->get_shipping_last_name(); |
| 190 |
// Using email from billing address since Woocommerce doesn't provide one for the shipping address by default. |
| 191 |
$shipping_email = PayplugWoocommerceHelper::is_pre_30() ? $order->billing_email : $order->get_billing_email(); |
| 192 |
$shipping_address1 = PayplugWoocommerceHelper::is_pre_30() ? $order->shipping_address_1 : $order->get_shipping_address_1(); |
| 193 |
$shipping_address2 = PayplugWoocommerceHelper::is_pre_30() ? $order->shipping_address_2 : $order->get_shipping_address_2(); |
| 194 |
$shipping_postcode = PayplugWoocommerceHelper::is_pre_30() ? $order->shipping_postcode : $order->get_shipping_postcode(); |
| 195 |
$shipping_city = PayplugWoocommerceHelper::is_pre_30() ? $order->shipping_city : $order->get_shipping_city(); |
| 196 |
$shipping_country = PayplugWoocommerceHelper::is_pre_30() ? $order->shipping_country : $order->get_shipping_country(); |
| 197 |
if ( ! PayplugWoocommerceHelper::is_country_supported( $shipping_country ) ) { |
| 198 |
$shipping_country = PayplugWoocommerceHelper::get_default_country(); |
| 199 |
} |
| 200 |
|
| 201 |
$this->shipping = [ |
| 202 |
'first_name' => $this->limit_length( $shipping_first_name ), |
| 203 |
'last_name' => $this->limit_length( $shipping_last_name ), |
| 204 |
'email' => $this->limit_length( $shipping_email, 255 ), |
| 205 |
'address1' => $this->limit_length( $shipping_address1, 255 ), |
| 206 |
'postcode' => $this->limit_length( $shipping_postcode, 16 ), |
| 207 |
'city' => $this->limit_length( $shipping_city ), |
| 208 |
'country' => $this->limit_length( $shipping_country, 2 ), |
| 209 |
]; |
| 210 |
|
| 211 |
if ( ! empty( $shipping_address2 ) ) { |
| 212 |
$this->shipping['address2'] = $this->limit_length( $shipping_address2, 255 ); |
| 213 |
} |
| 214 |
|
| 215 |
if ( $customer > 0 ) { |
| 216 |
$address_hash = self::hash_address( $this->shipping ); |
| 217 |
$address_already_used = self::address_already_used( $customer, $address_hash ); |
| 218 |
|
| 219 |
// Set the delivery_type to `VERIFIED` if the customer has already used the address before |
| 220 |
// otherwise set it to `NEW` since we have separate shipping address |
| 221 |
$this->shipping['delivery_type'] = ( $address_already_used ) |
| 222 |
? self::DELIVERY_TYPE_VERIFIED |
| 223 |
: self::DELIVERY_TYPE_NEW; |
| 224 |
} else { |
| 225 |
// Set the delivery_type to `NEW` since we have separate shipping address |
| 226 |
$this->shipping['delivery_type'] = self::DELIVERY_TYPE_NEW; |
| 227 |
} |
| 228 |
} else { |
| 229 |
$this->shipping = $this->billing; |
| 230 |
|
| 231 |
if ( $customer > 0 ) { |
| 232 |
$address_hash = self::hash_address( $this->shipping ); |
| 233 |
$address_already_used = self::address_already_used( $customer, $address_hash ); |
| 234 |
|
| 235 |
// Set the delivery_type to `VERIFIED` if the customer has already used the address before |
| 236 |
// otherwise set it to `BILLING` since we reuse the billing address for shipping |
| 237 |
$this->shipping['delivery_type'] = ( $address_already_used ) |
| 238 |
? self::DELIVERY_TYPE_VERIFIED |
| 239 |
: self::DELIVERY_TYPE_BILLING; |
| 240 |
} else { |
| 241 |
// Set the delivery_type to `BILLING` since we reuse the billing address for shipping |
| 242 |
$this->shipping['delivery_type'] = self::DELIVERY_TYPE_BILLING; |
| 243 |
} |
| 244 |
} |
| 245 |
} else { |
| 246 |
// Set the delivery_type to `OTHER` since we dont't need to ship the products |
| 247 |
$this->shipping = $this->billing; |
| 248 |
$this->shipping['delivery_type'] = self::DELIVERY_TYPE_OTHER; |
| 249 |
} |
| 250 |
|
| 251 |
// Maybe set the phone field if available |
| 252 |
$country = PayplugWoocommerceHelper::is_pre_30() ? $order->billing_country : $order->get_billing_country(); |
| 253 |
$phone = PayplugWoocommerceHelper::is_pre_30() ? $order->billing_phone : $order->get_billing_phone(); |
| 254 |
if ( ! empty( $phone ) && ! empty( $country ) ) { |
| 255 |
try { |
| 256 |
$phone_number_util = PhoneNumberUtil::getInstance(); |
| 257 |
$phone_number = $phone_number_util->parse( $phone, $country ); |
| 258 |
|
| 259 |
if ( ! $phone_number_util->isValidNumber( $phone_number ) ) { |
| 260 |
throw new \Exception( 'Invalid phone number' ); |
| 261 |
} |
| 262 |
|
| 263 |
if ( PhoneNumberType::MOBILE === $phone_number_util->getNumberType( $phone_number ) ) { |
| 264 |
$this->billing['mobile_phone_number'] = $phone_number_util->format( $phone_number, PhoneNumberFormat::E164 ); |
| 265 |
} else { |
| 266 |
$this->billing['landline_phone_number'] = $phone_number_util->format( $phone_number, PhoneNumberFormat::E164 ); |
| 267 |
} |
| 268 |
} catch ( \Exception $e ) { |
| 269 |
// Fail to parse phone number. |
| 270 |
// Could be an incorrect number or the number doesn't belong to the billing country. |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Limit string length. |
| 277 |
* |
| 278 |
* @param string $value |
| 279 |
* @param int $maxlength |
| 280 |
* |
| 281 |
* @return string |
| 282 |
*/ |
| 283 |
protected function limit_length( $value, $maxlength = 100 ) { |
| 284 |
if ( ! empty( $value ) ) { |
| 285 |
$value = ( strlen( $value ) > $maxlength ) ? substr( $value, 0, $maxlength ) : $value; |
| 286 |
} |
| 287 |
|
| 288 |
return $value; |
| 289 |
} |
| 290 |
} |
| 291 |
|