PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 4.8.0
WooCommerce Square v4.8.0
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
Card.php 3 years ago Customers.php 3 years ago Gift_Card.php 2 years ago Gift_Card_Activities.php 2 years ago Orders.php 2 years ago Payments.php 2 years ago Refunds.php 3 years ago Transactions.php 3 years ago
Payments.php
339 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 $this->square_request = new \Square\Models\CreatePaymentRequest(
86 ! empty( $order->payment->token ) ? $order->payment->token : $order->payment->nonce->credit_card,
87 wc_square()->get_idempotency_key( $order->unique_transaction_ref, false )
88 );
89 }
90
91 $this->square_request->setReferenceId( $order->get_order_number() );
92 $this->square_request->setAmountMoney(
93 Utilities\Money_Utility::amount_to_money( $payment_total, $order->get_currency() )
94 );
95
96 /**
97 * Filters the Square payment order note (legacy filter).
98 *
99 * @since 2.2.0
100 *
101 * @param string $description the order note (description)
102 * @param \WC_Order $order the order object
103 */
104 $description = (string) apply_filters( 'wc_square_payment_order_note', $order->description, $order );
105
106 $this->square_request->setNote( Square_Helper::str_truncate( $description, 500 ) );
107
108 if ( ! empty( $order->square_customer_id ) ) {
109 $this->square_request->setCustomerId( $order->square_customer_id );
110 }
111
112 if ( isset( $order->payment->partial_total ) ) {
113 $this->square_request->setAutocomplete( false );
114 } else {
115 $this->square_request->setAutocomplete( $capture );
116 }
117
118 // Cash App Pay payment.
119 if ( $is_cash_app_pay ) {
120 // Payment nonce (from JS)
121 $this->square_request->setSourceId( $order->payment->nonce->cash_app_pay );
122 } else {
123 // payment token (card ID) or card nonce (from JS)
124 $this->square_request->setSourceId( ! empty( $order->payment->token ) ? $order->payment->token : $order->payment->nonce->credit_card );
125
126 // 3DS / SCA verification token (from JS)
127 if ( ! empty( $order->payment->verification_token ) ) {
128 $this->square_request->setVerificationToken( $order->payment->verification_token );
129 }
130 }
131
132 if ( ! empty( $this->location_id ) ) {
133 $this->square_request->setLocationId( $this->location_id );
134 }
135
136 $billing_address = new \Square\Models\Address();
137 $billing_address->setFirstName( $order->get_billing_first_name() );
138 $billing_address->setLastName( $order->get_billing_last_name() );
139 $billing_address->setAddressLine1( $order->get_billing_address_1() );
140 $billing_address->setAddressLine2( $order->get_billing_address_2() );
141 $billing_address->setLocality( $order->get_billing_city() );
142 $billing_address->setAdministrativeDistrictLevel1( $order->get_billing_state() );
143 $billing_address->setPostalCode( $order->get_billing_postcode() );
144 $billing_address->setCountry( $order->get_billing_country() );
145
146 $this->square_request->setBillingAddress( $billing_address );
147
148 if ( $order->get_shipping_address_1( 'edit' ) || $order->get_shipping_address_2( 'edit' ) ) {
149
150 $shipping_address = new \Square\Models\Address();
151 $shipping_address->setFirstName( $order->get_shipping_first_name() );
152 $shipping_address->setLastName( $order->get_shipping_last_name() );
153 $shipping_address->setAddressLine1( $order->get_shipping_address_1() );
154 $shipping_address->setAddressLine2( $order->get_shipping_address_2() );
155 $shipping_address->setLocality( $order->get_shipping_city() );
156 $shipping_address->setAdministrativeDistrictLevel1( $order->get_shipping_state() );
157 $shipping_address->setPostalCode( $order->get_shipping_postcode() );
158 $shipping_address->setCountry( $order->get_shipping_country() );
159
160 $this->square_request->setShippingAddress( $shipping_address );
161 }
162
163 $this->square_request->setBuyerEmailAddress( $order->get_billing_email() );
164
165 if ( ! empty( $order->square_order_id ) ) {
166 $this->square_request->setOrderId( $order->square_order_id );
167 }
168
169 $this->square_api_args = array(
170 $this->square_request,
171 );
172 }
173
174 /**
175 * Prepares data required to update the payment total.
176 *
177 * @since 3.7.0
178 *
179 * @param \WC_Order $order The WooCommerce order object.
180 * @param float $amount The new payment total.
181 */
182 public function set_update_payment_data( \WC_Order $order, float $amount ) {
183 // Set the money object.
184 $amount_money = new \Square\Models\Money();
185
186 $charge_type = wc_square()->get_gateway( $order->get_payment_method() )->get_order_meta( $order, 'charge_type' );
187 $body = new \Square\Models\UpdatePaymentRequest( $order->unique_transaction_ref );
188 $transaction_id = '';
189
190 // Set the body for the updatePayment request.
191 if ( Payment_Gateway::CHARGE_TYPE_PARTIAL === $charge_type ) {
192 $gift_card_amount = Order::get_gift_card_total_charged_amount( $order );
193 $gift_card_amount = Money_Utility::amount_to_cents( $gift_card_amount, $order->get_currency() );
194 $transaction_id = wc_square()->get_gateway( $order->get_payment_method() )->get_order_meta( $order, 'trans_id' );
195 $amount_money->setAmount( $amount - $gift_card_amount );
196 } else {
197 $transaction_id = $order->get_transaction_id();
198 $amount_money->setAmount( $amount );
199 }
200
201 $amount_money->setCurrency( $order->get_currency() );
202
203 // Set the payments object.
204 $payment = new \Square\Models\Payment();
205 $payment->setAmountMoney( $amount_money );
206
207 $body->setPayment( $payment );
208
209 $this->square_api_method = 'updatePayment';
210 $this->square_api_args = array(
211 $transaction_id,
212 $body,
213 );
214 }
215
216 /**
217 * Sets the data for capturing a payment.
218 *
219 * @since 2.2.0
220 *
221 * @param \WC_Order $order order object
222 */
223 public function set_capture_data( \WC_Order $order ) {
224 $this->square_api_method = 'completePayment';
225
226 $body = new \Square\Models\CompletePaymentRequest();
227 $body->setVersionToken( null );
228 $this->square_api_args = array( $order->capture->trans_id, $body );
229 }
230
231 /**
232 * Sets the data for capturing a gift card payment.
233 *
234 * @param \WC_Order $order order object
235 * @since 3.7.0
236 */
237 public function set_gift_card_charge_data( \WC_Order $order ) {
238 $payment_total = isset( $order->payment->partial_total ) ? $order->payment->partial_total->gift_card : $order->payment_total;
239 $this->square_api_method = 'createPayment';
240 $this->square_request = new \Square\Models\CreatePaymentRequest(
241 $order->payment->nonce->gift_card,
242 wc_square()->get_idempotency_key( $order->unique_transaction_ref, false )
243 );
244
245 $this->square_request->setReferenceId( $order->get_order_number() );
246 $this->square_request->setAmountMoney(
247 Utilities\Money_Utility::amount_to_money( $payment_total, $order->get_currency() )
248 );
249
250 $should_authorize = wc_square()->get_gateway( $order->get_payment_method() )->perform_authorization( $order );
251
252 if ( isset( $order->payment->partial_total ) || $should_authorize ) {
253 $this->square_request->setAutocomplete( false );
254 }
255
256 /**
257 * Filters the Square payment order note (legacy filter).
258 *
259 * @since 2.2.0
260 *
261 * @param string $description the order note (description)
262 * @param \WC_Order $order the order object
263 */
264 $description = (string) apply_filters( 'wc_square_payment_order_note', $order->description, $order );
265
266 $this->square_request->setNote( Square_Helper::str_truncate( $description, 500 ) );
267
268 if ( ! empty( $order->square_customer_id ) ) {
269 $this->square_request->setCustomerId( $order->square_customer_id );
270 }
271
272 if ( $order->square_order_id ) {
273 $this->square_request->setOrderId( $order->square_order_id );
274 }
275
276 if ( ! empty( $this->location_id ) ) {
277 $this->square_request->setLocationId( $this->location_id );
278 }
279
280 $this->square_api_args = array(
281 $this->square_request,
282 );
283 }
284
285
286 /**
287 * Sets the data for voiding/cancelling a payment.
288 *
289 * @since 2.2.0
290 *
291 * @param \WC_Order $order order object
292 */
293 public function set_void_data( \WC_Order $order ) {
294 $this->square_api_method = 'cancelPayment';
295 $this->square_api_args = array( $order->refund->trans_id );
296 }
297
298
299 /**
300 * Sets the data for getting a Payment.
301 *
302 * @since 2.2.0
303 *
304 * @param string $payment_id payment ID
305 */
306 public function set_get_payment_data( $payment_id ) {
307 $this->square_api_method = 'getPayment';
308 $this->square_api_args = array( $payment_id );
309 }
310
311 /**
312 * Sets the data for cancel a Payment.
313 *
314 * @since 4.6.0
315 *
316 * @param string $payment_id payment ID
317 */
318 public function set_cancel_payment_data( $payment_id ) {
319 $this->square_api_method = 'cancelPayment';
320 $this->square_api_args = array( $payment_id );
321 }
322
323 /** Getter methods ************************************************************************************************/
324
325
326 /** Gets the location ID for this request.
327 *
328 * All requests in this type must have a location ID.
329 *
330 * @since 2.0.0
331 *
332 * @return string
333 */
334 protected function get_location_id() {
335
336 return $this->location_id;
337 }
338 }
339