/** * Returns and/or create the single instance of this class. * * @return Charitable_Donation_Processor * @access public * @since 1.0.0 */ public static function get_instance() { if (is_null(self::$instance)) { self::$instance = new Charitable_Donation_Processor(); } return self::$instance; }
/** * Test retrieving the campaign donation data. */ public function test_get_campaign_donation_data() { $processor = Charitable_Donation_Processor::get_instance(); $donation_id = $processor->save_donation(array('user_id' => 1, 'campaigns' => array(array('campaign_id' => $this->campaign_1->ID, 'campaign_name' => get_the_title($this->campaign_1), 'amount' => 30), array('campaign_id' => $this->campaign_2->ID, 'campaign_name' => get_the_title($this->campaign_2), 'amount' => 40)), 'status' => 'charitable-completed', 'gateway' => 'manual', 'note' => '')); $this->assertCount(2, $processor->get_campaign_donations_data()); Charitable_Donation_Processor::destroy(); }
/** * Create a donation. * * @param array $args Optional arguments. * @return int $donation_id * @access public * @static * @since 1.0.0 */ public static function create_donation($args = array()) { $defaults = array('user_id' => 1, 'campaigns' => array(), 'status' => 'charitable-completed', 'gateway' => 'manual', 'note' => ''); $args = array_merge($defaults, $args); if (empty($args['campaigns']) || !is_array($args['campaigns'])) { wp_die('You must pass an array of campaigns to create a donation.'); } return Charitable_Donation_Processor::get_instance()->save_donation($args); }
/** * Process the donation with the gateway. * * @param mixed $return * @param int $donation_id * @param Charitable_Donation_Processor $processor * @return boolean|array * @access public * @static * @since 1.0.0 */ public static function process_donation($return, $donation_id, $processor) { $gateway = new Charitable_Gateway_Extension_Boilerplate(); $donation = charitable_get_donation($donation_id); $donor = $donation->get_donor(); $values = $processor->get_donation_data(); // API keys // $keys = $gateway->get_keys(); // Donation fields // $donation_key = $donation->get_donation_key(); // $item_name = sprintf( __( 'Donation %d', 'charitable-payu-money' ), $donation->ID );; // $description = $donation->get_campaigns_donated_to(); // $amount = $donation->get_total_donation_amount( true ); // Donor fields // $first_name = $donor->get_donor_meta( 'first_name' ); // $last_name = $donor->get_donor_meta( 'last_name' ); // $address = $donor->get_donor_meta( 'address' ); // $address_2 = $donor->get_donor_meta( 'address_2' ); // $email = $donor->get_donor_meta( 'email' ); // $city = $donor->get_donor_meta( 'city' ); // $state = $donor->get_donor_meta( 'state' ); // $country = $donor->get_donor_meta( 'country' ); // $postcode = $donor->get_donor_meta( 'postcode' ); // $phone = $donor->get_donor_meta( 'phone' ); // URL fields // $return_url = charitable_get_permalink( 'donation_receipt_page', array( 'donation_id' => $donation->ID ) ); // $cancel_url = charitable_get_permalink( 'donation_cancel_page', array( 'donation_id' => $donation->ID ) ); // $notify_url = function_exists( 'charitable_get_ipn_url' ) // ? charitable_get_ipn_url( Charitable_Gateway_Sparrow::ID ) // : Charitable_Donation_Processor::get_instance()->get_ipn_url( Charitable_Gateway_Sparrow::ID ); // Credit card fields // $cc_expiration = $this->get_gateway_value( 'cc_expiration', $values ); // $cc_number = $this->get_gateway_value( 'cc_number', $values ); // $cc_year = $cc_expiration['year']; // $cc_month = $cc_expiration['month']; // $cc_cvc = $this->get_gateway_value( 'cc_cvc', $values ); /** * @todo Create donation charge through gateway. * * You should return one of three values. * * 1. If the donation fails to be processed and the user should be * returned to the donation page, return false. * 2. If the donation succeeds and the user should be directed to * the donation receipt, return true. * 3. If the user should be redirected elsewhere (for example, * a gateway-hosted payment page), you should return an array * like this: array( 'redirect' => $redirect_url, 'safe' => false // Set to false if you are redirecting away from the site. ); * */ return true; }
/** * Process the donation with PayPal. * * @param int $donation_id * @param Charitable_Donation_Processor $processor * @return void * @access public * @static * @since 1.0.0 */ public static function process_donation($donation_id, $processor) { $gateway = new Charitable_Gateway_Paypal(); $user_data = $processor->get_donation_data_value('user'); $donation = new Charitable_Donation($donation_id); $transaction_mode = $gateway->get_value('transaction_mode'); $paypal_args = apply_filters('charitable_paypal_redirect_args', array('business' => $gateway->get_value('paypal_email'), 'email' => $user_data['email'], 'first_name' => $user_data['first_name'], 'last_name' => $user_data['last_name'], 'address1' => $user_data['address'], 'address2' => $user_data['address_2'], 'city' => $user_data['city'], 'country' => $user_data['country'], 'zip' => $user_data['postcode'], 'invoice' => $processor->get_donation_data_value('donation_key'), 'amount' => $donation->get_total_donation_amount(), 'item_name' => html_entity_decode($donation->get_campaigns_donated_to(), ENT_COMPAT, 'UTF-8'), 'no_shipping' => '1', 'shipping' => '0', 'no_note' => '1', 'currency_code' => charitable_get_currency(), 'charset' => get_bloginfo('charset'), 'custom' => $donation_id, 'rm' => '2', 'return' => charitable_get_permalink('donation_receipt_page', array('donation_id' => $donation_id)), 'cancel_return' => home_url(), 'notify_url' => $processor->get_ipn_url(self::ID), 'cbt' => get_bloginfo('name'), 'bn' => 'Charitable_SP', 'cmd' => $transaction_mode == 'donations' ? '_donations' : '_xclick'), $donation_id, $processor); /** * Set up the PayPal redirect URL */ $paypal_redirect = trailingslashit($gateway->get_redirect_url()) . '?'; $paypal_redirect .= http_build_query($paypal_args); $paypal_redirect = str_replace('&', '&', $paypal_redirect); /** * Redirect to PayPal */ wp_redirect($paypal_redirect); exit; }
/** * Returns the Charitable_Donation_Processor class instance. * * @return Charitable_Donation_Processor * @since 1.0.0 */ function charitable_get_donation_processor() { return Charitable_Donation_Processor::get_instance(); }
/** * Add donation_receipt_optout to the list of meta fields to be saved. * * @param mixed[] $meta * @param int $donation_id * @param Charitable_Donation_Processor $processor * @return mixed[] */ function ed_save_donation_receipt_opt_out_meta_field($meta, $donation_id, Charitable_Donation_Processor $processor) { $meta['donation_receipt_optout'] = $processor->get_donation_data_value('donation_receipt_optout'); return $meta; }
/** * Create a donation. * * @param array $args Values for the donation. * @return int * @since 1.4.0 */ function charitable_create_donation(array $args) { $donation_id = Charitable_Donation_Processor::get_instance()->save_donation($args); Charitable_Donation_Processor::destroy(); return $donation_id; }
/** * Destroy the Charitable_Donation_Processor instance. * * This is primarily useful for testing purposes, as it allows you to * create multiple donations in a single request. * * @return void * @access public * @static * @since 1.3.0 */ public static function destroy() { self::$instance = null; }