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
Payments.php
206 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 SquareConnect\Api\PaymentsApi; |
| 29 | use SquareConnect\Model as SquareModel; |
| 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 \SquareConnect\ApiClient $api_client the API client |
| 48 | */ |
| 49 | public function __construct( $location_id, $api_client ) { |
| 50 | $this->location_id = $location_id; |
| 51 | $this->square_api = new PaymentsApi( $api_client ); |
| 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 | |
| 74 | $this->square_api_method = 'createPayment'; |
| 75 | $this->square_request = new SquareModel\CreatePaymentRequest(); |
| 76 | |
| 77 | $this->square_request->setIdempotencyKey( wc_square()->get_idempotency_key( $order->unique_transaction_ref ) ); |
| 78 | $this->square_request->setAmountMoney( Utilities\Money_Utility::amount_to_money( $order->payment_total, $order->get_currency() ) ); |
| 79 | $this->square_request->setReferenceId( $order->get_order_number() ); |
| 80 | |
| 81 | /** |
| 82 | * Filters the Square payment order note (legacy filter). |
| 83 | * |
| 84 | * @since 2.2.0 |
| 85 | * |
| 86 | * @param string $description the order note (description) |
| 87 | * @param \WC_Order $order the order object |
| 88 | */ |
| 89 | $description = (string) apply_filters( 'wc_square_payment_order_note', $order->description, $order ); |
| 90 | |
| 91 | $this->square_request->setNote( Utilities\String_Utility::truncate( $description, 500 ) ); |
| 92 | |
| 93 | $this->square_request->setAutocomplete( $capture ); |
| 94 | |
| 95 | if ( ! empty( $order->square_customer_id ) ) { |
| 96 | $this->square_request->setCustomerId( $order->square_customer_id ); |
| 97 | } |
| 98 | |
| 99 | // payment token (card ID) or card nonce (from JS) |
| 100 | $this->square_request->setSourceId( ! empty( $order->payment->token ) ? $order->payment->token : $order->payment->nonce ); |
| 101 | |
| 102 | // 3DS / SCA verification token (from JS) |
| 103 | if ( ! empty( $order->payment->verification_token ) ) { |
| 104 | $this->square_request->setVerificationToken( $order->payment->verification_token ); |
| 105 | } |
| 106 | |
| 107 | if ( ! empty( $this->location_id ) ) { |
| 108 | $this->square_request->setLocationId( $this->location_id ); |
| 109 | } |
| 110 | |
| 111 | $billing_address = new SquareModel\Address(); |
| 112 | $billing_address->setFirstName( $order->get_billing_first_name() ); |
| 113 | $billing_address->setLastName( $order->get_billing_last_name() ); |
| 114 | $billing_address->setOrganization( $order->get_billing_company() ); |
| 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 SquareModel\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 | /** |
| 152 | * Sets the data for capturing a payment. |
| 153 | * |
| 154 | * @since 2.2.0 |
| 155 | * |
| 156 | * @param \WC_Order $order order object |
| 157 | */ |
| 158 | public function set_capture_data( \WC_Order $order ) { |
| 159 | $this->square_api_method = 'completePayment'; |
| 160 | $this->square_api_args = array( $order->capture->trans_id ); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | /** |
| 165 | * Sets the data for voiding/cancelling a payment. |
| 166 | * |
| 167 | * @since 2.2.0 |
| 168 | * |
| 169 | * @param \WC_Order $order order object |
| 170 | */ |
| 171 | public function set_void_data( \WC_Order $order ) { |
| 172 | $this->square_api_method = 'cancelPayment'; |
| 173 | $this->square_api_args = array( $order->refund->trans_id ); |
| 174 | } |
| 175 | |
| 176 | |
| 177 | /** |
| 178 | * Sets the data for getting a Payment. |
| 179 | * |
| 180 | * @since 2.2.0 |
| 181 | * |
| 182 | * @param string $payment_id payment ID |
| 183 | */ |
| 184 | public function set_get_payment_data( $payment_id ) { |
| 185 | $this->square_api_method = 'getPayment'; |
| 186 | $this->square_api_args = array( $payment_id ); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | /** Getter methods ************************************************************************************************/ |
| 191 | |
| 192 | |
| 193 | /** Gets the location ID for this request. |
| 194 | * |
| 195 | * All requests in this type must have a location ID. |
| 196 | * |
| 197 | * @since 2.0.0 |
| 198 | * |
| 199 | * @return string |
| 200 | */ |
| 201 | protected function get_location_id() { |
| 202 | |
| 203 | return $this->location_id; |
| 204 | } |
| 205 | } |
| 206 |