@@ -13,58 +13,58 @@ discard block |
||
| 13 | 13 | class GetPaid_Authorize_Net_Gateway extends GetPaid_Authorize_Net_Legacy_Gateway { |
| 14 | 14 | |
| 15 | 15 | /** |
| 16 | - * Payment method id. |
|
| 17 | - * |
|
| 18 | - * @var string |
|
| 19 | - */ |
|
| 16 | + * Payment method id. |
|
| 17 | + * |
|
| 18 | + * @var string |
|
| 19 | + */ |
|
| 20 | 20 | public $id = 'authorizenet'; |
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | - * An array of features that this gateway supports. |
|
| 24 | - * |
|
| 25 | - * @var array |
|
| 26 | - */ |
|
| 23 | + * An array of features that this gateway supports. |
|
| 24 | + * |
|
| 25 | + * @var array |
|
| 26 | + */ |
|
| 27 | 27 | protected $supports = array( 'subscription', 'sandbox', 'tokens', 'addons', 'single_subscription_group', 'multiple_subscription_groups', 'subscription_date_change' ); |
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | - * Payment method order. |
|
| 31 | - * |
|
| 32 | - * @var int |
|
| 33 | - */ |
|
| 30 | + * Payment method order. |
|
| 31 | + * |
|
| 32 | + * @var int |
|
| 33 | + */ |
|
| 34 | 34 | public $order = 4; |
| 35 | 35 | |
| 36 | 36 | /** |
| 37 | - * Endpoint for requests from Authorize.net. |
|
| 38 | - * |
|
| 39 | - * @var string |
|
| 40 | - */ |
|
| 41 | - protected $notify_url; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Endpoint for requests to Authorize.net. |
|
| 45 | - * |
|
| 46 | - * @var string |
|
| 47 | - */ |
|
| 37 | + * Endpoint for requests from Authorize.net. |
|
| 38 | + * |
|
| 39 | + * @var string |
|
| 40 | + */ |
|
| 41 | + protected $notify_url; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Endpoint for requests to Authorize.net. |
|
| 45 | + * |
|
| 46 | + * @var string |
|
| 47 | + */ |
|
| 48 | 48 | protected $endpoint; |
| 49 | 49 | |
| 50 | 50 | /** |
| 51 | - * Currencies this gateway is allowed for. |
|
| 52 | - * |
|
| 53 | - * @var array |
|
| 54 | - */ |
|
| 55 | - public $currencies = array( 'USD', 'CAD', 'GBP', 'DKK', 'NOK', 'PLN', 'SEK', 'AUD', 'EUR', 'NZD' ); |
|
| 51 | + * Currencies this gateway is allowed for. |
|
| 52 | + * |
|
| 53 | + * @var array |
|
| 54 | + */ |
|
| 55 | + public $currencies = array( 'USD', 'CAD', 'GBP', 'DKK', 'NOK', 'PLN', 'SEK', 'AUD', 'EUR', 'NZD' ); |
|
| 56 | 56 | |
| 57 | 57 | /** |
| 58 | - * URL to view a transaction. |
|
| 59 | - * |
|
| 60 | - * @var string |
|
| 61 | - */ |
|
| 58 | + * URL to view a transaction. |
|
| 59 | + * |
|
| 60 | + * @var string |
|
| 61 | + */ |
|
| 62 | 62 | public $view_transaction_url = 'https://{sandbox}authorize.net/ui/themes/sandbox/Transaction/TransactionReceipt.aspx?transid=%s'; |
| 63 | 63 | |
| 64 | 64 | /** |
| 65 | - * Class constructor. |
|
| 66 | - */ |
|
| 67 | - public function __construct() { |
|
| 65 | + * Class constructor. |
|
| 66 | + */ |
|
| 67 | + public function __construct() { |
|
| 68 | 68 | |
| 69 | 69 | $this->title = __( 'Credit Card / Debit Card', 'invoicing' ); |
| 70 | 70 | $this->method_title = __( 'Authorize.Net', 'invoicing' ); |
@@ -76,11 +76,11 @@ discard block |
||
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | /** |
| 79 | - * Displays the payment method select field. |
|
| 80 | - * |
|
| 81 | - * @param int $invoice_id 0 or invoice id. |
|
| 82 | - * @param GetPaid_Payment_Form $form Current payment form. |
|
| 83 | - */ |
|
| 79 | + * Displays the payment method select field. |
|
| 80 | + * |
|
| 81 | + * @param int $invoice_id 0 or invoice id. |
|
| 82 | + * @param GetPaid_Payment_Form $form Current payment form. |
|
| 83 | + */ |
|
| 84 | 84 | public function payment_fields( $invoice_id, $form ) { |
| 85 | 85 | |
| 86 | 86 | // Let the user select a payment method. |
@@ -91,16 +91,16 @@ discard block |
||
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | /** |
| 94 | - * Creates a customer profile. |
|
| 95 | - * |
|
| 96 | - * |
|
| 97 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 94 | + * Creates a customer profile. |
|
| 95 | + * |
|
| 96 | + * |
|
| 97 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 98 | 98 | * @param array $submission_data Posted checkout fields. |
| 99 | 99 | * @param bool $save Whether or not to save the payment as a token. |
| 100 | 100 | * @link https://developer.authorize.net/api/reference/index.html#customer-profiles-create-customer-profile |
| 101 | - * @return string|WP_Error Payment profile id. |
|
| 102 | - */ |
|
| 103 | - public function create_customer_profile( $invoice, $submission_data, $save = true ) { |
|
| 101 | + * @return string|WP_Error Payment profile id. |
|
| 102 | + */ |
|
| 103 | + public function create_customer_profile( $invoice, $submission_data, $save = true ) { |
|
| 104 | 104 | |
| 105 | 105 | // Remove non-digits from the number |
| 106 | 106 | $submission_data['authorizenet']['cc_number'] = preg_replace( '/\D/', '', $submission_data['authorizenet']['cc_number'] ); |
@@ -182,14 +182,14 @@ discard block |
||
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | /** |
| 185 | - * Retrieves a customer profile. |
|
| 186 | - * |
|
| 187 | - * |
|
| 188 | - * @param string $profile_id profile id. |
|
| 189 | - * @return string|WP_Error Profile id. |
|
| 185 | + * Retrieves a customer profile. |
|
| 186 | + * |
|
| 187 | + * |
|
| 188 | + * @param string $profile_id profile id. |
|
| 189 | + * @return string|WP_Error Profile id. |
|
| 190 | 190 | * @link https://developer.authorize.net/api/reference/index.html#customer-profiles-get-customer-profile |
| 191 | - */ |
|
| 192 | - public function get_customer_profile( $profile_id ) { |
|
| 191 | + */ |
|
| 192 | + public function get_customer_profile( $profile_id ) { |
|
| 193 | 193 | |
| 194 | 194 | // Generate args. |
| 195 | 195 | $args = array( |
@@ -204,17 +204,17 @@ discard block |
||
| 204 | 204 | } |
| 205 | 205 | |
| 206 | 206 | /** |
| 207 | - * Creates a customer profile. |
|
| 208 | - * |
|
| 209 | - * |
|
| 207 | + * Creates a customer profile. |
|
| 208 | + * |
|
| 209 | + * |
|
| 210 | 210 | * @param string $profile_id profile id. |
| 211 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 211 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 212 | 212 | * @param array $submission_data Posted checkout fields. |
| 213 | 213 | * @param bool $save Whether or not to save the payment as a token. |
| 214 | 214 | * @link https://developer.authorize.net/api/reference/index.html#customer-profiles-create-customer-profile |
| 215 | - * @return string|WP_Error Profile id. |
|
| 216 | - */ |
|
| 217 | - public function create_customer_payment_profile( $customer_profile, $invoice, $submission_data, $save ) { |
|
| 215 | + * @return string|WP_Error Profile id. |
|
| 216 | + */ |
|
| 217 | + public function create_customer_payment_profile( $customer_profile, $invoice, $submission_data, $save ) { |
|
| 218 | 218 | |
| 219 | 219 | // Remove non-digits from the number |
| 220 | 220 | $submission_data['authorizenet']['cc_number'] = preg_replace( '/\D/', '', $submission_data['authorizenet']['cc_number'] ); |
@@ -302,13 +302,13 @@ discard block |
||
| 302 | 302 | } |
| 303 | 303 | |
| 304 | 304 | /** |
| 305 | - * Retrieves payment details from cache. |
|
| 306 | - * |
|
| 307 | - * |
|
| 305 | + * Retrieves payment details from cache. |
|
| 306 | + * |
|
| 307 | + * |
|
| 308 | 308 | * @param array $payment_details. |
| 309 | - * @return array|false Profile id. |
|
| 310 | - */ |
|
| 311 | - public function retrieve_payment_profile_from_cache( $payment_details, $customer_profile, $invoice ) { |
|
| 309 | + * @return array|false Profile id. |
|
| 310 | + */ |
|
| 311 | + public function retrieve_payment_profile_from_cache( $payment_details, $customer_profile, $invoice ) { |
|
| 312 | 312 | |
| 313 | 313 | $cached_information = get_option( 'getpaid_authorize_net_cached_profiles', array() ); |
| 314 | 314 | $payment_details = hash_hmac( 'sha256', json_encode( $payment_details ), SECURE_AUTH_KEY ); |
@@ -333,13 +333,13 @@ discard block |
||
| 333 | 333 | } |
| 334 | 334 | |
| 335 | 335 | /** |
| 336 | - * Securely adds payment details to cache. |
|
| 337 | - * |
|
| 338 | - * |
|
| 336 | + * Securely adds payment details to cache. |
|
| 337 | + * |
|
| 338 | + * |
|
| 339 | 339 | * @param array $payment_details. |
| 340 | 340 | * @param string $payment_profile_id. |
| 341 | - */ |
|
| 342 | - public function add_payment_profile_to_cache( $payment_details, $payment_profile_id ) { |
|
| 341 | + */ |
|
| 342 | + public function add_payment_profile_to_cache( $payment_details, $payment_profile_id ) { |
|
| 343 | 343 | |
| 344 | 344 | $cached_information = get_option( 'getpaid_authorize_net_cached_profiles', array() ); |
| 345 | 345 | $cached_information = is_array( $cached_information ) ? $cached_information : array(); |
@@ -351,15 +351,15 @@ discard block |
||
| 351 | 351 | } |
| 352 | 352 | |
| 353 | 353 | /** |
| 354 | - * Retrieves a customer payment profile. |
|
| 355 | - * |
|
| 356 | - * |
|
| 357 | - * @param string $customer_profile_id customer profile id. |
|
| 354 | + * Retrieves a customer payment profile. |
|
| 355 | + * |
|
| 356 | + * |
|
| 357 | + * @param string $customer_profile_id customer profile id. |
|
| 358 | 358 | * @param string $payment_profile_id payment profile id. |
| 359 | - * @return string|WP_Error Profile id. |
|
| 359 | + * @return string|WP_Error Profile id. |
|
| 360 | 360 | * @link https://developer.authorize.net/api/reference/index.html#customer-profiles-get-customer-payment-profile |
| 361 | - */ |
|
| 362 | - public function get_customer_payment_profile( $customer_profile_id, $payment_profile_id ) { |
|
| 361 | + */ |
|
| 362 | + public function get_customer_payment_profile( $customer_profile_id, $payment_profile_id ) { |
|
| 363 | 363 | |
| 364 | 364 | // Generate args. |
| 365 | 365 | $args = array( |
@@ -375,15 +375,15 @@ discard block |
||
| 375 | 375 | } |
| 376 | 376 | |
| 377 | 377 | /** |
| 378 | - * Charges a customer payment profile. |
|
| 379 | - * |
|
| 378 | + * Charges a customer payment profile. |
|
| 379 | + * |
|
| 380 | 380 | * @param string $customer_profile_id customer profile id. |
| 381 | 381 | * @param string $payment_profile_id payment profile id. |
| 382 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 382 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 383 | 383 | * @link https://developer.authorize.net/api/reference/index.html#payment-transactions-charge-a-customer-profile |
| 384 | - * @return WP_Error|object |
|
| 385 | - */ |
|
| 386 | - public function charge_customer_payment_profile( $customer_profile_id, $payment_profile_id, $invoice ) { |
|
| 384 | + * @return WP_Error|object |
|
| 385 | + */ |
|
| 386 | + public function charge_customer_payment_profile( $customer_profile_id, $payment_profile_id, $invoice ) { |
|
| 387 | 387 | |
| 388 | 388 | // Generate args. |
| 389 | 389 | $args = array( |
@@ -429,41 +429,41 @@ discard block |
||
| 429 | 429 | } |
| 430 | 430 | |
| 431 | 431 | /** |
| 432 | - * Processes a customer charge. |
|
| 433 | - * |
|
| 432 | + * Processes a customer charge. |
|
| 433 | + * |
|
| 434 | 434 | * @param stdClass $result Api response. |
| 435 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 436 | - */ |
|
| 437 | - public function process_charge_response( $result, $invoice ) { |
|
| 435 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 436 | + */ |
|
| 437 | + public function process_charge_response( $result, $invoice ) { |
|
| 438 | 438 | |
| 439 | 439 | wpinv_clear_errors(); |
| 440 | - $response_code = (int) $result->transactionResponse->responseCode; |
|
| 440 | + $response_code = (int) $result->transactionResponse->responseCode; |
|
| 441 | 441 | |
| 442 | - // Succeeded. |
|
| 443 | - if ( 1 == $response_code || 4 == $response_code ) { |
|
| 442 | + // Succeeded. |
|
| 443 | + if ( 1 == $response_code || 4 == $response_code ) { |
|
| 444 | 444 | |
| 445 | - // Maybe set a transaction id. |
|
| 446 | - if ( ! empty( $result->transactionResponse->transId ) ) { |
|
| 447 | - $invoice->set_transaction_id( $result->transactionResponse->transId ); |
|
| 448 | - } |
|
| 445 | + // Maybe set a transaction id. |
|
| 446 | + if ( ! empty( $result->transactionResponse->transId ) ) { |
|
| 447 | + $invoice->set_transaction_id( $result->transactionResponse->transId ); |
|
| 448 | + } |
|
| 449 | 449 | |
| 450 | - $invoice->add_note( sprintf( __( 'Authentication code: %1$s (%2$s).', 'invoicing' ), $result->transactionResponse->authCode, $result->transactionResponse->accountNumber ), false, false, true ); |
|
| 450 | + $invoice->add_note( sprintf( __( 'Authentication code: %1$s (%2$s).', 'invoicing' ), $result->transactionResponse->authCode, $result->transactionResponse->accountNumber ), false, false, true ); |
|
| 451 | 451 | |
| 452 | - if ( 1 == $response_code ) { |
|
| 453 | - return $invoice->mark_paid(); |
|
| 454 | - } |
|
| 452 | + if ( 1 == $response_code ) { |
|
| 453 | + return $invoice->mark_paid(); |
|
| 454 | + } |
|
| 455 | 455 | |
| 456 | - $invoice->set_status( 'wpi-onhold' ); |
|
| 457 | - $invoice->add_note( |
|
| 456 | + $invoice->set_status( 'wpi-onhold' ); |
|
| 457 | + $invoice->add_note( |
|
| 458 | 458 | sprintf( |
| 459 | 459 | __( 'Held for review: %s', 'invoicing' ), |
| 460 | 460 | $result->transactionResponse->messages->message[0]->description |
| 461 | 461 | ) |
| 462 | - ); |
|
| 462 | + ); |
|
| 463 | 463 | |
| 464 | - return $invoice->save(); |
|
| 464 | + return $invoice->save(); |
|
| 465 | 465 | |
| 466 | - } |
|
| 466 | + } |
|
| 467 | 467 | |
| 468 | 468 | wpinv_set_error( 'card_declined' ); |
| 469 | 469 | |
@@ -475,13 +475,13 @@ discard block |
||
| 475 | 475 | } |
| 476 | 476 | |
| 477 | 477 | /** |
| 478 | - * Returns payment information. |
|
| 479 | - * |
|
| 480 | - * |
|
| 481 | - * @param array $card Card details. |
|
| 482 | - * @return array |
|
| 483 | - */ |
|
| 484 | - public function get_payment_information( $card ) { |
|
| 478 | + * Returns payment information. |
|
| 479 | + * |
|
| 480 | + * |
|
| 481 | + * @param array $card Card details. |
|
| 482 | + * @return array |
|
| 483 | + */ |
|
| 484 | + public function get_payment_information( $card ) { |
|
| 485 | 485 | return array( |
| 486 | 486 | |
| 487 | 487 | 'creditCard' => array( |
@@ -494,25 +494,25 @@ discard block |
||
| 494 | 494 | } |
| 495 | 495 | |
| 496 | 496 | /** |
| 497 | - * Returns the customer profile meta name. |
|
| 498 | - * |
|
| 499 | - * |
|
| 500 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 501 | - * @return string |
|
| 502 | - */ |
|
| 503 | - public function get_customer_profile_meta_name( $invoice ) { |
|
| 497 | + * Returns the customer profile meta name. |
|
| 498 | + * |
|
| 499 | + * |
|
| 500 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 501 | + * @return string |
|
| 502 | + */ |
|
| 503 | + public function get_customer_profile_meta_name( $invoice ) { |
|
| 504 | 504 | return $this->is_sandbox( $invoice ) ? 'getpaid_authorizenet_sandbox_customer_profile_id' : 'getpaid_authorizenet_customer_profile_id'; |
| 505 | 505 | } |
| 506 | 506 | |
| 507 | 507 | /** |
| 508 | - * Validates the submitted data. |
|
| 509 | - * |
|
| 510 | - * |
|
| 511 | - * @param array $submission_data Posted checkout fields. |
|
| 508 | + * Validates the submitted data. |
|
| 509 | + * |
|
| 510 | + * |
|
| 511 | + * @param array $submission_data Posted checkout fields. |
|
| 512 | 512 | * @param WPInv_Invoice $invoice |
| 513 | - * @return WP_Error|string The payment profile id |
|
| 514 | - */ |
|
| 515 | - public function validate_submission_data( $submission_data, $invoice ) { |
|
| 513 | + * @return WP_Error|string The payment profile id |
|
| 514 | + */ |
|
| 515 | + public function validate_submission_data( $submission_data, $invoice ) { |
|
| 516 | 516 | |
| 517 | 517 | // Validate authentication details. |
| 518 | 518 | $auth = $this->get_auth_params(); |
@@ -544,13 +544,13 @@ discard block |
||
| 544 | 544 | } |
| 545 | 545 | |
| 546 | 546 | /** |
| 547 | - * Returns invoice line items. |
|
| 548 | - * |
|
| 549 | - * |
|
| 550 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 551 | - * @return array |
|
| 552 | - */ |
|
| 553 | - public function get_line_items( $invoice ) { |
|
| 547 | + * Returns invoice line items. |
|
| 548 | + * |
|
| 549 | + * |
|
| 550 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 551 | + * @return array |
|
| 552 | + */ |
|
| 553 | + public function get_line_items( $invoice ) { |
|
| 554 | 554 | $items = array(); |
| 555 | 555 | |
| 556 | 556 | foreach ( $invoice->get_items() as $item ) { |
@@ -587,15 +587,15 @@ discard block |
||
| 587 | 587 | } |
| 588 | 588 | |
| 589 | 589 | /** |
| 590 | - * Process Payment. |
|
| 591 | - * |
|
| 592 | - * |
|
| 593 | - * @param WPInv_Invoice $invoice Invoice. |
|
| 594 | - * @param array $submission_data Posted checkout fields. |
|
| 595 | - * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
| 596 | - * @return array |
|
| 597 | - */ |
|
| 598 | - public function process_payment( $invoice, $submission_data, $submission ) { |
|
| 590 | + * Process Payment. |
|
| 591 | + * |
|
| 592 | + * |
|
| 593 | + * @param WPInv_Invoice $invoice Invoice. |
|
| 594 | + * @param array $submission_data Posted checkout fields. |
|
| 595 | + * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
| 596 | + * @return array |
|
| 597 | + */ |
|
| 598 | + public function process_payment( $invoice, $submission_data, $submission ) { |
|
| 599 | 599 | |
| 600 | 600 | // Validate the submitted data. |
| 601 | 601 | $payment_profile_id = $this->validate_submission_data( $submission_data, $invoice ); |
@@ -628,45 +628,45 @@ discard block |
||
| 628 | 628 | |
| 629 | 629 | exit; |
| 630 | 630 | |
| 631 | - } |
|
| 631 | + } |
|
| 632 | 632 | |
| 633 | - /** |
|
| 634 | - * Processes the initial payment. |
|
| 635 | - * |
|
| 633 | + /** |
|
| 634 | + * Processes the initial payment. |
|
| 635 | + * |
|
| 636 | 636 | * @param WPInv_Invoice $invoice Invoice. |
| 637 | - */ |
|
| 638 | - protected function process_initial_payment( $invoice ) { |
|
| 637 | + */ |
|
| 638 | + protected function process_initial_payment( $invoice ) { |
|
| 639 | 639 | |
| 640 | - $payment_profile_id = get_post_meta( $invoice->get_id(), 'getpaid_authorizenet_profile_id', true ); |
|
| 640 | + $payment_profile_id = get_post_meta( $invoice->get_id(), 'getpaid_authorizenet_profile_id', true ); |
|
| 641 | 641 | $customer_profile = get_user_meta( $invoice->get_user_id(), $this->get_customer_profile_meta_name( $invoice ), true ); |
| 642 | - $result = $this->charge_customer_payment_profile( $customer_profile, $payment_profile_id, $invoice ); |
|
| 642 | + $result = $this->charge_customer_payment_profile( $customer_profile, $payment_profile_id, $invoice ); |
|
| 643 | 643 | |
| 644 | - // Do we have an error? |
|
| 645 | - if ( is_wp_error( $result ) ) { |
|
| 646 | - wpinv_set_error( $result->get_error_code(), $result->get_error_message() ); |
|
| 647 | - wpinv_send_back_to_checkout( $invoice ); |
|
| 648 | - } |
|
| 644 | + // Do we have an error? |
|
| 645 | + if ( is_wp_error( $result ) ) { |
|
| 646 | + wpinv_set_error( $result->get_error_code(), $result->get_error_message() ); |
|
| 647 | + wpinv_send_back_to_checkout( $invoice ); |
|
| 648 | + } |
|
| 649 | 649 | |
| 650 | - // Process the response. |
|
| 651 | - $this->process_charge_response( $result, $invoice ); |
|
| 650 | + // Process the response. |
|
| 651 | + $this->process_charge_response( $result, $invoice ); |
|
| 652 | 652 | |
| 653 | - if ( wpinv_get_errors() ) { |
|
| 654 | - wpinv_send_back_to_checkout( $invoice ); |
|
| 655 | - } |
|
| 653 | + if ( wpinv_get_errors() ) { |
|
| 654 | + wpinv_send_back_to_checkout( $invoice ); |
|
| 655 | + } |
|
| 656 | 656 | |
| 657 | - } |
|
| 657 | + } |
|
| 658 | 658 | |
| 659 | 659 | /** |
| 660 | - * Processes recurring payments. |
|
| 661 | - * |
|
| 660 | + * Processes recurring payments. |
|
| 661 | + * |
|
| 662 | 662 | * @param WPInv_Invoice $invoice Invoice. |
| 663 | 663 | * @param WPInv_Subscription[]|WPInv_Subscription $subscriptions Subscriptions. |
| 664 | - */ |
|
| 665 | - public function process_subscription( $invoice, $subscriptions ) { |
|
| 664 | + */ |
|
| 665 | + public function process_subscription( $invoice, $subscriptions ) { |
|
| 666 | 666 | |
| 667 | 667 | // Check if there is an initial amount to charge. |
| 668 | 668 | if ( (float) $invoice->get_total() > 0 ) { |
| 669 | - $this->process_initial_payment( $invoice ); |
|
| 669 | + $this->process_initial_payment( $invoice ); |
|
| 670 | 670 | } |
| 671 | 671 | |
| 672 | 672 | // Activate the subscriptions. |
@@ -684,36 +684,36 @@ discard block |
||
| 684 | 684 | } |
| 685 | 685 | } |
| 686 | 686 | |
| 687 | - // Redirect to the success page. |
|
| 687 | + // Redirect to the success page. |
|
| 688 | 688 | wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) ); |
| 689 | 689 | |
| 690 | 690 | } |
| 691 | 691 | |
| 692 | - /** |
|
| 693 | - * (Maybe) renews an authorize.net subscription profile. |
|
| 694 | - * |
|
| 695 | - * |
|
| 692 | + /** |
|
| 693 | + * (Maybe) renews an authorize.net subscription profile. |
|
| 694 | + * |
|
| 695 | + * |
|
| 696 | 696 | * @param WPInv_Subscription $subscription |
| 697 | - */ |
|
| 698 | - public function maybe_renew_subscription( $subscription ) { |
|
| 697 | + */ |
|
| 698 | + public function maybe_renew_subscription( $subscription ) { |
|
| 699 | 699 | |
| 700 | 700 | // Ensure its our subscription && it's active. |
| 701 | 701 | if ( $this->id === $subscription->get_gateway() && $subscription->has_status( 'active trialling' ) ) { |
| 702 | 702 | $this->renew_subscription( $subscription ); |
| 703 | 703 | } |
| 704 | 704 | |
| 705 | - } |
|
| 705 | + } |
|
| 706 | 706 | |
| 707 | 707 | /** |
| 708 | - * Renews a subscription. |
|
| 709 | - * |
|
| 708 | + * Renews a subscription. |
|
| 709 | + * |
|
| 710 | 710 | * @param WPInv_Subscription $subscription |
| 711 | - */ |
|
| 712 | - public function renew_subscription( $subscription ) { |
|
| 711 | + */ |
|
| 712 | + public function renew_subscription( $subscription ) { |
|
| 713 | 713 | |
| 714 | - // Generate the renewal invoice. |
|
| 715 | - $new_invoice = $subscription->create_payment(); |
|
| 716 | - $old_invoice = $subscription->get_parent_payment(); |
|
| 714 | + // Generate the renewal invoice. |
|
| 715 | + $new_invoice = $subscription->create_payment(); |
|
| 716 | + $old_invoice = $subscription->get_parent_payment(); |
|
| 717 | 717 | |
| 718 | 718 | if ( empty( $new_invoice ) ) { |
| 719 | 719 | $old_invoice->add_note( __( 'Error generating a renewal invoice.', 'invoicing' ), false, false, false ); |
@@ -722,37 +722,37 @@ discard block |
||
| 722 | 722 | } |
| 723 | 723 | |
| 724 | 724 | // Charge the payment method. |
| 725 | - $payment_profile_id = get_post_meta( $old_invoice->get_id(), 'getpaid_authorizenet_profile_id', true ); |
|
| 726 | - $customer_profile = get_user_meta( $old_invoice->get_user_id(), $this->get_customer_profile_meta_name( $old_invoice ), true ); |
|
| 727 | - $result = $this->charge_customer_payment_profile( $customer_profile, $payment_profile_id, $new_invoice ); |
|
| 728 | - |
|
| 729 | - // Do we have an error? |
|
| 730 | - if ( is_wp_error( $result ) ) { |
|
| 731 | - |
|
| 732 | - $old_invoice->add_note( |
|
| 733 | - sprintf( __( 'Error renewing subscription : ( %s ).', 'invoicing' ), $result->get_error_message() ), |
|
| 734 | - true, |
|
| 735 | - false, |
|
| 736 | - true |
|
| 737 | - ); |
|
| 738 | - $subscription->failing(); |
|
| 739 | - return; |
|
| 740 | - |
|
| 741 | - } |
|
| 742 | - |
|
| 743 | - // Process the response. |
|
| 744 | - $this->process_charge_response( $result, $new_invoice ); |
|
| 745 | - |
|
| 746 | - if ( wpinv_get_errors() ) { |
|
| 747 | - |
|
| 748 | - $old_invoice->add_note( |
|
| 749 | - sprintf( __( 'Error renewing subscription : ( %s ).', 'invoicing' ), getpaid_get_errors_html() ), |
|
| 750 | - true, |
|
| 751 | - false, |
|
| 752 | - true |
|
| 753 | - ); |
|
| 754 | - $subscription->failing(); |
|
| 755 | - return; |
|
| 725 | + $payment_profile_id = get_post_meta( $old_invoice->get_id(), 'getpaid_authorizenet_profile_id', true ); |
|
| 726 | + $customer_profile = get_user_meta( $old_invoice->get_user_id(), $this->get_customer_profile_meta_name( $old_invoice ), true ); |
|
| 727 | + $result = $this->charge_customer_payment_profile( $customer_profile, $payment_profile_id, $new_invoice ); |
|
| 728 | + |
|
| 729 | + // Do we have an error? |
|
| 730 | + if ( is_wp_error( $result ) ) { |
|
| 731 | + |
|
| 732 | + $old_invoice->add_note( |
|
| 733 | + sprintf( __( 'Error renewing subscription : ( %s ).', 'invoicing' ), $result->get_error_message() ), |
|
| 734 | + true, |
|
| 735 | + false, |
|
| 736 | + true |
|
| 737 | + ); |
|
| 738 | + $subscription->failing(); |
|
| 739 | + return; |
|
| 740 | + |
|
| 741 | + } |
|
| 742 | + |
|
| 743 | + // Process the response. |
|
| 744 | + $this->process_charge_response( $result, $new_invoice ); |
|
| 745 | + |
|
| 746 | + if ( wpinv_get_errors() ) { |
|
| 747 | + |
|
| 748 | + $old_invoice->add_note( |
|
| 749 | + sprintf( __( 'Error renewing subscription : ( %s ).', 'invoicing' ), getpaid_get_errors_html() ), |
|
| 750 | + true, |
|
| 751 | + false, |
|
| 752 | + true |
|
| 753 | + ); |
|
| 754 | + $subscription->failing(); |
|
| 755 | + return; |
|
| 756 | 756 | |
| 757 | 757 | } |
| 758 | 758 | |
@@ -761,13 +761,13 @@ discard block |
||
| 761 | 761 | } |
| 762 | 762 | |
| 763 | 763 | /** |
| 764 | - * Processes invoice addons. |
|
| 765 | - * |
|
| 766 | - * @param WPInv_Invoice $invoice |
|
| 767 | - * @param GetPaid_Form_Item[] $items |
|
| 768 | - * @return WPInv_Invoice |
|
| 769 | - */ |
|
| 770 | - public function process_addons( $invoice, $items ) { |
|
| 764 | + * Processes invoice addons. |
|
| 765 | + * |
|
| 766 | + * @param WPInv_Invoice $invoice |
|
| 767 | + * @param GetPaid_Form_Item[] $items |
|
| 768 | + * @return WPInv_Invoice |
|
| 769 | + */ |
|
| 770 | + public function process_addons( $invoice, $items ) { |
|
| 771 | 771 | |
| 772 | 772 | global $getpaid_authorize_addons; |
| 773 | 773 | |
@@ -786,7 +786,7 @@ discard block |
||
| 786 | 786 | $invoice->recalculate_total(); |
| 787 | 787 | |
| 788 | 788 | $payment_profile_id = get_post_meta( $invoice->get_id(), 'getpaid_authorizenet_profile_id', true ); |
| 789 | - $customer_profile = get_user_meta( $invoice->get_user_id(), $this->get_customer_profile_meta_name( $invoice ), true ); |
|
| 789 | + $customer_profile = get_user_meta( $invoice->get_user_id(), $this->get_customer_profile_meta_name( $invoice ), true ); |
|
| 790 | 790 | |
| 791 | 791 | add_filter( 'getpaid_authorizenet_charge_customer_payment_profile_args', array( $this, 'filter_addons_request' ), 10, 2 ); |
| 792 | 792 | $result = $this->charge_customer_payment_profile( $customer_profile, $payment_profile_id, $invoice ); |
@@ -801,11 +801,11 @@ discard block |
||
| 801 | 801 | } |
| 802 | 802 | |
| 803 | 803 | /** |
| 804 | - * Processes invoice addons. |
|
| 805 | - * |
|
| 804 | + * Processes invoice addons. |
|
| 805 | + * |
|
| 806 | 806 | * @param array $args |
| 807 | - * @return array |
|
| 808 | - */ |
|
| 807 | + * @return array |
|
| 808 | + */ |
|
| 809 | 809 | public function filter_addons_request( $args ) { |
| 810 | 810 | |
| 811 | 811 | global $getpaid_authorize_addons; |
@@ -839,11 +839,11 @@ discard block |
||
| 839 | 839 | } |
| 840 | 840 | |
| 841 | 841 | /** |
| 842 | - * Filters the gateway settings. |
|
| 843 | - * |
|
| 844 | - * @param array $admin_settings |
|
| 845 | - */ |
|
| 846 | - public function admin_settings( $admin_settings ) { |
|
| 842 | + * Filters the gateway settings. |
|
| 843 | + * |
|
| 844 | + * @param array $admin_settings |
|
| 845 | + */ |
|
| 846 | + public function admin_settings( $admin_settings ) { |
|
| 847 | 847 | |
| 848 | 848 | $currencies = sprintf( |
| 849 | 849 | __( 'Supported Currencies: %s', 'invoicing' ), |
@@ -883,7 +883,7 @@ discard block |
||
| 883 | 883 | 'readonly' => true, |
| 884 | 884 | ); |
| 885 | 885 | |
| 886 | - return $admin_settings; |
|
| 887 | - } |
|
| 886 | + return $admin_settings; |
|
| 887 | + } |
|
| 888 | 888 | |
| 889 | 889 | } |
@@ -12,144 +12,144 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class GetPaid_Daily_Maintenance { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Class constructor. |
|
| 17 | - */ |
|
| 18 | - public function __construct() { |
|
| 19 | - |
|
| 20 | - // Clear deprecated events. |
|
| 21 | - add_action( 'wp', array( $this, 'maybe_clear_deprecated_events' ) ); |
|
| 22 | - |
|
| 23 | - // (Maybe) schedule a cron that runs daily. |
|
| 24 | - add_action( 'wp', array( $this, 'maybe_create_scheduled_event' ) ); |
|
| 25 | - |
|
| 26 | - // Fired everyday at 7 a.m (this might vary for sites with few visitors) |
|
| 27 | - add_action( 'getpaid_daily_maintenance', array( $this, 'log_cron_run' ) ); |
|
| 28 | - add_action( 'getpaid_daily_maintenance', array( $this, 'backwards_compat' ) ); |
|
| 29 | - add_action( 'getpaid_daily_maintenance', array( $this, 'maybe_expire_subscriptions' ) ); |
|
| 30 | - add_action( 'getpaid_daily_maintenance', array( $this, 'check_renewing_subscriptions' ) ); |
|
| 31 | - add_action( 'getpaid_daily_maintenance', array( $this, 'maybe_update_geoip_databases' ) ); |
|
| 32 | - |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Schedules a cron to run every day at 7 a.m |
|
| 37 | - * |
|
| 38 | - */ |
|
| 39 | - public function maybe_create_scheduled_event() { |
|
| 40 | - |
|
| 41 | - if ( ! wp_next_scheduled( 'getpaid_daily_maintenance' ) ) { |
|
| 42 | - $timestamp = strtotime( 'tomorrow 07:00:00', current_time( 'timestamp' ) ); |
|
| 43 | - wp_schedule_event( $timestamp, 'daily', 'getpaid_daily_maintenance' ); |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Clears deprecated events. |
|
| 50 | - * |
|
| 51 | - */ |
|
| 52 | - public function maybe_clear_deprecated_events() { |
|
| 53 | - |
|
| 54 | - if ( ! get_option( 'wpinv_cleared_old_events' ) ) { |
|
| 55 | - wp_clear_scheduled_hook( 'wpinv_register_schedule_event_twicedaily' ); |
|
| 56 | - wp_clear_scheduled_hook( 'wpinv_register_schedule_event_daily' ); |
|
| 57 | - update_option( 'wpinv_cleared_old_events', 1 ); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Fires the old hook for backwards compatibility. |
|
| 64 | - * |
|
| 65 | - */ |
|
| 66 | - public function backwards_compat() { |
|
| 67 | - do_action( 'wpinv_register_schedule_event_daily' ); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Checks for subscriptions that are scheduled to renew. |
|
| 72 | - * |
|
| 73 | - */ |
|
| 74 | - public function check_renewing_subscriptions() { |
|
| 75 | - |
|
| 76 | - // Fetch subscriptions that expire today. |
|
| 77 | - $args = array( |
|
| 78 | - 'number' => -1, |
|
| 79 | - 'count_total' => false, |
|
| 80 | - 'status' => 'trialling active', |
|
| 81 | - 'date_expires_query' => array( |
|
| 82 | - array( |
|
| 83 | - 'year' => gmdate( 'Y' ), |
|
| 84 | - 'month' => gmdate( 'n' ), |
|
| 85 | - 'day' => gmdate( 'j' ), |
|
| 86 | - 'compare' => '=', |
|
| 87 | - ), |
|
| 88 | - ), |
|
| 89 | - ); |
|
| 90 | - |
|
| 91 | - $subscriptions = new GetPaid_Subscriptions_Query( $args ); |
|
| 92 | - |
|
| 93 | - foreach ( $subscriptions->get_results() as $subscription ) { |
|
| 94 | - |
|
| 95 | - /** @var WPInv_Subscription $subscription */ |
|
| 96 | - if ( $subscription->is_last_renewal() ) { |
|
| 97 | - $subscription->complete(); |
|
| 98 | - } else { |
|
| 99 | - do_action( 'getpaid_should_renew_subscription', $subscription ); |
|
| 100 | - } |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Expires expired subscriptions. |
|
| 107 | - * |
|
| 108 | - */ |
|
| 109 | - public function maybe_expire_subscriptions() { |
|
| 110 | - |
|
| 111 | - // Fetch expired subscriptions (skips those that expire today). |
|
| 112 | - $args = array( |
|
| 113 | - 'number' => -1, |
|
| 114 | - 'count_total' => false, |
|
| 115 | - 'status' => 'trialling active failing cancelled', |
|
| 116 | - 'date_expires_query' => array( |
|
| 117 | - 'before' => 'yesterday', |
|
| 118 | - 'inclusive' => false, |
|
| 119 | - ), |
|
| 120 | - ); |
|
| 121 | - |
|
| 122 | - $subscriptions = new GetPaid_Subscriptions_Query( $args ); |
|
| 123 | - |
|
| 124 | - foreach ( $subscriptions->get_results() as $subscription ) { |
|
| 125 | - if ( apply_filters( 'getpaid_daily_maintenance_should_expire_subscription', false, $subscription ) ) { |
|
| 126 | - $subscription->set_status( 'expired' ); |
|
| 127 | - $subscription->save(); |
|
| 128 | - } |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Logs cron runs. |
|
| 135 | - * |
|
| 136 | - */ |
|
| 137 | - public function log_cron_run() { |
|
| 138 | - wpinv_error_log( 'GetPaid Daily Cron', false ); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * Updates GeoIP databases. |
|
| 143 | - * |
|
| 144 | - */ |
|
| 145 | - public function maybe_update_geoip_databases() { |
|
| 146 | - $updated = get_transient( 'getpaid_updated_geoip_databases' ); |
|
| 147 | - |
|
| 148 | - if ( false === $updated ) { |
|
| 149 | - set_transient( 'getpaid_updated_geoip_databases', 1, 15 * DAY_IN_SECONDS ); |
|
| 150 | - do_action( 'getpaid_update_geoip_databases' ); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - } |
|
| 15 | + /** |
|
| 16 | + * Class constructor. |
|
| 17 | + */ |
|
| 18 | + public function __construct() { |
|
| 19 | + |
|
| 20 | + // Clear deprecated events. |
|
| 21 | + add_action( 'wp', array( $this, 'maybe_clear_deprecated_events' ) ); |
|
| 22 | + |
|
| 23 | + // (Maybe) schedule a cron that runs daily. |
|
| 24 | + add_action( 'wp', array( $this, 'maybe_create_scheduled_event' ) ); |
|
| 25 | + |
|
| 26 | + // Fired everyday at 7 a.m (this might vary for sites with few visitors) |
|
| 27 | + add_action( 'getpaid_daily_maintenance', array( $this, 'log_cron_run' ) ); |
|
| 28 | + add_action( 'getpaid_daily_maintenance', array( $this, 'backwards_compat' ) ); |
|
| 29 | + add_action( 'getpaid_daily_maintenance', array( $this, 'maybe_expire_subscriptions' ) ); |
|
| 30 | + add_action( 'getpaid_daily_maintenance', array( $this, 'check_renewing_subscriptions' ) ); |
|
| 31 | + add_action( 'getpaid_daily_maintenance', array( $this, 'maybe_update_geoip_databases' ) ); |
|
| 32 | + |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Schedules a cron to run every day at 7 a.m |
|
| 37 | + * |
|
| 38 | + */ |
|
| 39 | + public function maybe_create_scheduled_event() { |
|
| 40 | + |
|
| 41 | + if ( ! wp_next_scheduled( 'getpaid_daily_maintenance' ) ) { |
|
| 42 | + $timestamp = strtotime( 'tomorrow 07:00:00', current_time( 'timestamp' ) ); |
|
| 43 | + wp_schedule_event( $timestamp, 'daily', 'getpaid_daily_maintenance' ); |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Clears deprecated events. |
|
| 50 | + * |
|
| 51 | + */ |
|
| 52 | + public function maybe_clear_deprecated_events() { |
|
| 53 | + |
|
| 54 | + if ( ! get_option( 'wpinv_cleared_old_events' ) ) { |
|
| 55 | + wp_clear_scheduled_hook( 'wpinv_register_schedule_event_twicedaily' ); |
|
| 56 | + wp_clear_scheduled_hook( 'wpinv_register_schedule_event_daily' ); |
|
| 57 | + update_option( 'wpinv_cleared_old_events', 1 ); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Fires the old hook for backwards compatibility. |
|
| 64 | + * |
|
| 65 | + */ |
|
| 66 | + public function backwards_compat() { |
|
| 67 | + do_action( 'wpinv_register_schedule_event_daily' ); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Checks for subscriptions that are scheduled to renew. |
|
| 72 | + * |
|
| 73 | + */ |
|
| 74 | + public function check_renewing_subscriptions() { |
|
| 75 | + |
|
| 76 | + // Fetch subscriptions that expire today. |
|
| 77 | + $args = array( |
|
| 78 | + 'number' => -1, |
|
| 79 | + 'count_total' => false, |
|
| 80 | + 'status' => 'trialling active', |
|
| 81 | + 'date_expires_query' => array( |
|
| 82 | + array( |
|
| 83 | + 'year' => gmdate( 'Y' ), |
|
| 84 | + 'month' => gmdate( 'n' ), |
|
| 85 | + 'day' => gmdate( 'j' ), |
|
| 86 | + 'compare' => '=', |
|
| 87 | + ), |
|
| 88 | + ), |
|
| 89 | + ); |
|
| 90 | + |
|
| 91 | + $subscriptions = new GetPaid_Subscriptions_Query( $args ); |
|
| 92 | + |
|
| 93 | + foreach ( $subscriptions->get_results() as $subscription ) { |
|
| 94 | + |
|
| 95 | + /** @var WPInv_Subscription $subscription */ |
|
| 96 | + if ( $subscription->is_last_renewal() ) { |
|
| 97 | + $subscription->complete(); |
|
| 98 | + } else { |
|
| 99 | + do_action( 'getpaid_should_renew_subscription', $subscription ); |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Expires expired subscriptions. |
|
| 107 | + * |
|
| 108 | + */ |
|
| 109 | + public function maybe_expire_subscriptions() { |
|
| 110 | + |
|
| 111 | + // Fetch expired subscriptions (skips those that expire today). |
|
| 112 | + $args = array( |
|
| 113 | + 'number' => -1, |
|
| 114 | + 'count_total' => false, |
|
| 115 | + 'status' => 'trialling active failing cancelled', |
|
| 116 | + 'date_expires_query' => array( |
|
| 117 | + 'before' => 'yesterday', |
|
| 118 | + 'inclusive' => false, |
|
| 119 | + ), |
|
| 120 | + ); |
|
| 121 | + |
|
| 122 | + $subscriptions = new GetPaid_Subscriptions_Query( $args ); |
|
| 123 | + |
|
| 124 | + foreach ( $subscriptions->get_results() as $subscription ) { |
|
| 125 | + if ( apply_filters( 'getpaid_daily_maintenance_should_expire_subscription', false, $subscription ) ) { |
|
| 126 | + $subscription->set_status( 'expired' ); |
|
| 127 | + $subscription->save(); |
|
| 128 | + } |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Logs cron runs. |
|
| 135 | + * |
|
| 136 | + */ |
|
| 137 | + public function log_cron_run() { |
|
| 138 | + wpinv_error_log( 'GetPaid Daily Cron', false ); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * Updates GeoIP databases. |
|
| 143 | + * |
|
| 144 | + */ |
|
| 145 | + public function maybe_update_geoip_databases() { |
|
| 146 | + $updated = get_transient( 'getpaid_updated_geoip_databases' ); |
|
| 147 | + |
|
| 148 | + if ( false === $updated ) { |
|
| 149 | + set_transient( 'getpaid_updated_geoip_databases', 1, 15 * DAY_IN_SECONDS ); |
|
| 150 | + do_action( 'getpaid_update_geoip_databases' ); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + } |
|
| 154 | 154 | |
| 155 | 155 | } |