Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Make Paypal default to the Credit Card screen instead of the Paypal login screen
<?php /*
--------------------------------------------------------------------------------
Plugin Name: CiviCRM Paypal Credit Card Screen
Plugin URI: http://haystack.co.uk
Description: Makes Paypal default to the Credit Card screen instead of the Paypal login screen
Author: Christian Wach
Version: 0.1
Author URI: http://haystack.co.uk
Depends: CiviCRM
--------------------------------------------------------------------------------
*/
/**
* CiviCRM Paypal Credit Card Screen Class.
*
* A class that encapsulates plugin functionality.
*
* @since 0.1
*/
class CiviCRM_Paypal_Credit_Card_Screen {
/**
* Constructor.
*
* @since 0.1
*/
public function __construct() {
// register hooks when CiviCRM has loaded
add_action( 'civicrm_instance_loaded', array( $this, 'register_civi_hooks' ) );
}
/**
* Register hooks on CiviCRM init.
*
* @since 0.1
*/
public function register_civi_hooks() {
// intercept CiviCRM's payment params
add_action( 'civicrm_alterPaymentProcessorParams', array( $this, 'paypal_params' ), 10, 4 );
}
/**
* Overwrite Paypal params.
*
* @since 0.1
*
* @param object $payment_obj The instance of payment class of the payment processor invoked
* @param array $raw_params The associative array passed by CiviCRM to the processor
* @param array $cooked_params The associative array of parameters as translated into the processor's API
* @return void
*/
public function paypal_params( $payment_obj, &$raw_params, &$cooked_params ) {
// only apply to Paypal
if ( ! ( $payment_obj instanceof CRM_Core_Payment_PayPalImpl ) ) return;
// get processor
$processor = $payment_obj->getPaymentProcessor();
// get processor type safely
$processor_type = isset( $processor['payment_processor_type'] ) ?
$processor['payment_processor_type'] :
'';
// bail if empty
if ( empty( $processor_type ) ) return;
// bail if not a Paypal processor
if ( ! in_array( $processor_type, array( 'PayPal_Standard', 'PayPal_Express', 'PayPal' ) ) ) return;
// clear the email address
$cooked_params['email'] = '';
// add landing page to params
$cooked_params['landingpage'] = 'creditcard';
}
} // class ends
// init plugin
global $civicrm_paypal_credit_card_screen;
$civicrm_paypal_credit_card_screen = new CiviCRM_Paypal_Credit_Card_Screen;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment