PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 5.4.2
WooCommerce Square v5.4.2
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
Card.php 11 months ago Customers.php 3 years ago Gift_Card.php 2 years ago Gift_Card_Activities.php 2 years ago Orders.php 4 months ago Payments.php 11 months ago Refunds.php 3 years ago Transactions.php 11 months ago
Payments.php
352 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 GNU General Public License v3.0 or later
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 or later
22 */
23
24 namespace WooCommerce\Square\Gateway\API\Requests;
25
26 defined( 'ABSPATH' ) || exit;
27
28 use WooCommerce\Square\Framework\PaymentGateway\Payment_Gateway;
29 use WooCommerce\Square\Framework\Square_Helper;
30 use WooCommerce\Square\Utilities;
31 use WooCommerce\Square\Handlers\Order;
32 use WooCommerce\Square\Utilities\Money_Utility;
33
34 /**
35 * The Payments API request class.
36 *
37 * @since 2.2.0
38 */
39 class Payments extends \WooCommerce\Square\API\Request {
40
41 /** @var string location ID */
42 protected $location_id;
43
44 /**
45 * Initializes a new payments request.
46 *
47 * @since 2.2.0
48 * @param string $location_id location ID
49 * @param \Square\SquareClient $api_client the API client
50 */
51 public function __construct( $location_id, $api_client ) {
52 $this->location_id = $location_id;
53 $this->square_api = $api_client->getPaymentsApi();
54 }
55
56 /**
57 * Sets the data for an authorization/delayed capture.
58 *
59 * @since 2.2.0
60 * @param \WC_Order $order order object
61 */
62 public function set_authorization_data( \WC_Order $order ) {
63 $this->set_charge_data( $order, false );
64 }
65
66 /**
67 * Sets the data for a charge.
68 *
69 * @since 2.2.0
70 *
71 * @param \WC_Order $order
72 * @param bool $capture whether to immediately capture the charge
73 */
74 public function set_charge_data( \WC_Order $order, $capture = true, $is_cash_app_pay = false ) {
75 $this->square_api_method = 'createPayment';
76
77 $payment_total = isset( $order->payment->partial_total ) ? $order->payment->partial_total->other_gateway : $order->payment_total;
78 // Cash App Pay payment.
79 if ( $is_cash_app_pay ) {
80 $this->square_request = new \Square\Models\CreatePaymentRequest(
81 $order->payment->nonce->cash_app_pay,
82 wc_square()->get_idempotency_key( $order->unique_transaction_ref, false )
83 );
84 } else {
85 $source_id = ! empty( $order->payment->verified_token ) ? $order->payment->verified_token : ( ! empty( $order->payment->token ) ? $order->payment->token : $order->payment->nonce->credit_card );
86 $this->square_request = new \Square\Models\CreatePaymentRequest(
87 $source_id,
88 wc_square()->get_idempotency_key( $order->unique_transaction_ref, false )
89 );
90 }
91
92 $this->square_request->setReferenceId( $order->get_order_number() );
93 $this->square_request->setAmountMoney(
94 Utilities\Money_Utility::amount_to_money( $payment_total, $order->get_currency() )
95 );
96
97 if ( defined( 'WOOCOMMERCE_CHECKOUT' ) && WOOCOMMERCE_CHECKOUT ) {
98 $customer_details = new \Square\Models\CustomerDetails();
99 $customer_details->setCustomerInitiated( true );
100 $this->square_request->setCustomerDetails( $customer_details );
101 } elseif ( ! empty( $order->payment->token ) ) {
102 // Set customer initiated to false for the case of a renewal order or a pre-order release payment.
103 $customer_details = new \Square\Models\CustomerDetails();
104 $customer_details->setCustomerInitiated( false );
105 $this->square_request->setCustomerDetails( $customer_details );
106 }
107
108 /**
109 * Filters the Square payment order note (legacy filter).
110 *
111 * @since 2.2.0
112 *
113 * @param string $description the order note (description)
114 * @param \WC_Order $order the order object
115 */
116 $description = (string) apply_filters( 'wc_square_payment_order_note', $order->description, $order );
117
118 $this->square_request->setNote( Square_Helper::str_truncate( $description, 500 ) );
119
120 if ( ! empty( $order->square_customer_id ) ) {
121 $this->square_request->setCustomerId( $order->square_customer_id );
122 }
123
124 if ( isset( $order->payment->partial_total ) ) {
125 $this->square_request->setAutocomplete( false );
126 } else {
127 $this->square_request->setAutocomplete( $capture );
128 }
129
130 // Cash App Pay payment.
131 if ( $is_cash_app_pay ) {
132 // Payment nonce (from JS)
133 $this->square_request->setSourceId( $order->payment->nonce->cash_app_pay );
134 } else {
135 // payment token (card ID) or card nonce (from JS)
136 $source_id = ! empty( $order->payment->verified_token ) ? $order->payment->verified_token : ( ! empty( $order->payment->token ) ? $order->payment->token : $order->payment->nonce->credit_card );
137 $this->square_request->setSourceId( $source_id );
138
139 // 3DS / SCA verification token (from JS)
140 if ( ! empty( $order->payment->verification_token ) ) {
141 $this->square_request->setVerificationToken( $order->payment->verification_token );
142 }
143 }
144
145 if ( ! empty( $this->location_id ) ) {
146 $this->square_request->setLocationId( $this->location_id );
147 }
148
149 $billing_address = new \Square\Models\Address();
150 $billing_address->setFirstName( $order->get_billing_first_name() );
151 $billing_address->setLastName( $order->get_billing_last_name() );
152 $billing_address->setAddressLine1( $order->get_billing_address_1() );
153 $billing_address->setAddressLine2( $order->get_billing_address_2() );
154 $billing_address->setLocality( $order->get_billing_city() );
155 $billing_address->setAdministrativeDistrictLevel1( $order->get_billing_state() );
156 $billing_address->setPostalCode( $order->get_billing_postcode() );
157 $billing_address->setCountry( $order->get_billing_country() );
158
159 $this->square_request->setBillingAddress( $billing_address );
160
161 if ( $order->get_shipping_address_1( 'edit' ) || $order->get_shipping_address_2( 'edit' ) ) {
162
163 $shipping_address = new \Square\Models\Address();
164 $shipping_address->setFirstName( $order->get_shipping_first_name() );
165 $shipping_address->setLastName( $order->get_shipping_last_name() );
166 $shipping_address->setAddressLine1( $order->get_shipping_address_1() );
167 $shipping_address->setAddressLine2( $order->get_shipping_address_2() );
168 $shipping_address->setLocality( $order->get_shipping_city() );
169 $shipping_address->setAdministrativeDistrictLevel1( $order->get_shipping_state() );
170 $shipping_address->setPostalCode( $order->get_shipping_postcode() );
171 $shipping_address->setCountry( $order->get_shipping_country() );
172
173 $this->square_request->setShippingAddress( $shipping_address );
174 }
175
176 $this->square_request->setBuyerEmailAddress( $order->get_billing_email() );
177
178 if ( ! empty( $order->square_order_id ) ) {
179 $this->square_request->setOrderId( $order->square_order_id );
180 }
181
182 $this->square_api_args = array(
183 $this->square_request,
184 );
185 }
186
187 /**
188 * Prepares data required to update the payment total.
189 *
190 * @since 3.7.0
191 *
192 * @param \WC_Order $order The WooCommerce order object.
193 * @param float $amount The new payment total.
194 */
195 public function set_update_payment_data( \WC_Order $order, float $amount ) {
196 // Set the money object.
197 $amount_money = new \Square\Models\Money();
198
199 $charge_type = wc_square()->get_gateway( $order->get_payment_method() )->get_order_meta( $order, 'charge_type' );
200 $body = new \Square\Models\UpdatePaymentRequest( $order->unique_transaction_ref );
201 $transaction_id = '';
202
203 // Set the body for the updatePayment request.
204 if ( Payment_Gateway::CHARGE_TYPE_PARTIAL === $charge_type ) {
205 $gift_card_amount = Order::get_gift_card_total_charged_amount( $order );
206 $gift_card_amount = Money_Utility::amount_to_cents( $gift_card_amount, $order->get_currency() );
207 $transaction_id = wc_square()->get_gateway( $order->get_payment_method() )->get_order_meta( $order, 'trans_id' );
208 $amount_money->setAmount( $amount - $gift_card_amount );
209 } else {
210 $transaction_id = $order->get_transaction_id();
211 $amount_money->setAmount( $amount );
212 }
213
214 $amount_money->setCurrency( $order->get_currency() );
215
216 // Set the payments object.
217 $payment = new \Square\Models\Payment();
218 $payment->setAmountMoney( $amount_money );
219
220 $body->setPayment( $payment );
221
222 $this->square_api_method = 'updatePayment';
223 $this->square_api_args = array(
224 $transaction_id,
225 $body,
226 );
227 }
228
229 /**
230 * Sets the data for capturing a payment.
231 *
232 * @since 2.2.0
233 *
234 * @param \WC_Order $order order object
235 */
236 public function set_capture_data( \WC_Order $order ) {
237 $this->square_api_method = 'completePayment';
238
239 $body = new \Square\Models\CompletePaymentRequest();
240 $body->setVersionToken( null );
241 $this->square_api_args = array( $order->capture->trans_id, $body );
242 }
243
244 /**
245 * Sets the data for capturing a gift card payment.
246 *
247 * @param \WC_Order $order order object
248 * @since 3.7.0
249 */
250 public function set_gift_card_charge_data( \WC_Order $order ) {
251 $payment_total = isset( $order->payment->partial_total ) ? $order->payment->partial_total->gift_card : $order->payment_total;
252 $this->square_api_method = 'createPayment';
253 $this->square_request = new \Square\Models\CreatePaymentRequest(
254 $order->payment->nonce->gift_card,
255 wc_square()->get_idempotency_key( $order->unique_transaction_ref, false )
256 );
257
258 $this->square_request->setReferenceId( $order->get_order_number() );
259 $this->square_request->setAmountMoney(
260 Utilities\Money_Utility::amount_to_money( $payment_total, $order->get_currency() )
261 );
262
263 $should_authorize = wc_square()->get_gateway( $order->get_payment_method() )->perform_authorization( $order );
264
265 if ( isset( $order->payment->partial_total ) || $should_authorize ) {
266 $this->square_request->setAutocomplete( false );
267 }
268
269 /**
270 * Filters the Square payment order note (legacy filter).
271 *
272 * @since 2.2.0
273 *
274 * @param string $description the order note (description)
275 * @param \WC_Order $order the order object
276 */
277 $description = (string) apply_filters( 'wc_square_payment_order_note', $order->description, $order );
278
279 $this->square_request->setNote( Square_Helper::str_truncate( $description, 500 ) );
280
281 if ( ! empty( $order->square_customer_id ) ) {
282 $this->square_request->setCustomerId( $order->square_customer_id );
283 }
284
285 if ( $order->square_order_id ) {
286 $this->square_request->setOrderId( $order->square_order_id );
287 }
288
289 if ( ! empty( $this->location_id ) ) {
290 $this->square_request->setLocationId( $this->location_id );
291 }
292
293 $this->square_api_args = array(
294 $this->square_request,
295 );
296 }
297
298
299 /**
300 * Sets the data for voiding/cancelling a payment.
301 *
302 * @since 2.2.0
303 *
304 * @param \WC_Order $order order object
305 */
306 public function set_void_data( \WC_Order $order ) {
307 $this->square_api_method = 'cancelPayment';
308 $this->square_api_args = array( $order->refund->trans_id );
309 }
310
311
312 /**
313 * Sets the data for getting a Payment.
314 *
315 * @since 2.2.0
316 *
317 * @param string $payment_id payment ID
318 */
319 public function set_get_payment_data( $payment_id ) {
320 $this->square_api_method = 'getPayment';
321 $this->square_api_args = array( $payment_id );
322 }
323
324 /**
325 * Sets the data for cancel a Payment.
326 *
327 * @since 4.6.0
328 *
329 * @param string $payment_id payment ID
330 */
331 public function set_cancel_payment_data( $payment_id ) {
332 $this->square_api_method = 'cancelPayment';
333 $this->square_api_args = array( $payment_id );
334 }
335
336 /** Getter methods ************************************************************************************************/
337
338
339 /** Gets the location ID for this request.
340 *
341 * All requests in this type must have a location ID.
342 *
343 * @since 2.0.0
344 *
345 * @return string
346 */
347 protected function get_location_id() {
348
349 return $this->location_id;
350 }
351 }
352