PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 3.8.1
WooCommerce Square v3.8.1
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 / Transactions.php
woocommerce-square / includes / Gateway / API / Requests Last commit date
Card.php 3 years ago Customers.php 3 years ago Gift_Card.php 3 years ago Orders.php 3 years ago Payments.php 3 years ago Refunds.php 3 years ago Transactions.php 3 years ago
Transactions.php
261 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 Square\Models\Address;
29 use Square\Models\ChargeRequest;
30 use Square\Models\CreateRefundRequest;
31 use WooCommerce\Square\Framework\Compatibility\Order_Compatibility;
32 use WooCommerce\Square\Framework\Square_Helper;
33 use WooCommerce\Square\Utilities\Money_Utility;
34
35 class Transactions extends \WooCommerce\Square\API\Request {
36
37
38 /** @var string location ID */
39 protected $location_id;
40
41
42 /**
43 * Initializes a new transactions request.
44 *
45 * @since 2.0.0
46 *
47 * @param string $location_id location ID
48 * @param \Square\SquareClient $api_client the API client
49 */
50 public function __construct( $location_id, $api_client ) {
51
52 $this->location_id = $location_id;
53 $this->square_api = $api_client->getTransactionsApi();
54 }
55
56
57 /**
58 * Sets the data for an authorization/delayed capture.
59 *
60 * @since 2.0.0
61 *
62 * @param \WC_Order $order order object
63 */
64 public function set_authorization_data( \WC_Order $order ) {
65
66 $this->set_charge_data( $order, false );
67 }
68
69
70 /**
71 * Sets the data for a charge.
72 *
73 * @since 2.0.0
74 *
75 * @param \WC_Order $order
76 * @param bool $capture whether to immediately capture the charge
77 */
78 public function set_charge_data( \WC_Order $order, $capture = true ) {
79
80 $this->square_api_method = 'charge';
81 $this->square_request = new ChargeRequest(
82 wc_square()->get_idempotency_key( $order->unique_transaction_ref ),
83 Money_Utility::amount_to_money( $order->payment_total, $order->get_currency() )
84 );
85
86 $this->square_request->setReferenceId( $order->get_order_number() );
87
88 /**
89 * Filters the Square payment order note (legacy filter).
90 *
91 * @since 1.0.0
92 *
93 * @param string $description the order note (description)
94 * @param \WC_Order $order the order object
95 */
96 $description = (string) apply_filters( 'wc_square_payment_order_note', $order->description, $order );
97
98 $this->square_request->setNote( Square_Helper::str_truncate( $description, 60 ) );
99
100 $this->square_request->setDelayCapture( ! $capture );
101
102 if ( ! empty( $order->square_customer_id ) ) {
103 $this->square_request->setCustomerId( $order->square_customer_id );
104 }
105
106 // payment token (card ID) or card nonce (from JS)
107 if ( ! empty( $order->payment->token ) ) {
108 $this->square_request->setCustomerCardId( $order->payment->token );
109 } else {
110 $this->square_request->setCardNonce( $order->payment->nonce );
111 }
112
113 // 3DS / SCA verification token (from JS)
114 if ( ! empty( $order->payment->verification_token ) ) {
115 $this->square_request->setVerificationToken( $order->payment->verification_token );
116 }
117
118 $billing_address = new Address();
119 $billing_address->setFirstName( $order->get_billing_first_name() );
120 $billing_address->setLastName( $order->get_billing_last_name() );
121 $billing_address->setAddressLine1( $order->get_billing_address_1() );
122 $billing_address->setAddressLine2( $order->get_billing_address_2() );
123 $billing_address->setLocality( $order->get_billing_city() );
124 $billing_address->setAdministrativeDistrictLevel1( $order->get_billing_state() );
125 $billing_address->setPostalCode( $order->get_billing_postcode() );
126 $billing_address->setCountry( $order->get_billing_country() );
127
128 $this->square_request->setBillingAddress( $billing_address );
129
130 if ( Order_Compatibility::has_shipping_address( $order ) ) {
131
132 $shipping_address = new Address();
133 $shipping_address->setFirstName( $order->get_shipping_first_name() );
134 $shipping_address->setLastName( $order->get_shipping_last_name() );
135 $shipping_address->setAddressLine1( $order->get_shipping_address_1() );
136 $shipping_address->setAddressLine2( $order->get_shipping_address_2() );
137 $shipping_address->setLocality( $order->get_shipping_city() );
138 $shipping_address->setAdministrativeDistrictLevel1( $order->get_shipping_state() );
139 $shipping_address->setPostalCode( $order->get_shipping_postcode() );
140 $shipping_address->setCountry( $order->get_shipping_country() );
141
142 $this->square_request->setShippingAddress( $shipping_address );
143 }
144
145 $this->square_request->setBuyerEmailAddress( $order->get_billing_email() );
146
147 if ( ! empty( $order->square_order_id ) ) {
148 $this->square_request->setOrderId( $order->square_order_id );
149 }
150
151 $this->square_api_args = array(
152 $this->get_location_id(),
153 $this->square_request,
154 );
155 }
156
157
158 /**
159 * Sets the data for capturing a transaction.
160 *
161 * @since 2.0.0
162 *
163 * @param \WC_Order $order order object
164 */
165 public function set_capture_data( \WC_Order $order ) {
166
167 $this->square_api_method = 'captureTransaction';
168
169 $this->square_api_args = array(
170 $this->get_location_id(),
171 $order->capture->trans_id,
172 );
173 }
174
175
176 /**
177 * Sets the data for refund a transaction.
178 *
179 * @since 2.0.0
180 *
181 * @param \WC_Order $order order object
182 */
183 public function set_refund_data( \WC_Order $order ) {
184
185 $this->square_api_method = 'createRefund';
186
187 // The refund objects are sorted by date DESC, so the last one created will be at the start of the array
188 $refunds = $order->get_refunds();
189 $refund_obj = $refunds[0];
190
191 $this->square_request = new CreateRefundRequest(
192 wc_square()->get_idempotency_key( $order->get_id() . ':' . $refund_obj->get_id() ),
193 $order->refund->tender_id,
194 Money_Utility::amount_to_money( $order->refund->amount, $order->get_currency() )
195 );
196
197 $this->square_request->setReason( $order->refund->reason );
198
199 $this->square_api_args = array(
200 $this->get_location_id(),
201 $order->refund->trans_id,
202 $this->square_request,
203 );
204 }
205
206
207 /**
208 * Sets the data for voiding a transaction.
209 *
210 * @since 2.0.0
211 *
212 * @param \WC_Order $order order object
213 */
214 public function set_void_data( \WC_Order $order ) {
215
216 $this->square_api_method = 'voidTransaction';
217
218 $this->square_api_args = array(
219 $this->get_location_id(),
220 $order->refund->trans_id,
221 );
222 }
223
224
225 /**
226 * Sets the data for getting a transaction.
227 *
228 * @since 2.0.0
229 *
230 * @param string $transaction_id transaction ID
231 */
232 public function set_get_transaction_data( $transaction_id ) {
233
234 $this->square_api_method = 'retrieveTransaction';
235
236 $this->square_api_args = array(
237 $this->get_location_id(),
238 $transaction_id,
239 );
240 }
241
242
243 /** Getter methods ************************************************************************************************/
244
245
246 /** Gets the location ID for this request.
247 *
248 * All requests in this type must have a location ID.
249 *
250 * @since 2.0.0
251 *
252 * @return string
253 */
254 protected function get_location_id() {
255
256 return $this->location_id;
257 }
258
259
260 }
261