PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 2.2.2
WooCommerce Square v2.2.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 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.2.0 3.3.0 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.7.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 4.0.0 4.1.0 4.2.0 4.2.1 4.2.2 4.2.3 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.9.0 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 4.9.8 4.9.9 5.0.0 5.0.1 5.1.0 5.1.1 5.1.2 5.2.0 5.3.0 5.3.1 5.3.2 5.3.3
woocommerce-square / includes / Gateway / API / Requests / Payments.php
woocommerce-square / includes / Gateway / API / Requests Last commit date
Customers.php 6 years ago Orders.php 6 years ago Payments.php 6 years ago Refunds.php 6 years ago Transactions.php 6 years ago
Payments.php
206 lines
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
26 defined( 'ABSPATH' ) || exit;
27
28 use SquareConnect\Api\PaymentsApi;
29 use SquareConnect\Model as SquareModel;
30 use WooCommerce\Square\Utilities;
31
32 /**
33 * The Payments API request class.
34 *
35 * @since 2.2.0
36 */
37 class Payments extends \WooCommerce\Square\API\Request {
38
39 /** @var string location ID */
40 protected $location_id;
41
42 /**
43 * Initializes a new payments request.
44 *
45 * @since 2.2.0
46 * @param string $location_id location ID
47 * @param \SquareConnect\ApiClient $api_client the API client
48 */
49 public function __construct( $location_id, $api_client ) {
50 $this->location_id = $location_id;
51 $this->square_api = new PaymentsApi( $api_client );
52 }
53
54 /**
55 * Sets the data for an authorization/delayed capture.
56 *
57 * @since 2.2.0
58 * @param \WC_Order $order order object
59 */
60 public function set_authorization_data( \WC_Order $order ) {
61 $this->set_charge_data( $order, false );
62 }
63
64 /**
65 * Sets the data for a charge.
66 *
67 * @since 2.2.0
68 *
69 * @param \WC_Order $order
70 * @param bool $capture whether to immediately capture the charge
71 */
72 public function set_charge_data( \WC_Order $order, $capture = true ) {
73
74 $this->square_api_method = 'createPayment';
75 $this->square_request = new SquareModel\CreatePaymentRequest();
76
77 $this->square_request->setIdempotencyKey( wc_square()->get_idempotency_key( $order->unique_transaction_ref ) );
78 $this->square_request->setAmountMoney( Utilities\Money_Utility::amount_to_money( $order->payment_total, $order->get_currency() ) );
79 $this->square_request->setReferenceId( $order->get_order_number() );
80
81 /**
82 * Filters the Square payment order note (legacy filter).
83 *
84 * @since 2.2.0
85 *
86 * @param string $description the order note (description)
87 * @param \WC_Order $order the order object
88 */
89 $description = (string) apply_filters( 'wc_square_payment_order_note', $order->description, $order );
90
91 $this->square_request->setNote( Utilities\String_Utility::truncate( $description, 500 ) );
92
93 $this->square_request->setAutocomplete( $capture );
94
95 if ( ! empty( $order->square_customer_id ) ) {
96 $this->square_request->setCustomerId( $order->square_customer_id );
97 }
98
99 // payment token (card ID) or card nonce (from JS)
100 $this->square_request->setSourceId( ! empty( $order->payment->token ) ? $order->payment->token : $order->payment->nonce );
101
102 // 3DS / SCA verification token (from JS)
103 if ( ! empty( $order->payment->verification_token ) ) {
104 $this->square_request->setVerificationToken( $order->payment->verification_token );
105 }
106
107 if ( ! empty( $this->location_id ) ) {
108 $this->square_request->setLocationId( $this->location_id );
109 }
110
111 $billing_address = new SquareModel\Address();
112 $billing_address->setFirstName( $order->get_billing_first_name() );
113 $billing_address->setLastName( $order->get_billing_last_name() );
114 $billing_address->setOrganization( $order->get_billing_company() );
115 $billing_address->setAddressLine1( $order->get_billing_address_1() );
116 $billing_address->setAddressLine2( $order->get_billing_address_2() );
117 $billing_address->setLocality( $order->get_billing_city() );
118 $billing_address->setAdministrativeDistrictLevel1( $order->get_billing_state() );
119 $billing_address->setPostalCode( $order->get_billing_postcode() );
120 $billing_address->setCountry( $order->get_billing_country() );
121
122 $this->square_request->setBillingAddress( $billing_address );
123
124 if ( $order->get_shipping_address_1( 'edit' ) || $order->get_shipping_address_2( 'edit' ) ) {
125
126 $shipping_address = new SquareModel\Address();
127 $shipping_address->setFirstName( $order->get_shipping_first_name() );
128 $shipping_address->setLastName( $order->get_shipping_last_name() );
129 $shipping_address->setAddressLine1( $order->get_shipping_address_1() );
130 $shipping_address->setAddressLine2( $order->get_shipping_address_2() );
131 $shipping_address->setLocality( $order->get_shipping_city() );
132 $shipping_address->setAdministrativeDistrictLevel1( $order->get_shipping_state() );
133 $shipping_address->setPostalCode( $order->get_shipping_postcode() );
134 $shipping_address->setCountry( $order->get_shipping_country() );
135
136 $this->square_request->setShippingAddress( $shipping_address );
137 }
138
139 $this->square_request->setBuyerEmailAddress( $order->get_billing_email() );
140
141 if ( ! empty( $order->square_order_id ) ) {
142 $this->square_request->setOrderId( $order->square_order_id );
143 }
144
145 $this->square_api_args = array(
146 $this->square_request,
147 );
148 }
149
150
151 /**
152 * Sets the data for capturing a payment.
153 *
154 * @since 2.2.0
155 *
156 * @param \WC_Order $order order object
157 */
158 public function set_capture_data( \WC_Order $order ) {
159 $this->square_api_method = 'completePayment';
160 $this->square_api_args = array( $order->capture->trans_id );
161 }
162
163
164 /**
165 * Sets the data for voiding/cancelling a payment.
166 *
167 * @since 2.2.0
168 *
169 * @param \WC_Order $order order object
170 */
171 public function set_void_data( \WC_Order $order ) {
172 $this->square_api_method = 'cancelPayment';
173 $this->square_api_args = array( $order->refund->trans_id );
174 }
175
176
177 /**
178 * Sets the data for getting a Payment.
179 *
180 * @since 2.2.0
181 *
182 * @param string $payment_id payment ID
183 */
184 public function set_get_payment_data( $payment_id ) {
185 $this->square_api_method = 'getPayment';
186 $this->square_api_args = array( $payment_id );
187 }
188
189
190 /** Getter methods ************************************************************************************************/
191
192
193 /** Gets the location ID for this request.
194 *
195 * All requests in this type must have a location ID.
196 *
197 * @since 2.0.0
198 *
199 * @return string
200 */
201 protected function get_location_id() {
202
203 return $this->location_id;
204 }
205 }
206