| 1 |
<?php |
| 2 |
/** |
| 3 |
* The class responsible for helping us understand the response to a Square payment request. |
| 4 |
* |
| 5 |
* @package \Charitable\Pro\Square\Gateway\Payment\Response |
| 6 |
* @author Studio 164a |
| 7 |
* @copyright Copyright (c) 2022, Studio 164a |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.0.0 |
| 10 |
* @version 1.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Response object after making payment request. |
| 20 |
* |
| 21 |
* @since 1.0.0 |
| 22 |
*/ |
| 23 |
class Charitable_Square_Payment_Response { |
| 24 |
|
| 25 |
/** |
| 26 |
* The response data. |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
* |
| 30 |
* @var object |
| 31 |
*/ |
| 32 |
private $response; |
| 33 |
|
| 34 |
/** |
| 35 |
* Set up the response object. |
| 36 |
* |
| 37 |
* @since 1.0.0 |
| 38 |
* |
| 39 |
* @param object $response The response data. |
| 40 |
*/ |
| 41 |
public function __construct( $response ) { |
| 42 |
$this->response = $response; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Return the gateway transaction id. |
| 47 |
* |
| 48 |
* @since 1.8.7 |
| 49 |
* |
| 50 |
* @return string |
| 51 |
*/ |
| 52 |
public function get_gateway_transaction_id() { |
| 53 |
$id = null; |
| 54 |
|
| 55 |
if ( isset( $this->response->checkout->order ) ) { |
| 56 |
$id = $this->response->checkout->order->id; |
| 57 |
} elseif ( isset( $this->response->payment ) ) { |
| 58 |
$id = $this->response->payment->order_id; |
| 59 |
} |
| 60 |
|
| 61 |
return $id; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Return the gateway payment id |
| 66 |
* |
| 67 |
* @since 1.8.7 |
| 68 |
* |
| 69 |
* @return string|false |
| 70 |
*/ |
| 71 |
public function get_gateway_payment_id() { |
| 72 |
$id = null; |
| 73 |
|
| 74 |
if ( isset( $this->response->checkout->payment ) ) { |
| 75 |
$id = $this->response->checkout->payment->id; |
| 76 |
} elseif ( isset( $this->response->payment ) ) { |
| 77 |
$id = $this->response->payment->id; |
| 78 |
} |
| 79 |
|
| 80 |
return $id; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Return the gateway transaction url |
| 85 |
* |
| 86 |
* @since 1.0.0 |
| 87 |
* |
| 88 |
* @return string|false |
| 89 |
*/ |
| 90 |
public function get_gateway_transaction_url() { |
| 91 |
if ( ! $this->get_gateway_transaction_id() ) { |
| 92 |
return ''; |
| 93 |
} |
| 94 |
$domain = $this->is_sandbox() ? 'https://squareupsandbox.com/' : 'https://squareup.com/'; |
| 95 |
$transaction_url = $domain . 'dashboard/sales/transactions/' . $this->get_gateway_transaction_id(); |
| 96 |
return $transaction_url; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Return the gateway subscription id |
| 101 |
* |
| 102 |
* @since 1.8.7 |
| 103 |
* |
| 104 |
* @return string|false |
| 105 |
*/ |
| 106 |
public function get_gateway_subscription_id() { |
| 107 |
$id = null; |
| 108 |
|
| 109 |
if ( isset( $this->response->subscription->id ) ) { |
| 110 |
$id = $this->response->subscription->id; |
| 111 |
} |
| 112 |
|
| 113 |
return $id; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Return the gateway plan variation id |
| 118 |
* |
| 119 |
* @since 1.8.7 |
| 120 |
* |
| 121 |
* @return string|false |
| 122 |
*/ |
| 123 |
public function get_gateway_plan_variation_id() { |
| 124 |
$id = null; |
| 125 |
|
| 126 |
if ( isset( $this->response->subscription->plan_variation_id ) ) { |
| 127 |
$id = $this->response->subscription->plan_variation_id; |
| 128 |
} |
| 129 |
|
| 130 |
return $id; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Checks if this is a sandbox transaction or not |
| 135 |
* |
| 136 |
* @since 1.0.0 |
| 137 |
* |
| 138 |
* @return boolean |
| 139 |
*/ |
| 140 |
private function is_sandbox() { |
| 141 |
$checkout_page_url = isset( $this->response->checkout->checkout_page_url ) ? $this->response->checkout->checkout_page_url : ''; |
| 142 |
return str_contains( $checkout_page_url, 'sandbox' ); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Returns whether the payment requires some further action. |
| 147 |
* |
| 148 |
* @since 1.0.0 |
| 149 |
* |
| 150 |
* @return boolean |
| 151 |
*/ |
| 152 |
public function payment_requires_action() { |
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Get any data that is needed to perform the required action. |
| 158 |
* |
| 159 |
* @since 1.0.0 |
| 160 |
* |
| 161 |
* @return array |
| 162 |
*/ |
| 163 |
public function get_required_action_data() { |
| 164 |
return array(); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Whether the payment requires a redirect to a payment page. |
| 169 |
* |
| 170 |
* @since 1.0.0 |
| 171 |
* |
| 172 |
* @return boolean |
| 173 |
*/ |
| 174 |
public function payment_requires_redirect() { |
| 175 |
return true; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* The URL to redirect the donor to, or null if not required. |
| 180 |
* |
| 181 |
* @since 1.0.0 |
| 182 |
* |
| 183 |
* @param int $donation_id The donation ID. |
| 184 |
* @return string|null |
| 185 |
*/ |
| 186 |
public function get_redirect( $donation_id = false ) { |
| 187 |
// If it's recurring we need to add in the recurring meta as well. |
| 188 |
if ( charitable_is_debug() ) { |
| 189 |
// phpcs:disable |
| 190 |
error_log( 'charitable_square_payment_response - get_redirect' ); |
| 191 |
error_log( 'the donation id is ' ); |
| 192 |
error_log( print_r( $donation_id, true ) ); // David |
| 193 |
error_log( 'the response is ' ); |
| 194 |
error_log( print_r( $this->response, true ) ); // David |
| 195 |
// phpcs:enable |
| 196 |
} |
| 197 |
if ( $donation_id ) { |
| 198 |
$checkout_page_url = home_url( 'donation-receipt/' . intval( $donation_id ) . '/' ); |
| 199 |
} else { |
| 200 |
$checkout_page_url = isset( $this->response->checkout->checkout_page_url ) ? $this->response->checkout->checkout_page_url : home_url(); |
| 201 |
} |
| 202 |
return $checkout_page_url; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Returns whether the payment failed. |
| 207 |
* |
| 208 |
* @since 1.0.0 |
| 209 |
* |
| 210 |
* @return boolean |
| 211 |
*/ |
| 212 |
public function payment_failed() { |
| 213 |
if ( isset( $this->response->status ) ) { |
| 214 |
return 'failed' === $this->response->status; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Returns whether the payment was completed. |
| 220 |
* |
| 221 |
* @since 1.0.0 |
| 222 |
* |
| 223 |
* @return boolean |
| 224 |
*/ |
| 225 |
public function payment_completed() { |
| 226 |
if ( isset( $this->response->status ) ) { |
| 227 |
return 'paid' === $this->response->status; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Returns whether the payment was cancelled. |
| 233 |
* |
| 234 |
* @since 1.0.0 |
| 235 |
* |
| 236 |
* @return boolean |
| 237 |
*/ |
| 238 |
public function payment_cancelled() { |
| 239 |
if ( isset( $this->response->status ) ) { |
| 240 |
return in_array( |
| 241 |
$this->response->status, |
| 242 |
array( 'canceled', 'expired' ), |
| 243 |
true |
| 244 |
); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Returns any log messages to be added for the payment. |
| 250 |
* |
| 251 |
* @since 1.0.0 |
| 252 |
* |
| 253 |
* @return array |
| 254 |
*/ |
| 255 |
public function get_logs() { |
| 256 |
return array(); |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Returns any meta data to be recorded for the payment, beyond |
| 261 |
* the gateway transaction id. |
| 262 |
* |
| 263 |
* @since 1.0.0 |
| 264 |
* |
| 265 |
* @return array |
| 266 |
*/ |
| 267 |
public function get_meta() { |
| 268 |
return array(); |
| 269 |
} |
| 270 |
} |
| 271 |
|