wp-pay-extensions /
memberpress
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Extension |
||
| 4 | * |
||
| 5 | * @author Pronamic <[email protected]> |
||
| 6 | * @copyright 2005-2019 Pronamic |
||
| 7 | * @license GPL-3.0-or-later |
||
| 8 | * @package Pronamic\WordPress\Pay\Extensions\MemberPress |
||
| 9 | */ |
||
| 10 | |||
| 11 | namespace Pronamic\WordPress\Pay\Extensions\MemberPress; |
||
| 12 | |||
| 13 | use MeprDb; |
||
| 14 | use MeprOptions; |
||
| 15 | use MeprProduct; |
||
| 16 | use MeprSubscription; |
||
| 17 | use MeprTransaction; |
||
| 18 | use MeprUtils; |
||
| 19 | use Pronamic\WordPress\Pay\Payments\PaymentStatus; |
||
| 20 | use Pronamic\WordPress\Pay\Extensions\MemberPress\Gateways\Gateway; |
||
| 21 | use Pronamic\WordPress\Pay\Payments\Payment; |
||
| 22 | use Pronamic\WordPress\Pay\Subscriptions\Subscription; |
||
| 23 | use Pronamic\WordPress\Pay\Subscriptions\SubscriptionStatus; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * WordPress pay MemberPress extension |
||
| 27 | * |
||
| 28 | * @author Remco Tolsma |
||
| 29 | * @version 2.0.4 |
||
| 30 | * @since 1.0.0 |
||
| 31 | */ |
||
| 32 | class Extension { |
||
| 33 | /** |
||
| 34 | * The slug of this addon |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | const SLUG = 'memberpress'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Bootstrap |
||
| 42 | */ |
||
| 43 | public static function bootstrap() { |
||
| 44 | new self(); |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Constructs and initializes the MemberPress extension. |
||
| 49 | */ |
||
| 50 | public function __construct() { |
||
| 51 | // @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprGatewayFactory.php#L48-50 |
||
| 52 | add_filter( 'mepr-gateway-paths', array( $this, 'gateway_paths' ) ); |
||
| 53 | |||
| 54 | add_filter( 'pronamic_payment_redirect_url_' . self::SLUG, array( __CLASS__, 'redirect_url' ), 10, 2 ); |
||
| 55 | add_action( 'pronamic_payment_status_update_' . self::SLUG, array( __CLASS__, 'status_update' ), 10, 1 ); |
||
| 56 | |||
| 57 | add_filter( 'pronamic_payment_source_text_' . self::SLUG, array( __CLASS__, 'source_text' ), 10, 2 ); |
||
| 58 | add_filter( 'pronamic_payment_source_description_' . self::SLUG, array( __CLASS__, 'source_description' ), 10, 2 ); |
||
| 59 | add_filter( 'pronamic_payment_source_url_' . self::SLUG, array( __CLASS__, 'source_url' ), 10, 2 ); |
||
| 60 | add_filter( 'pronamic_subscription_source_text_' . self::SLUG, array( __CLASS__, 'subscription_source_text' ), 10, 2 ); |
||
| 61 | add_filter( 'pronamic_subscription_source_description_' . self::SLUG, array( __CLASS__, 'subscription_source_description' ), 10, 2 ); |
||
| 62 | add_filter( 'pronamic_subscription_source_url_' . self::SLUG, array( __CLASS__, 'subscription_source_url' ), 10, 2 ); |
||
| 63 | |||
| 64 | add_action( 'mepr_subscription_pre_delete', array( $this, 'subscription_pre_delete' ), 10, 1 ); |
||
| 65 | |||
| 66 | add_action( 'mepr_subscription_transition_status', array( $this, 'memberpress_subscription_transition_status' ), 10, 3 ); |
||
| 67 | |||
| 68 | // Hide MemberPress columns for payments and subscriptions. |
||
| 69 | add_filter( 'registered_post_type', array( $this, 'post_type_columns_hide' ), 15, 1 ); |
||
| 70 | |||
| 71 | if ( is_admin() ) { |
||
| 72 | $this->admin_subscriptions = new Admin\AdminSubscriptions(); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 73 | $this->admin_transactions = new Admin\AdminTransactions(); |
||
|
0 ignored issues
–
show
|
|||
| 74 | |||
| 75 | $this->admin_subscriptions->setup(); |
||
| 76 | $this->admin_transactions->setup(); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Gateway paths. |
||
| 82 | * |
||
| 83 | * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/lib/MeprGatewayFactory.php#L48-50 |
||
| 84 | * |
||
| 85 | * @param array $paths Array with gateway paths. |
||
| 86 | * @return array |
||
| 87 | */ |
||
| 88 | public function gateway_paths( $paths ) { |
||
| 89 | $paths[] = dirname( __FILE__ ) . '/../gateways/'; |
||
| 90 | |||
| 91 | return $paths; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Hide MemberPress columns for payments and subscriptions. |
||
| 96 | * |
||
| 97 | * @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/controllers/MeprAppCtrl.php#L129-146 |
||
| 98 | * |
||
| 99 | * @param string $post_type Registered post type. |
||
| 100 | * |
||
| 101 | * @return void |
||
| 102 | */ |
||
| 103 | public function post_type_columns_hide( $post_type ) { |
||
| 104 | if ( ! in_array( $post_type, array( 'pronamic_payment', 'pronamic_pay_subscr' ), true ) ) { |
||
| 105 | return; |
||
| 106 | } |
||
| 107 | |||
| 108 | remove_filter( 'manage_edit-' . $post_type . '_columns', 'MeprAppCtrl::columns' ); |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Payment redirect URL filter. |
||
| 113 | * |
||
| 114 | * @since 1.0.1 |
||
| 115 | * |
||
| 116 | * @param string $url Payment redirect URL. |
||
| 117 | * @param Payment $payment Payment to redirect for. |
||
| 118 | * |
||
| 119 | * @return string |
||
| 120 | */ |
||
| 121 | public static function redirect_url( $url, Payment $payment ) { |
||
| 122 | global $transaction; |
||
| 123 | |||
| 124 | $transaction_id = $payment->get_source_id(); |
||
| 125 | |||
| 126 | $transaction = new MeprTransaction( $transaction_id ); |
||
| 127 | |||
| 128 | switch ( $payment->get_status() ) { |
||
| 129 | case PaymentStatus::CANCELLED: |
||
| 130 | case PaymentStatus::EXPIRED: |
||
| 131 | case PaymentStatus::FAILURE: |
||
| 132 | $product = $transaction->product(); |
||
| 133 | |||
| 134 | $url = add_query_arg( |
||
| 135 | array( |
||
| 136 | 'action' => 'payment_form', |
||
| 137 | 'txn' => $transaction->trans_num, |
||
| 138 | 'errors' => array( |
||
| 139 | __( 'Payment failed. Please try again.', 'pronamic_ideal' ), |
||
| 140 | ), |
||
| 141 | '_wpnonce' => wp_create_nonce( 'mepr_payment_form' ), |
||
| 142 | ), |
||
| 143 | $product->url() |
||
| 144 | ); |
||
| 145 | |||
| 146 | break; |
||
| 147 | case PaymentStatus::SUCCESS: |
||
| 148 | // @link https://gitlab.com/pronamic/memberpress/blob/1.2.4/app/models/MeprOptions.php#L768-782 |
||
| 149 | $mepr_options = MeprOptions::fetch(); |
||
| 150 | |||
| 151 | $product = new MeprProduct( $transaction->product_id ); |
||
| 152 | $sanitized_title = sanitize_title( $product->post_title ); |
||
| 153 | |||
| 154 | $args = array( |
||
| 155 | 'membership_id' => $product->ID, |
||
| 156 | 'membership' => $sanitized_title, |
||
| 157 | 'trans_num' => $transaction->trans_num, |
||
| 158 | ); |
||
| 159 | |||
| 160 | $url = $mepr_options->thankyou_page_url( http_build_query( $args ) ); |
||
| 161 | |||
| 162 | break; |
||
| 163 | case PaymentStatus::OPEN: |
||
| 164 | default: |
||
| 165 | break; |
||
| 166 | } |
||
| 167 | |||
| 168 | return $url; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Update lead status of the specified payment. |
||
| 173 | * |
||
| 174 | * @link https://github.com/Charitable/Charitable/blob/1.1.4/includes/gateways/class-charitable-gateway-paypal.php#L229-L357 |
||
| 175 | * |
||
| 176 | * @param Payment $payment The payment whose status is updated. |
||
| 177 | */ |
||
| 178 | public static function status_update( Payment $payment ) { |
||
| 179 | $transaction = new MeprTransaction( $payment->get_source_id() ); |
||
| 180 | |||
| 181 | if ( $payment->get_recurring() ) { |
||
| 182 | $subscription_id = $payment->get_subscription()->get_source_id(); |
||
| 183 | $subscription = new MeprSubscription( $subscription_id ); |
||
| 184 | |||
| 185 | // Same source ID and first transaction ID for recurring payment means we need to add a new transaction. |
||
| 186 | if ( $payment->get_source_id() === $subscription->id ) { |
||
| 187 | // First transaction. |
||
| 188 | $first_txn = $subscription->first_txn(); |
||
| 189 | |||
| 190 | if ( false === $first_txn || ! ( $first_txn instanceof MeprTransaction ) ) { |
||
| 191 | $first_txn = new MeprTransaction(); |
||
| 192 | $first_txn->user_id = $subscription->user_id; |
||
| 193 | $first_txn->product_id = $subscription->product_id; |
||
| 194 | $first_txn->coupon_id = $subscription->coupon_id; |
||
| 195 | $first_txn->gateway = null; |
||
| 196 | } |
||
| 197 | |||
| 198 | // Transaction number. |
||
| 199 | $trans_num = $payment->get_transaction_id(); |
||
| 200 | |||
| 201 | if ( empty( $trans_num ) ) { |
||
| 202 | $trans_num = uniqid(); |
||
| 203 | } |
||
| 204 | |||
| 205 | // New transaction. |
||
| 206 | $transaction = new MeprTransaction(); |
||
| 207 | $transaction->created_at = $payment->post->post_date_gmt; |
||
| 208 | $transaction->user_id = $first_txn->user_id; |
||
| 209 | $transaction->product_id = $first_txn->product_id; |
||
| 210 | $transaction->coupon_id = $first_txn->coupon_id; |
||
| 211 | $transaction->gateway = $first_txn->gateway; |
||
| 212 | $transaction->trans_num = $trans_num; |
||
| 213 | $transaction->txn_type = MeprTransaction::$payment_str; |
||
| 214 | $transaction->status = MeprTransaction::$pending_str; |
||
| 215 | $transaction->subscription_id = $subscription->id; |
||
| 216 | |||
| 217 | $transaction->set_gross( $payment->get_total_amount()->get_value() ); |
||
| 218 | |||
| 219 | $transaction->store(); |
||
| 220 | |||
| 221 | // Set source ID. |
||
| 222 | $payment->set_meta( 'source_id', $transaction->id ); |
||
| 223 | |||
| 224 | $payment->source_id = $transaction->id; |
||
| 225 | |||
| 226 | if ( MeprSubscription::$active_str === $subscription->status ) { |
||
| 227 | /* |
||
| 228 | * We create a 'confirmed' 'subscription_confirmation' |
||
| 229 | * transaction for a grace period of 15 days. |
||
| 230 | * |
||
| 231 | * Transactions of type "subscription_confirmation" with a |
||
| 232 | * status of "confirmed" are hidden in the UI, and are used |
||
| 233 | * as a way to provide free trial periods and the 24 hour |
||
| 234 | * grace period on a recurring subscription signup. |
||
| 235 | * |
||
| 236 | * @link https://docs.memberpress.com/article/219-where-is-data-stored. |
||
| 237 | */ |
||
| 238 | $subscription_confirmation = new MeprTransaction(); |
||
| 239 | $subscription_confirmation->created_at = $payment->post->post_date_gmt; |
||
| 240 | $subscription_confirmation->user_id = $first_txn->user_id; |
||
| 241 | $subscription_confirmation->product_id = $first_txn->product_id; |
||
| 242 | $subscription_confirmation->coupon_id = $first_txn->coupon_id; |
||
| 243 | $subscription_confirmation->gateway = $first_txn->gateway; |
||
| 244 | $subscription_confirmation->trans_num = $trans_num; |
||
| 245 | $subscription_confirmation->txn_type = MeprTransaction::$subscription_confirmation_str; |
||
| 246 | $subscription_confirmation->status = MeprTransaction::$confirmed_str; |
||
| 247 | $subscription_confirmation->subscription_id = $subscription->id; |
||
| 248 | $subscription_confirmation->expires_at = MeprUtils::ts_to_mysql_date( strtotime( $payment->post->post_date_gmt ) + MeprUtils::days( 15 ), 'Y-m-d 23:59:59' ); |
||
|
0 ignored issues
–
show
|
|||
| 249 | |||
| 250 | $subscription_confirmation->set_subtotal( 0.00 ); |
||
| 251 | |||
| 252 | $subscription_confirmation->store(); |
||
| 253 | } |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | $should_update = ! MemberPress::transaction_has_status( |
||
| 258 | $transaction, |
||
| 259 | array( |
||
| 260 | MeprTransaction::$failed_str, |
||
| 261 | MeprTransaction::$complete_str, |
||
| 262 | ) |
||
| 263 | ); |
||
| 264 | |||
| 265 | // Allow successful recurring payments to update failed transaction. |
||
| 266 | if ( $payment->get_recurring() && PaymentStatus::SUCCESS === $payment->get_status() && MeprTransaction::$failed_str === $transaction->status ) { |
||
| 267 | $should_update = true; |
||
| 268 | } |
||
| 269 | |||
| 270 | if ( $should_update ) { |
||
| 271 | $gateway = new Gateway(); |
||
| 272 | |||
| 273 | $gateway->pronamic_payment = $payment; |
||
| 274 | $gateway->mp_txn = $transaction; |
||
| 275 | |||
| 276 | switch ( $payment->get_status() ) { |
||
| 277 | case PaymentStatus::CANCELLED: |
||
| 278 | case PaymentStatus::EXPIRED: |
||
| 279 | case PaymentStatus::FAILURE: |
||
| 280 | $gateway->record_payment_failure(); |
||
| 281 | |||
| 282 | break; |
||
| 283 | case PaymentStatus::SUCCESS: |
||
| 284 | if ( $payment->get_recurring() ) { |
||
| 285 | $gateway->record_subscription_payment(); |
||
| 286 | } else { |
||
| 287 | $gateway->record_payment(); |
||
| 288 | } |
||
| 289 | |||
| 290 | break; |
||
| 291 | case PaymentStatus::OPEN: |
||
| 292 | default: |
||
| 293 | break; |
||
| 294 | } |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Subscription deleted. |
||
| 300 | * |
||
| 301 | * @param int $subscription_id MemberPress subscription id. |
||
| 302 | */ |
||
| 303 | public function subscription_pre_delete( $subscription_id ) { |
||
| 304 | $subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', $subscription_id ); |
||
| 305 | |||
| 306 | if ( ! $subscription ) { |
||
| 307 | return; |
||
| 308 | } |
||
| 309 | |||
| 310 | // Add note. |
||
| 311 | $note = sprintf( |
||
| 312 | /* translators: %s: MemberPress */ |
||
| 313 | __( '%s subscription deleted.', 'pronamic_ideal' ), |
||
| 314 | __( 'MemberPress', 'pronamic_ideal' ) |
||
| 315 | ); |
||
| 316 | |||
| 317 | $subscription->add_note( $note ); |
||
| 318 | |||
| 319 | // The status of canceled or completed subscriptions will not be changed automatically. |
||
| 320 | if ( ! in_array( $subscription->get_status(), array( SubscriptionStatus::CANCELLED, SubscriptionStatus::COMPLETED ), true ) ) { |
||
| 321 | $subscription->set_status( SubscriptionStatus::CANCELLED ); |
||
| 322 | |||
| 323 | $subscription->save(); |
||
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Source text. |
||
| 329 | * |
||
| 330 | * @param string $text Source text. |
||
| 331 | * @param Payment $payment Payment to create the source text for. |
||
| 332 | * |
||
| 333 | * @return string |
||
| 334 | */ |
||
| 335 | public static function source_text( $text, Payment $payment ) { |
||
| 336 | $text = __( 'MemberPress', 'pronamic_ideal' ) . '<br />'; |
||
| 337 | |||
| 338 | $text .= sprintf( |
||
| 339 | '<a href="%s">%s</a>', |
||
| 340 | add_query_arg( |
||
| 341 | array( |
||
| 342 | 'page' => 'memberpress-trans', |
||
| 343 | 'action' => 'edit', |
||
| 344 | 'id' => $payment->source_id, |
||
| 345 | ), |
||
| 346 | admin_url( 'admin.php' ) |
||
| 347 | ), |
||
| 348 | /* translators: %s: payment source id */ |
||
| 349 | sprintf( __( 'Transaction %s', 'pronamic_ideal' ), $payment->source_id ) |
||
| 350 | ); |
||
| 351 | |||
| 352 | return $text; |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Subscription source text. |
||
| 357 | * |
||
| 358 | * @param string $text Source text. |
||
| 359 | * @param Subscription $subscription Subscription to create the source text for. |
||
| 360 | * |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | public static function subscription_source_text( $text, Subscription $subscription ) { |
||
| 364 | $text = __( 'MemberPress', 'pronamic_ideal' ) . '<br />'; |
||
| 365 | |||
| 366 | $text .= sprintf( |
||
| 367 | '<a href="%s">%s</a>', |
||
| 368 | add_query_arg( |
||
| 369 | array( |
||
| 370 | 'page' => 'memberpress-subscriptions', |
||
| 371 | 'subscription' => $subscription->source_id, |
||
| 372 | ), |
||
| 373 | admin_url( 'admin.php' ) |
||
| 374 | ), |
||
| 375 | /* translators: %s: payment source id */ |
||
| 376 | sprintf( __( 'Subscription %s', 'pronamic_ideal' ), $subscription->source_id ) |
||
| 377 | ); |
||
| 378 | |||
| 379 | return $text; |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Source description. |
||
| 384 | * |
||
| 385 | * @param string $description Description. |
||
| 386 | * @param Payment $payment Payment to create the description for. |
||
| 387 | * |
||
| 388 | * @return string |
||
| 389 | */ |
||
| 390 | public static function source_description( $description, Payment $payment ) { |
||
| 391 | return __( 'MemberPress Transaction', 'pronamic_ideal' ); |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Subscription source description. |
||
| 396 | * |
||
| 397 | * @param string $description Description. |
||
| 398 | * @param Subscription $subscription Subscription to create the description for. |
||
| 399 | * |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | public static function subscription_source_description( $description, Subscription $subscription ) { |
||
| 403 | return __( 'MemberPress Subscription', 'pronamic_ideal' ); |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Source URL. |
||
| 408 | * |
||
| 409 | * @param string $url URL. |
||
| 410 | * @param Payment $payment The payment to create the source URL for. |
||
| 411 | * |
||
| 412 | * @return string |
||
| 413 | */ |
||
| 414 | public static function source_url( $url, Payment $payment ) { |
||
| 415 | $url = add_query_arg( |
||
| 416 | array( |
||
| 417 | 'page' => 'memberpress-trans', |
||
| 418 | 'action' => 'edit', |
||
| 419 | 'id' => $payment->source_id, |
||
| 420 | ), |
||
| 421 | admin_url( 'admin.php' ) |
||
| 422 | ); |
||
| 423 | |||
| 424 | return $url; |
||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Subscription source URL. |
||
| 429 | * |
||
| 430 | * @param string $url URL. |
||
| 431 | * @param Subscription $subscription Subscription. |
||
| 432 | * |
||
| 433 | * @return string |
||
| 434 | */ |
||
| 435 | public static function subscription_source_url( $url, Subscription $subscription ) { |
||
| 436 | $url = add_query_arg( |
||
| 437 | array( |
||
| 438 | 'page' => 'memberpress-subscriptions', |
||
| 439 | 'subscription' => $subscription->source_id, |
||
| 440 | ), |
||
| 441 | admin_url( 'admin.php' ) |
||
| 442 | ); |
||
| 443 | |||
| 444 | return $url; |
||
| 445 | } |
||
| 446 | |||
| 447 | /** |
||
| 448 | * MemberPress update subscription. |
||
| 449 | * |
||
| 450 | * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/controllers/MeprSubscriptionsCtrl.php#L92-L111 |
||
| 451 | * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprSubscription.php#L100-L123 |
||
| 452 | * @link https://github.com/wp-premium/memberpress-basic/blob/1.3.18/app/models/MeprSubscription.php#L112 |
||
| 453 | * |
||
| 454 | * @param string $status_old Old status identifier. |
||
| 455 | * @param string $status_new New status identifier. |
||
| 456 | * @param MeprSubscription $memberpress_subscription MemberPress subscription object. |
||
| 457 | */ |
||
| 458 | public function memberpress_subscription_transition_status( $status_old, $status_new, $memberpress_subscription ) { |
||
| 459 | $subscription = get_pronamic_subscription_by_meta( '_pronamic_subscription_source_id', $memberpress_subscription->id ); |
||
| 460 | |||
| 461 | if ( empty( $subscription ) ) { |
||
| 462 | return; |
||
| 463 | } |
||
| 464 | |||
| 465 | $status = SubscriptionStatuses::transform( $status_new ); |
||
| 466 | |||
| 467 | $subscription->set_status( $status ); |
||
| 468 | |||
| 469 | $subscription->save(); |
||
| 470 | } |
||
| 471 | } |
||
| 472 |