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
Payments.php
290 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 | |
| 32 | /** |
| 33 | * The Payments API request class. |
| 34 | * |
| 35 | * @since 2.2.0 |
| 36 | */ |
| 37 | class Payments extends \WooCommerce\Square\API\Request { |
| 38 | |
| 39 | /** @var string location ID */ |
| 40 | protected $location_id; |
| 41 | |
| 42 | /** |
| 43 | * Initializes a new payments request. |
| 44 | * |
| 45 | * @since 2.2.0 |
| 46 | * @param string $location_id location ID |
| 47 | * @param \Square\SquareClient $api_client the API client |
| 48 | */ |
| 49 | public function __construct( $location_id, $api_client ) { |
| 50 | $this->location_id = $location_id; |
| 51 | $this->square_api = $api_client->getPaymentsApi(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Sets the data for an authorization/delayed capture. |
| 56 | * |
| 57 | * @since 2.2.0 |
| 58 | * @param \WC_Order $order order object |
| 59 | */ |
| 60 | public function set_authorization_data( \WC_Order $order ) { |
| 61 | $this->set_charge_data( $order, false ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Sets the data for a charge. |
| 66 | * |
| 67 | * @since 2.2.0 |
| 68 | * |
| 69 | * @param \WC_Order $order |
| 70 | * @param bool $capture whether to immediately capture the charge |
| 71 | */ |
| 72 | public function set_charge_data( \WC_Order $order, $capture = true ) { |
| 73 | $this->square_api_method = 'createPayment'; |
| 74 | $this->square_request = new \Square\Models\CreatePaymentRequest( |
| 75 | ! empty( $order->payment->token ) ? $order->payment->token : $order->payment->nonce, |
| 76 | wc_square()->get_idempotency_key( $order->unique_transaction_ref, false ), |
| 77 | Utilities\Money_Utility::amount_to_money( $order->payment_total, $order->get_currency() ) |
| 78 | ); |
| 79 | |
| 80 | $this->square_request->setReferenceId( $order->get_order_number() ); |
| 81 | |
| 82 | /** |
| 83 | * Filters the Square payment order note (legacy filter). |
| 84 | * |
| 85 | * @since 2.2.0 |
| 86 | * |
| 87 | * @param string $description the order note (description) |
| 88 | * @param \WC_Order $order the order object |
| 89 | */ |
| 90 | $description = (string) apply_filters( 'wc_square_payment_order_note', $order->description, $order ); |
| 91 | |
| 92 | $this->square_request->setNote( Square_Helper::str_truncate( $description, 500 ) ); |
| 93 | |
| 94 | $this->square_request->setAutocomplete( $capture ); |
| 95 | |
| 96 | if ( ! empty( $order->square_customer_id ) ) { |
| 97 | $this->square_request->setCustomerId( $order->square_customer_id ); |
| 98 | } |
| 99 | |
| 100 | // payment token (card ID) or card nonce (from JS) |
| 101 | $this->square_request->setSourceId( ! empty( $order->payment->token ) ? $order->payment->token : $order->payment->nonce ); |
| 102 | |
| 103 | // 3DS / SCA verification token (from JS) |
| 104 | if ( ! empty( $order->payment->verification_token ) ) { |
| 105 | $this->square_request->setVerificationToken( $order->payment->verification_token ); |
| 106 | } |
| 107 | |
| 108 | if ( ! empty( $this->location_id ) ) { |
| 109 | $this->square_request->setLocationId( $this->location_id ); |
| 110 | } |
| 111 | |
| 112 | $billing_address = new \Square\Models\Address(); |
| 113 | $billing_address->setFirstName( $order->get_billing_first_name() ); |
| 114 | $billing_address->setLastName( $order->get_billing_last_name() ); |
| 115 | $billing_address->setAddressLine1( $order->get_billing_address_1() ); |
| 116 | $billing_address->setAddressLine2( $order->get_billing_address_2() ); |
| 117 | $billing_address->setLocality( $order->get_billing_city() ); |
| 118 | $billing_address->setAdministrativeDistrictLevel1( $order->get_billing_state() ); |
| 119 | $billing_address->setPostalCode( $order->get_billing_postcode() ); |
| 120 | $billing_address->setCountry( $order->get_billing_country() ); |
| 121 | |
| 122 | $this->square_request->setBillingAddress( $billing_address ); |
| 123 | |
| 124 | if ( $order->get_shipping_address_1( 'edit' ) || $order->get_shipping_address_2( 'edit' ) ) { |
| 125 | |
| 126 | $shipping_address = new \Square\Models\Address(); |
| 127 | $shipping_address->setFirstName( $order->get_shipping_first_name() ); |
| 128 | $shipping_address->setLastName( $order->get_shipping_last_name() ); |
| 129 | $shipping_address->setAddressLine1( $order->get_shipping_address_1() ); |
| 130 | $shipping_address->setAddressLine2( $order->get_shipping_address_2() ); |
| 131 | $shipping_address->setLocality( $order->get_shipping_city() ); |
| 132 | $shipping_address->setAdministrativeDistrictLevel1( $order->get_shipping_state() ); |
| 133 | $shipping_address->setPostalCode( $order->get_shipping_postcode() ); |
| 134 | $shipping_address->setCountry( $order->get_shipping_country() ); |
| 135 | |
| 136 | $this->square_request->setShippingAddress( $shipping_address ); |
| 137 | } |
| 138 | |
| 139 | $this->square_request->setBuyerEmailAddress( $order->get_billing_email() ); |
| 140 | |
| 141 | if ( ! empty( $order->square_order_id ) ) { |
| 142 | $this->square_request->setOrderId( $order->square_order_id ); |
| 143 | } |
| 144 | |
| 145 | $this->square_api_args = array( |
| 146 | $this->square_request, |
| 147 | ); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Prepares data required to update the payment total. |
| 152 | * |
| 153 | * @since 3.7.0 |
| 154 | * |
| 155 | * @param \WC_Order $order The WooCommerce order object. |
| 156 | * @param float $amount The new payment total. |
| 157 | */ |
| 158 | public function set_update_payment_data( \WC_Order $order, float $amount ) { |
| 159 | // Set the money object. |
| 160 | $amount_money = new \Square\Models\Money(); |
| 161 | $amount_money->setAmount( $amount ); |
| 162 | $amount_money->setCurrency( $order->get_currency() ); |
| 163 | |
| 164 | // Set the payments object. |
| 165 | $payment = new \Square\Models\Payment(); |
| 166 | $payment->setAmountMoney( $amount_money ); |
| 167 | |
| 168 | // Set the body for the updatePayment request. |
| 169 | $body = new \Square\Models\UpdatePaymentRequest( |
| 170 | $order->unique_transaction_ref |
| 171 | ); |
| 172 | $body->setPayment( $payment ); |
| 173 | |
| 174 | $this->square_api_method = 'updatePayment'; |
| 175 | $this->square_api_args = array( |
| 176 | $order->get_transaction_id(), |
| 177 | $body, |
| 178 | ); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Sets the data for capturing a payment. |
| 183 | * |
| 184 | * @since 2.2.0 |
| 185 | * |
| 186 | * @param \WC_Order $order order object |
| 187 | */ |
| 188 | public function set_capture_data( \WC_Order $order ) { |
| 189 | $this->square_api_method = 'completePayment'; |
| 190 | |
| 191 | $body = new \Square\Models\CompletePaymentRequest(); |
| 192 | $body->setVersionToken( null ); |
| 193 | $this->square_api_args = array( $order->capture->trans_id, $body ); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Sets the data for capturing a gift card payment. |
| 198 | * |
| 199 | * @param \WC_Order $order order object |
| 200 | * @since 3.7.0 |
| 201 | */ |
| 202 | public function set_gift_card_charge_data( \WC_Order $order ) { |
| 203 | $this->square_api_method = 'createPayment'; |
| 204 | $this->square_request = new \Square\Models\CreatePaymentRequest( |
| 205 | $order->payment->nonce, |
| 206 | wc_square()->get_idempotency_key( $order->unique_transaction_ref, false ), |
| 207 | Utilities\Money_Utility::amount_to_money( $order->payment_total, $order->get_currency() ) |
| 208 | ); |
| 209 | |
| 210 | $this->square_request->setReferenceId( $order->get_order_number() ); |
| 211 | |
| 212 | $should_authorize = Payment_Gateway::TRANSACTION_TYPE_AUTHORIZATION === wc_square()->get_gateway()->get_option( 'transaction_type' ); |
| 213 | |
| 214 | if ( $should_authorize ) { |
| 215 | $this->square_request->setAutocomplete( false ); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Filters the Square payment order note (legacy filter). |
| 220 | * |
| 221 | * @since 2.2.0 |
| 222 | * |
| 223 | * @param string $description the order note (description) |
| 224 | * @param \WC_Order $order the order object |
| 225 | */ |
| 226 | $description = (string) apply_filters( 'wc_square_payment_order_note', $order->description, $order ); |
| 227 | |
| 228 | $this->square_request->setNote( Square_Helper::str_truncate( $description, 500 ) ); |
| 229 | |
| 230 | if ( ! empty( $order->square_customer_id ) ) { |
| 231 | $this->square_request->setCustomerId( $order->square_customer_id ); |
| 232 | } |
| 233 | |
| 234 | if ( $order->square_order_id ) { |
| 235 | $this->square_request->setOrderId( $order->square_order_id ); |
| 236 | } |
| 237 | |
| 238 | if ( ! empty( $this->location_id ) ) { |
| 239 | $this->square_request->setLocationId( $this->location_id ); |
| 240 | } |
| 241 | |
| 242 | $this->square_api_args = array( |
| 243 | $this->square_request, |
| 244 | ); |
| 245 | } |
| 246 | |
| 247 | |
| 248 | /** |
| 249 | * Sets the data for voiding/cancelling a payment. |
| 250 | * |
| 251 | * @since 2.2.0 |
| 252 | * |
| 253 | * @param \WC_Order $order order object |
| 254 | */ |
| 255 | public function set_void_data( \WC_Order $order ) { |
| 256 | $this->square_api_method = 'cancelPayment'; |
| 257 | $this->square_api_args = array( $order->refund->trans_id ); |
| 258 | } |
| 259 | |
| 260 | |
| 261 | /** |
| 262 | * Sets the data for getting a Payment. |
| 263 | * |
| 264 | * @since 2.2.0 |
| 265 | * |
| 266 | * @param string $payment_id payment ID |
| 267 | */ |
| 268 | public function set_get_payment_data( $payment_id ) { |
| 269 | $this->square_api_method = 'getPayment'; |
| 270 | $this->square_api_args = array( $payment_id ); |
| 271 | } |
| 272 | |
| 273 | |
| 274 | /** Getter methods ************************************************************************************************/ |
| 275 | |
| 276 | |
| 277 | /** Gets the location ID for this request. |
| 278 | * |
| 279 | * All requests in this type must have a location ID. |
| 280 | * |
| 281 | * @since 2.0.0 |
| 282 | * |
| 283 | * @return string |
| 284 | */ |
| 285 | protected function get_location_id() { |
| 286 | |
| 287 | return $this->location_id; |
| 288 | } |
| 289 | } |
| 290 |