id = 'paypal';
$this->icon = apply_filters('woocommerce_paypal_icon', $woocommerce->plugin_url() . '/assets/images/icons/paypal.png');
$this->has_fields = false;
$this->liveurl = 'https://www.paypal.com/webscr';
$this->testurl = 'https://www.sandbox.paypal.com/webscr';
// Load the form fields.
$this->init_form_fields();
// Load the settings.
$this->init_settings();
// Define user set variables
$this->title = $this->settings['title'];
$this->description = $this->settings['description'];
$this->email = $this->settings['email'];
$this->testmode = $this->settings['testmode'];
$this->send_shipping = $this->settings['send_shipping'];
$this->debug = $this->settings['debug'];
// Actions
add_action( 'init', array(&$this, 'check_ipn_response') );
add_action('valid-paypal-standard-ipn-request', array(&$this, 'successful_request') );
add_action('woocommerce_receipt_paypal', array(&$this, 'receipt_page'));
add_action('woocommerce_update_options_payment_gateways', array(&$this, 'process_admin_options'));
}
/**
* Initialise Gateway Settings Form Fields
*/
function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'woothemes' ),
'type' => 'checkbox',
'label' => __( 'Enable PayPal standard', 'woothemes' ),
'default' => 'yes'
),
'title' => array(
'title' => __( 'Title', 'woothemes' ),
'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.', 'woothemes' ),
'default' => __( 'PayPal', 'woothemes' )
),
'description' => array(
'title' => __( 'Description', 'woothemes' ),
'type' => 'textarea',
'description' => __( 'This controls the description which the user sees during checkout.', 'woothemes' ),
'default' => __("Pay via PayPal; you can pay with your credit card if you don't have a PayPal account", 'woothemes')
),
'email' => array(
'title' => __( 'PayPal Email', 'woothemes' ),
'type' => 'text',
'description' => __( 'Please enter your PayPal email address; this is needed in order to take payment.', 'woothemes' ),
'default' => ''
),
'send_shipping' => array(
'title' => __( 'Shipping details', 'woothemes' ),
'type' => 'checkbox',
'label' => __( 'Send shipping details to PayPal', 'woothemes' ),
'default' => 'no'
),
'testmode' => array(
'title' => __( 'PayPal sandbox', 'woothemes' ),
'type' => 'checkbox',
'label' => __( 'Enable PayPal sandbox', 'woothemes' ),
'default' => 'yes'
),
'debug' => array(
'title' => __( 'Debug', 'woothemes' ),
'type' => 'checkbox',
'label' => __( 'Enable logging (woocommerce/logs/paypal.txt
)', 'woothemes' ),
'default' => 'no'
)
);
} // End init_form_fields()
/**
* Admin Panel Options
* - Options for bits like 'title' and availability on a country-by-country basis
*
* @since 1.0.0
*/
public function admin_options() {
?>
'.__('Thank you for your order, please click the button below to pay with PayPal.', 'woothemes').'
'; echo $this->generate_paypal_form( $order ); } /** * Check PayPal IPN validity **/ function check_ipn_request_is_valid() { global $woocommerce; if ($this->debug=='yes') $woocommerce->log->add( 'paypal', 'Checking IPN response is valid...' ); // Add cmd to the post array $_POST['cmd'] = '_notify-validate'; // Send back post vars to paypal $params = array( 'body' => $_POST, 'sslverify' => false ); // Get url if ( $this->testmode == 'yes' ): $paypal_adr = $this->testurl; else : $paypal_adr = $this->liveurl; endif; // Post back to get a response $response = wp_remote_post( $paypal_adr, $params ); // Clean unset($_POST['cmd']); // check to see if the request was valid if ( !is_wp_error($response) && $response['response']['code'] >= 200 && $response['response']['code'] < 300 && (strcmp( $response['body'], "VERIFIED") == 0)) { if ($this->debug=='yes') $woocommerce->log->add( 'paypal', 'Received valid response from PayPal' ); return true; } if ($this->debug=='yes') : $woocommerce->log->add( 'paypal', 'Received invalid response from PayPal' ); if (is_wp_error($response)) : $woocommerce->log->add( 'paypal', 'Error response: ' . $result->get_error_message() ); endif; endif; return false; } /** * Check for PayPal IPN Response **/ function check_ipn_response() { if (isset($_GET['paypalListener']) && $_GET['paypalListener'] == 'paypal_standard_IPN'): $_POST = stripslashes_deep($_POST); if ($this->check_ipn_request_is_valid()) : do_action("valid-paypal-standard-ipn-request", $_POST); endif; endif; } /** * Successful Payment! **/ function successful_request( $posted ) { // Custom holds post ID if ( !empty($posted['txn_type']) && !empty($posted['invoice']) ) { $accepted_types = array('cart', 'instant', 'express_checkout', 'web_accept', 'masspay', 'send_money'); if (!in_array(strtolower($posted['txn_type']), $accepted_types)) exit; $order = new woocommerce_order( (int) $posted['custom'] ); if ($order->order_key!==$posted['invoice']) exit; // Sandbox fix if ($posted['test_ipn']==1 && $posted['payment_status']=='Pending') $posted['payment_status'] = 'completed'; if ($order->status !== 'completed') : // We are here so lets check status and do actions switch (strtolower($posted['payment_status'])) : case 'completed' : // Payment completed $order->add_order_note( __('IPN payment completed', 'woothemes') ); $order->payment_complete(); // Store paypal address update_post_meta( (int) $posted['custom'], 'Payer PayPal address', $posted['payer_email']); break; case 'denied' : case 'expired' : case 'failed' : case 'voided' : // Order failed $order->update_status('failed', sprintf(__('Payment %s via IPN.', 'woothemes'), strtolower($posted['payment_status']) ) ); break; default: // No action break; endswitch; endif; exit; } } } /** * Add the gateway to WooCommerce **/ function add_paypal_gateway( $methods ) { $methods[] = 'woocommerce_paypal'; return $methods; } add_filter('woocommerce_payment_gateways', 'add_paypal_gateway' );