PluginProbe
WooCommerce Square / 3.4.2
WooCommerce Square v3.4.2
5.5.0 5.4.3 5.4.2 5.4.1 5.4.0 trunk 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 All 132 releases
woocommerce-square / includes / Gateway / API / Requests / Customers.php
Customers.php
114 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WooCommerce Square
4 *
5 * This source file is subject to the GNU General Public License v3.0
6 * that is bundled with this package in the file license.txt.
7 * It is also available through the world-wide-web at this URL:
8 * http://www.gnu.org/licenses/gpl-3.0.html
9 * If you did not receive a copy of the license and are unable to
10 * obtain it through the world-wide-web, please send an email
11 * to license@woocommerce.com so we can send you a copy immediately.
12 *
13 * DISCLAIMER
14 *
15 * Do not edit or add to this file if you wish to upgrade WooCommerce Square to newer
16 * versions in the future. If you wish to customize WooCommerce Square for your
17 * needs please refer to https://docs.woocommerce.com/document/woocommerce-square/
18 *
19 * @author WooCommerce
20 * @copyright Copyright: (c) 2019, Automattic, Inc.
21 * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
22 */
23
24 namespace WooCommerce\Square\Gateway\API\Requests;
25 use WooCommerce\Square\API;
26
27 defined( 'ABSPATH' ) || exit;
28
29 /**
30 * The customers API request class.
31 *
32 * @since 2.0.0
33 */
34 class Customers extends API\Requests\Customers {
35
36
37 /**
38 * Sets the data for creating a new customer.
39 *
40 * @since 2.0.0
41 *
42 * @param \WC_Order $order order object
43 */
44 public function set_create_customer_data( \WC_Order $order ) {
45
46 $this->square_api_method = 'createCustomer';
47
48 // set the customer email as the WP user email, if available
49 try {
50
51 if ( ! $order->get_user_id() ) {
52 throw new \Exception( 'No user account' );
53 }
54
55 $customer = new \WC_Customer( $order->get_user_id() );
56
57 $email = $customer->get_email();
58
59 } catch ( \Exception $exception ) { // otherwise, use the order billing email
60
61 $email = $order->get_billing_email();
62 }
63
64 $customer_request = new \Square\Models\CreateCustomerRequest();
65 $customer_request->setGivenName( $order->get_billing_first_name() );
66 $customer_request->setFamilyName( $order->get_billing_last_name() );
67 $customer_request->setCompanyName( $order->get_billing_company() );
68 $customer_request->setEmailAddress( $email );
69 $customer_request->setPhoneNumber( $order->get_billing_phone() );
70
71 if ( $order->get_user_id() ) {
72 $customer_request->setReferenceId( (string) $order->get_user_id() );
73 }
74
75 $customer_request->setAddress( self::get_address_from_order( $order ) );
76
77 $this->square_request = $customer_request;
78
79 $this->square_api_args = array(
80 $this->square_request,
81 );
82 }
83
84 /**
85 * Gets a billing address model from a WC order.
86 *
87 * @since 2.0.0
88 *
89 * @param \WC_Order $order order object
90 * @return \Square\Models\Address
91 */
92 public static function get_address_from_order( \WC_Order $order ) {
93
94 $address = new \Square\Models\Address();
95 $address->setAddressLine1( $order->get_billing_address_1() );
96 $address->setAddressLine2( $order->get_billing_address_2() );
97 $address->setLocality( $order->get_billing_city() );
98 $address->setAdministrativeDistrictLevel1( $order->get_billing_state() );
99 if ( ! empty( $order->payment->postcode ) ) {
100 $address->setPostalCode( $order->payment->postcode );
101 } else {
102 $address->setPostalCode( $order->get_billing_postcode() );
103 }
104
105 if ( $order->get_billing_country() ) {
106 $address->setCountry( $order->get_billing_country() );
107 }
108
109 return $address;
110 }
111
112
113 }
114