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 |