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 / Transactions.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
Transactions.php
260 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 SkyVerge\WooCommerce\PluginFramework\v5_4_0 as Framework;
29 use SquareConnect\Api\TransactionsApi;
30 use SquareConnect\Model\Address;
31 use SquareConnect\Model\ChargeRequest;
32 use SquareConnect\Model\CreateRefundRequest;
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 \SquareConnect\ApiClient $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 = new TransactionsApi( $api_client );
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
83 $this->square_request->setIdempotencyKey( wc_square()->get_idempotency_key( $order->unique_transaction_ref ) );
84 $this->square_request->setAmountMoney( Money_Utility::amount_to_money( $order->payment_total, $order->get_currency() ) );
85 $this->square_request->setReferenceId( $order->get_order_number() );
86
87 /**
88 * Filters the Square payment order note (legacy filter).
89 *
90 * @since 1.0.0
91 *
92 * @param string $description the order note (description)
93 * @param \WC_Order $order the order object
94 */
95 $description = (string) apply_filters( 'wc_square_payment_order_note', $order->description, $order );
96
97 $this->square_request->setNote( Framework\SV_WC_Helper::str_truncate( $description, 60 ) );
98
99 $this->square_request->setDelayCapture( ! $capture );
100
101 if ( ! empty( $order->square_customer_id ) ) {
102 $this->square_request->setCustomerId( $order->square_customer_id );
103 }
104
105 // payment token (card ID) or card nonce (from JS)
106 if ( ! empty( $order->payment->token ) ) {
107 $this->square_request->setCustomerCardId( $order->payment->token );
108 } else {
109 $this->square_request->setCardNonce( $order->payment->nonce );
110 }
111
112 // 3DS / SCA verification token (from JS)
113 if ( ! empty( $order->payment->verification_token ) ) {
114 $this->square_request->setVerificationToken( $order->payment->verification_token );
115 }
116
117 $billing_address = new Address();
118 $billing_address->setFirstName( $order->get_billing_first_name() );
119 $billing_address->setLastName( $order->get_billing_last_name() );
120 $billing_address->setOrganization( $order->get_billing_company() );
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 ( Framework\SV_WC_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 $this->square_request->setIdempotencyKey( wc_square()->get_idempotency_key( $order->get_id() . ':' . $refund_obj->get_id() ) );
193 $this->square_request->setTenderId( $order->refund->tender_id );
194 $this->square_request->setReason( $order->refund->reason );
195
196 $this->square_request->setAmountMoney( Money_Utility::amount_to_money( $order->refund->amount, $order->get_currency() ) );
197
198 $this->square_api_args = array(
199 $this->get_location_id(),
200 $order->refund->trans_id,
201 $this->square_request,
202 );
203 }
204
205
206 /**
207 * Sets the data for voiding a transaction.
208 *
209 * @since 2.0.0
210 *
211 * @param \WC_Order $order order object
212 */
213 public function set_void_data( \WC_Order $order ) {
214
215 $this->square_api_method = 'voidTransaction';
216
217 $this->square_api_args = array(
218 $this->get_location_id(),
219 $order->refund->trans_id,
220 );
221 }
222
223
224 /**
225 * Sets the data for getting a transaction.
226 *
227 * @since 2.0.0
228 *
229 * @param string $transaction_id transaction ID
230 */
231 public function set_get_transaction_data( $transaction_id ) {
232
233 $this->square_api_method = 'retrieveTransaction';
234
235 $this->square_api_args = array(
236 $this->get_location_id(),
237 $transaction_id,
238 );
239 }
240
241
242 /** Getter methods ************************************************************************************************/
243
244
245 /** Gets the location ID for this request.
246 *
247 * All requests in this type must have a location ID.
248 *
249 * @since 2.0.0
250 *
251 * @return string
252 */
253 protected function get_location_id() {
254
255 return $this->location_id;
256 }
257
258
259 }
260