autoship_delete_general_payment_method

(src > payments.php) can be used to delete any payment method from QPilot. The function takes the 1. token_id, 2. WC token object, and 3. QPilot method type. It pulls the payment methods from QPilot and deletes the matching method. In addition it has two filters, autoship_delete_general_payment_method and autoship_delete{$type}payment_method filter both of which filter the boolean for if a Method matches between WC and QPilot. Both filters take the Boolean ( if there is a match using the default comparisons ), the QPilot Method Type, the WC Token, and the QPilot Payment Method object.

Example

/**
 * Deletes a payment method from QPilot for a customer
 * @param string $token_id Payment Token ID for this payment method
 * @param WC_Payment_Token_CC $token
 * @param string $type The Type for the Payment Method from QPilot.
 */
function autoship_delete_general_payment_method( $token_id, $token, $type = '' ) {

  // Get the WC Customer from the Token
 	$wc_customer_id       = $token->get_user_id();

  // Get the Autoship Customer ID.
	$autoship_customer_id = autoship_get_autoship_customer_id( $wc_customer_id, 'autoship_delete_general_payment' );

  // If not autoship bypass
 	if ( empty( $autoship_customer_id ) || empty( $type ) )
	return;

  // Get the method id from the token.
  $payment_method_id  = $token->get_token();

  // Get the Default QPilot Client.
	$client = autoship_get_default_client();

 	try {

    // Get the customer's payment methods from QPilot
		$payment_methods = $client->get_customer_payment_methods( $autoship_customer_id );

    // Iterate through the payment methods and delete the one that matches this type.
    foreach ( $payment_methods as $method ) {

      // If the token being deleted matches a token in QPilot Delete it.
      $valid = (
      $method->Type == $type
			&& $method->GatewayCustomerId == $wc_customer_id
			&& $method->GatewayPaymentId  == $payment_method_id );

      // Added a filter to allow for custom matches between QPilot and Autoship.
      // Takes the current matching boolean, the supplied QPilot Method Type, The current Token and the QPilot Payment Method object.
      $valid = apply_filters( 'autoship_delete_general_payment_method_qpilot_match', $valid, $type, $token, $method );
      $valid = apply_filters( "autoship_delete_{$type}_payment_method_qpilot_match", $valid, $type, $token, $method );

      if ( $valid ) {
				try {
 					// Delete the payment method
					$client->delete_payment_method( $method->Id );
        } catch ( Exception $e ) {
      		error_log( sprintf( 'Error deleting QPilot Payment Method. Additional Details: %s - %s', $e->getCode(), $e->getMessage() ) );
          return new WP_Error( 'Delete QPilot Payment Method Failed', __( $notice['desc'], "autoship" ) );
        }
				break;
    	}

		}

	} catch ( Exception $e ) {
    error_log( sprintf( 'Error retrieving QPilot Payment Methods. Additional Details: %s - %s', $e->getCode(), $e->getMessage() ) );
    return new WP_Error( 'Retrieve QPilot Payment Methods Failed', __( $notice['desc'], "autoship" ) );
	}

  return true;

}