autoship_add_general_payment_method (function)

(src/payments.php) can be used to add any payment method to QPilot. It takes the (WooCommerce) $token object and $return (Valid QPilot Payment Method or null).

Code:

/**
 * Adds a payment method from QPilot for a customer
 *
 * @param WC_Payment_Token_CC $token
 * @param string $type The Type for the Payment Method from QPilot.
 * @param bool $return Should the payment method data be returned or added.
 *                     True to return, false to add to QPilot
 * @return mixed The Payment Method object or error on failure if $return is set to false.
 *               Array of payment data if set to true.
 */
function autoship_add_general_payment_method( $token , $return = false ){

  // Get the User associated with the token.
	$wc_customer_id       = $token->get_user_id();

  // Get the Autoship customer.
	$autoship_customer_id = autoship_get_autoship_customer_id( $wc_customer_id , 'autoship_add_general_payment' );

  // Don't process if not a Autoship
	if ( empty( $autoship_customer_id ) )
	return;

 	// Create the payment method and gather the info.
	$payment_method_id        = $token->get_token();
	$description              = apply_filters(
  'autoship_add_general_payment_method_description',
  $token->get_display_name());
  $type                     = autoship_get_valid_payment_method_type( $token->get_gateway_id() );
  $wc_customer              = new WC_Customer( $wc_customer_id );
  $payment_method_data      = array(
		'CustomerId'        => $autoship_customer_id,
		'Type'              => $type,
		'GatewayCustomerId' => $wc_customer_id,
  	'GatewayPaymentId'  => $payment_method_id,
		'Description'       => $description,
		'BillingFirstName'  => substr( $wc_customer->get_billing_first_name(), 0, 20 ),
		'BillingLastName'   => substr( $wc_customer->get_billing_last_name(), 0, 20 ),
		'BillingStreet1'    => $wc_customer->get_billing_address_1(),
		'BillingStreet2'    => $wc_customer->get_billing_address_2(),
		'BillingCity'       => substr( $wc_customer->get_billing_city(), 0, 30 ),
		'BillingState'      => substr( $wc_customer->get_billing_state(), 0, 20 ),
		'BillingPostcode'   => substr( $wc_customer->get_billing_postcode(), 0, 20 ),
		'BillingCountry'    => substr( $wc_customer->get_billing_country(), 0, 2 )
	);

  // Apply the Payment method filter for customers to override.
  $payment_method_data = apply_filters( 'autoship_add_general_payment_method', $payment_method_data, $type, $token );
  $payment_method_data = apply_filters( "autoship_add_{$type}_payment_method", $payment_method_data, $type, $token );
  $payment_method_data = apply_filters( 'autoship_api_create_payment_method_data',$payment_method_data );

  // Add the Method
 	return $return ? $payment_method_data : autoship_add_payment_method( $payment_method_data );

}