$this->add_admin_notice( esc_html__( 'Payment successfully updated.', 'paid-member-subscriptions' ), 'updated' ); } // Adding a new payment manually if ( $action == 'add_payment' ) { if (!$this->validate_payment_data()) { return; } /** * If there are no errors proceed with inserting the payment in the db and adding the member (or updating his subscription) * */ if ( !empty($_POST) ) { $form_data = pms_array_sanitize_text_field( $_POST ); if ( !empty( $form_data['user_id'] ) ) { $member = pms_get_member( $form_data['user_id'] ); if ( is_object($member) && !empty($member) ) { $subscription_plan = pms_get_subscription_plan( $form_data['pms-payment-subscription-id'] ); $member_subscription_data = $member->get_subscription( $form_data['pms-payment-subscription-id'] ); if ( !empty($subscription_plan) ) { // Set member subscription status based on payment status switch ( $form_data['pms-payment-status'] ) { case 'failed' : case 'pending' : $member_subscription_status = 'pending'; break; case 'refunded' : $member_subscription_status = 'canceled'; break; default: $member_subscription_status = 'active'; } if( !empty( $member_subscription_data['id'] ) ){ // Subscription exists, extend duration if the payment is completed if( $form_data['pms-payment-status'] == 'completed' ){ $member_subscription = pms_get_member_subscription( $member_subscription_data['id'] ); if( !empty( $member_subscription ) ){ if( $subscription_plan->is_fixed_period_membership() ){ $data = array( 'expiration_date' => ( $subscription_plan->fixed_period_renewal_allowed() ) ? date( 'Y-m-d 23:59:59', strtotime( $member_subscription->expiration_date . '+ 1 year' ) ) : date( 'Y-m-d 23:59:59', strtotime( $member_subscription->expiration_date ) ), 'status' => $member_subscription_status ); } else{ $data = array( 'expiration_date' => date( 'Y-m-d 23:59:59', strtotime( $member_subscription->expiration_date . '+' . $subscription_plan->duration . ' ' . $subscription_plan->duration_unit ) ), 'status' => $member_subscription_status ); } $member_subscription->update( $data ); pms_add_member_subscription_log( $member_subscription->id, 'admin_subscription_activated_payments' ); } } } else { // User does not have this subscription, so add it $data = array( 'user_id' => $member->user_id, 'subscription_plan_id' => $subscription_plan->id, 'start_date' => $form_data['pms-payment-date'], 'expiration_date' => $subscription_plan->get_expiration_date(), 'status' => $member_subscription_status ); $member_subscription = new PMS_Member_Subscription(); $subscription_id = $member_subscription->insert( $data ); pms_add_member_subscription_log( $subscription_id, 'admin_subscription_added_payments' ); } } } } $payment_data = array( 'user_id' => $form_data['user_id'], 'subscription_plan_id' => $form_data['pms-payment-subscription-id'], 'date' => $form_data['pms-payment-date'], 'amount' => $form_data['pms-payment-amount'], 'type' => $form_data['pms-payment-type'], 'currency' => pms_get_active_currency(), 'status' => $form_data['pms-payment-status'], 'transaction_id' => $form_data['pms-payment-transaction-id'], 'payment_gateway' => 'manual' ); $payment = new PMS_Payment(); $added = $payment->insert($payment_data); if ( $added ) { do_action( 'pms_manually_added_payment_success', $payment ); wp_redirect(add_query_arg(array('page' => $this->menu_slug, 'message' => '1', 'updated' => '1'), admin_url('admin.php'))); exit; } } // if ( !empty($_POST) ) } // Used to complete payments in a scenario with the Manual payment gateway if ( $action == 'complete_payment' ) { // Get payment id $payment_id = ( !empty( $_REQUEST['payment_id'] ) ? (int)$_REQUEST['payment_id'] : 0 ); // Do nothing if there's no payment to work with if( $payment_id == 0 ) return; $payment = pms_get_payment( $payment_id ); if( empty( $payment->id ) || $payment->status != 'pending' ) return; if( $payment->update( array( 'status' => 'completed' ) ) ) $this->add_admin_notice( esc_html__( 'Payment successfully completed.', 'paid-member-subscriptions' ), 'updated' ); } } /** * Method to validate payment data in case it's added manually by the admin * * return bool * */ public function validate_payment_data() { $request_data = $_REQUEST; //Check to see if the a username was selected (not empty) if ( empty($request_data['user_id']) ) { $this->add_admin_notice( esc_html__( 'Please select a user.', 'paid-member-subscriptions' ), 'error' ); } else { // Check to see if the username exists $user = get_user_by( 'id', absint( $request_data['user_id'] ) ); if( !$user ) $this->add_admin_notice( esc_html__( 'It seems this user does not exist.', 'paid-member-subscriptions' ), 'error' ); } // Make sure a subscription plan was selected if ( empty($request_data['pms-payment-subscription-id']) ) { $this->add_admin_notice( esc_html__( 'Please select a subscription plan.', 'paid-member-subscriptions' ), 'error' ); } // Make sure the payment date is not empty if ( empty($request_data['pms-payment-date']) ){ $this->add_admin_notice( esc_html__( 'Please enter a date for the payment.', 'paid-member-subscriptions' ), 'error' ); } // Make sure we can add the selected subscription to this user (it doesn't already have one from the same group) $member = pms_get_member( absint( $request_data['user_id'] ) ); if ( is_object($member) && !empty($member) && isset( $request_data['pms-payment-subscription-id'] ) ) { $subscription_plan = pms_get_subscription_plan( absint( $request_data['pms-payment-subscription-id'] ) ); if ( !empty($member->subscriptions) && !empty($subscription_plan) ) { foreach ($member->subscriptions as $member_subscription) { if ( ($member_subscription['subscription_plan_id'] != $subscription_plan->id) && ( pms_get_subscription_plans_group_parent_id($member_subscription['subscription_plan_id']) == pms_get_subscription_plans_group_parent_id($subscription_plan->id) ) ) { $existing_subscription = new PMS_Subscription_Plan($member_subscription['subscription_plan_id']); $this->add_admin_notice( sprintf(esc_html__('This user already has a subscription (%s) from the same group with the one you selected. Select it or remove it to be able to complete this payment.', 'paid-member-subscriptions'), $existing_subscription->name), 'error'); break; } } } } if( $this->has_admin_notice( 'error' ) ) return false; else return true; } /** * Method to output content in the custom page * */ public function output() { // Check if payments cron is defined and if not, add it if ( ! wp_next_scheduled( 'pms_cron_process_member_subscriptions_payments' ) ) wp_schedule_event( time(), 'daily', 'pms_cron_process_member_subscriptions_payments' ); // Display the edit payment view if( isset( $_GET['pms-action'] ) && ( $_GET['pms-action'] == 'edit_payment' || $_GET['pms-action'] == 'add_payment' ) ) include_once 'views/view-page-payments-add-new-edit.php'; // Display all payments table else include_once 'views/view-page-payments-list-table.php'; } /** * Method that returns the price of a subscription plan. * We use this when adding a new payment to automatically fill in the price based on the selected subscription plan * */ public function ajax_populate_subscription_price() { if( !isset( $_POST['subscription_plan_id'] ) ) echo ''; $subscription_plan_id = (int)sanitize_text_field( $_POST['subscription_plan_id'] ); if( ! empty( $subscription_plan_id ) ) { $subscription_plan = pms_get_subscription_plan( $subscription_plan_id ); // Apply tax when the payment is handled in backend via admin if( class_exists( 'PMS_IN_Tax' ) ){ $pms_tax = new PMS_IN_Tax; $tax_exempt = get_post_meta( $subscription_plan_id, 'pms_subscription_plan_tax_exempt', true ); } if( isset( $pms_tax ) && isset( $tax_exempt ) && !$tax_exempt ) echo esc_html( pms_sanitize_date( $pms_tax->calculate_tax_rate( $subscription_plan->price ) ) ); else echo esc_html( pms_sanitize_date( $subscription_plan->price ) ); } else echo ''; wp_die(); } public function ajax_check_payment_username() { if( empty( $_POST['username'] ) ){ echo 0; die(); } $user = get_user_by( 'login', sanitize_text_field( $_POST['username'] ) ); if( !empty( $user->ID ) ){ echo esc_html( $user->ID ); wp_die(); } echo 0; wp_die(); } /** * Method that adds Screen Options to Payments page * */ public function add_screen_options() { $args = array( 'label' => 'Payments per page', 'default' => 10, 'option' => 'pms_payments_per_page' ); add_screen_option( 'per_page', $args ); } } $pms_submenu_page_payments = new PMS_Submenu_Page_Payments( 'paid-member-subscriptions', esc_html__( 'Payments', 'paid-member-subscriptions' ), esc_html__( 'Payments', 'paid-member-subscriptions' ), 'manage_options', 'pms-payments-page', 20, '', 'pms_payments_per_page' ); $pms_submenu_page_payments->init();