2011-08-09 15:16:18 +00:00
< ? php
/**
2011-08-10 17:11:11 +00:00
* PayPal Standard Payment Gateway
*
* Provides a PayPal Standard Payment Gateway .
*
2011-11-04 10:35:02 +00:00
* @ class woocommerce_paypal
2011-08-10 17:11:11 +00:00
* @ package WooCommerce
* @ category Payment Gateways
* @ author WooThemes
*/
class woocommerce_paypal extends woocommerce_payment_gateway {
2011-08-09 15:16:18 +00:00
public function __construct () {
2011-09-06 11:11:22 +00:00
global $woocommerce ;
2011-08-09 15:16:18 +00:00
$this -> id = 'paypal' ;
2011-11-03 09:34:53 +00:00
$this -> icon = apply_filters ( 'woocommerce_paypal_icon' , $woocommerce -> plugin_url () . '/assets/images/icons/paypal.png' );
2011-08-09 15:16:18 +00:00
$this -> has_fields = false ;
2011-09-22 14:58:20 +00:00
$this -> liveurl = 'https://www.paypal.com/webscr' ;
2011-08-09 15:16:18 +00:00
$this -> testurl = 'https://www.sandbox.paypal.com/webscr' ;
2011-09-22 14:58:20 +00:00
// Load the form fields.
$this -> init_form_fields ();
// Load the settings.
$this -> init_settings ();
2011-08-09 15:16:18 +00:00
2011-09-22 14:58:20 +00:00
// Define user set variables
$this -> title = $this -> settings [ 'title' ];
$this -> description = $this -> settings [ 'description' ];
$this -> email = $this -> settings [ 'email' ];
$this -> testmode = $this -> settings [ 'testmode' ];
2011-11-28 15:50:19 +00:00
$this -> send_shipping = $this -> settings [ 'send_shipping' ];
2011-11-15 13:08:56 +00:00
$this -> debug = $this -> settings [ 'debug' ];
2011-08-09 15:16:18 +00:00
2011-12-08 12:50:50 +00:00
// Logs
if ( $this -> debug == 'yes' ) $this -> log = $woocommerce -> logger ();
2011-09-22 14:58:20 +00:00
// Actions
2011-08-09 15:16:18 +00:00
add_action ( 'init' , array ( & $this , 'check_ipn_response' ) );
add_action ( 'valid-paypal-standard-ipn-request' , array ( & $this , 'successful_request' ) );
2011-09-21 15:19:55 +00:00
add_action ( 'woocommerce_receipt_paypal' , array ( & $this , 'receipt_page' ));
2011-09-22 14:58:20 +00:00
add_action ( 'woocommerce_update_options_payment_gateways' , array ( & $this , 'process_admin_options' ));
2011-11-24 15:22:28 +00:00
if ( ! $this -> is_valid_for_use () ) $this -> enabled = false ;
2011-08-09 15:16:18 +00:00
}
2011-11-24 15:22:28 +00:00
/**
* Check if this gateway is enabled and available in the user ' s country
*/
function is_valid_for_use () {
if ( ! in_array ( get_option ( 'woocommerce_currency' ), array ( 'AUD' , 'BRL' , 'CAD' , 'MXN' , 'NZD' , 'HKD' , 'SGD' , 'USD' , 'EUR' , 'JPY' , 'TRY' , 'NOK' , 'CZK' , 'DKK' , 'HUF' , 'ILS' , 'MYR' , 'PHP' , 'PLN' , 'SEK' , 'CHF' , 'TWD' , 'THB' , 'GBP' ))) return false ;
return true ;
}
/**
* Admin Panel Options
* - Options for bits like 'title' and availability on a country - by - country basis
*
* @ since 1.0 . 0
*/
public function admin_options () {
?>
2012-01-05 11:31:22 +00:00
< h3 >< ? php _e ( 'PayPal standard' , 'woocommerce' ); ?> </h3>
< p >< ? php _e ( 'PayPal standard works by sending the user to PayPal to enter their payment information.' , 'woocommerce' ); ?> </p>
2011-11-24 15:22:28 +00:00
< table class = " form-table " >
< ? php
if ( $this -> is_valid_for_use () ) :
// Generate the HTML For the settings form.
$this -> generate_settings_html ();
else :
?>
2012-01-05 11:31:22 +00:00
< div class = " inline error " >< p >< strong >< ? php _e ( 'Gateway Disabled' , 'woocommerce' ); ?> </strong>: <?php _e( 'PayPal does not support your store currency.', 'woocommerce' ); ?></p></div>
2011-11-24 15:22:28 +00:00
< ? php
endif ;
?>
</ table ><!--/. form - table -->
< ? php
} // End admin_options()
2011-08-09 15:16:18 +00:00
/**
2011-09-22 14:58:20 +00:00
* Initialise Gateway Settings Form Fields
*/
function init_form_fields () {
$this -> form_fields = array (
'enabled' => array (
2012-01-05 11:31:22 +00:00
'title' => __ ( 'Enable/Disable' , 'woocommerce' ),
2011-09-22 14:58:20 +00:00
'type' => 'checkbox' ,
2012-01-05 11:31:22 +00:00
'label' => __ ( 'Enable PayPal standard' , 'woocommerce' ),
2011-09-22 14:58:20 +00:00
'default' => 'yes'
),
'title' => array (
2012-01-05 11:31:22 +00:00
'title' => __ ( 'Title' , 'woocommerce' ),
2011-09-22 14:58:20 +00:00
'type' => 'text' ,
2012-01-05 11:31:22 +00:00
'description' => __ ( 'This controls the title which the user sees during checkout.' , 'woocommerce' ),
'default' => __ ( 'PayPal' , 'woocommerce' )
2011-09-22 14:58:20 +00:00
),
'description' => array (
2012-01-05 11:31:22 +00:00
'title' => __ ( 'Description' , 'woocommerce' ),
2011-09-22 14:58:20 +00:00
'type' => 'textarea' ,
2012-01-05 11:31:22 +00:00
'description' => __ ( 'This controls the description which the user sees during checkout.' , 'woocommerce' ),
'default' => __ ( " Pay via PayPal; you can pay with your credit card if you don't have a PayPal account " , 'woocommerce' )
2011-09-22 14:58:20 +00:00
),
'email' => array (
2012-01-05 11:31:22 +00:00
'title' => __ ( 'PayPal Email' , 'woocommerce' ),
2011-09-22 14:58:20 +00:00
'type' => 'text' ,
2012-01-05 11:31:22 +00:00
'description' => __ ( 'Please enter your PayPal email address; this is needed in order to take payment.' , 'woocommerce' ),
2011-09-22 14:58:20 +00:00
'default' => ''
),
'send_shipping' => array (
2012-01-05 11:31:22 +00:00
'title' => __ ( 'Shipping details' , 'woocommerce' ),
2011-09-22 14:58:20 +00:00
'type' => 'checkbox' ,
2012-01-05 11:31:22 +00:00
'label' => __ ( 'Send shipping details to PayPal. Since PayPal verifies addresses sent to it this can cause errors, therefore we recommend disabling this option.' , 'woocommerce' ),
2011-09-22 14:58:20 +00:00
'default' => 'no'
),
'testmode' => array (
2012-01-05 11:31:22 +00:00
'title' => __ ( 'PayPal sandbox' , 'woocommerce' ),
2011-09-22 14:58:20 +00:00
'type' => 'checkbox' ,
2012-01-05 11:31:22 +00:00
'label' => __ ( 'Enable PayPal sandbox' , 'woocommerce' ),
2011-09-22 14:58:20 +00:00
'default' => 'yes'
2011-11-15 13:08:56 +00:00
),
'debug' => array (
2012-01-05 11:31:22 +00:00
'title' => __ ( 'Debug' , 'woocommerce' ),
2011-11-15 13:08:56 +00:00
'type' => 'checkbox' ,
2012-01-05 11:31:22 +00:00
'label' => __ ( 'Enable logging (<code>woocommerce/logs/paypal.txt</code>)' , 'woocommerce' ),
2011-11-15 13:08:56 +00:00
'default' => 'no'
2011-09-22 14:58:20 +00:00
)
);
} // End init_form_fields()
2011-08-09 15:16:18 +00:00
/**
* There are no payment fields for paypal , but we want to show the description if set .
**/
function payment_fields () {
2011-09-22 14:58:20 +00:00
if ( $this -> description ) echo wpautop ( wptexturize ( $this -> description ));
2011-08-09 15:16:18 +00:00
}
/**
* Generate the paypal button link
**/
public function generate_paypal_form ( $order_id ) {
2011-09-06 11:11:22 +00:00
global $woocommerce ;
2011-08-09 15:16:18 +00:00
2011-12-30 19:36:44 +00:00
$order = new woocommerce_order ( $order_id );
2011-08-09 15:16:18 +00:00
if ( $this -> testmode == 'yes' ) :
$paypal_adr = $this -> testurl . '?test_ipn=1&' ;
else :
$paypal_adr = $this -> liveurl . '?' ;
endif ;
2011-12-08 12:50:50 +00:00
if ( $this -> debug == 'yes' ) $this -> log -> add ( 'paypal' , 'Generating payment form for order #' . $order_id . '. Notify URL: ' . trailingslashit ( home_url ()) . '?paypalListener=paypal_standard_IPN' );
2011-11-18 12:32:59 +00:00
2011-08-09 15:16:18 +00:00
if ( in_array ( $order -> billing_country , array ( 'US' , 'CA' ))) :
2011-09-28 08:35:51 +00:00
$order -> billing_phone = str_replace ( array ( '(' , '-' , ' ' , ')' ), '' , $order -> billing_phone );
2011-08-09 15:16:18 +00:00
$phone_args = array (
'night_phone_a' => substr ( $order -> billing_phone , 0 , 3 ),
2011-09-28 08:35:51 +00:00
'night_phone_b' => substr ( $order -> billing_phone , 3 , 3 ),
'night_phone_c' => substr ( $order -> billing_phone , 6 , 4 ),
2011-08-09 15:16:18 +00:00
'day_phone_a' => substr ( $order -> billing_phone , 0 , 3 ),
2011-09-28 08:35:51 +00:00
'day_phone_b' => substr ( $order -> billing_phone , 3 , 3 ),
'day_phone_c' => substr ( $order -> billing_phone , 6 , 4 )
2011-08-09 15:16:18 +00:00
);
else :
$phone_args = array (
'night_phone_b' => $order -> billing_phone ,
'day_phone_b' => $order -> billing_phone
);
endif ;
$paypal_args = array_merge (
array (
'cmd' => '_cart' ,
'business' => $this -> email ,
'no_note' => 1 ,
2011-08-10 17:11:11 +00:00
'currency_code' => get_option ( 'woocommerce_currency' ),
2011-08-09 15:16:18 +00:00
'charset' => 'UTF-8' ,
'rm' => 2 ,
'upload' => 1 ,
2011-10-23 11:25:22 +00:00
'return' => $this -> get_return_url ( $order ),
2011-08-09 15:16:18 +00:00
'cancel_return' => $order -> get_cancel_order_url (),
// Order key
'custom' => $order_id ,
// IPN
2011-11-18 12:36:02 +00:00
'notify_url' => trailingslashit ( home_url ()) . '?paypalListener=paypal_standard_IPN' ,
2011-08-09 15:16:18 +00:00
// Address info
'first_name' => $order -> billing_first_name ,
'last_name' => $order -> billing_last_name ,
'company' => $order -> billing_company ,
'address1' => $order -> billing_address_1 ,
'address2' => $order -> billing_address_2 ,
'city' => $order -> billing_city ,
'state' => $order -> billing_state ,
'zip' => $order -> billing_postcode ,
'country' => $order -> billing_country ,
'email' => $order -> billing_email ,
// Payment Info
'invoice' => $order -> order_key ,
2011-11-28 17:03:53 +00:00
'discount_amount_cart' => $order -> get_order_discount ()
2011-08-09 15:16:18 +00:00
),
$phone_args
);
if ( $this -> send_shipping == 'yes' ) :
$paypal_args [ 'no_shipping' ] = 0 ;
2011-11-28 17:03:53 +00:00
$paypal_args [ 'address_override' ] = 1 ;
2011-08-09 15:16:18 +00:00
else :
$paypal_args [ 'no_shipping' ] = 1 ;
endif ;
2011-11-21 16:26:06 +00:00
if ( get_option ( 'woocommerce_prices_include_tax' ) == 'yes' ) :
2011-11-30 20:43:13 +00:00
// Tax
2011-11-21 16:26:06 +00:00
$paypal_args [ 'tax_cart' ] = $order -> get_total_tax ();
2011-11-30 20:43:13 +00:00
// Don't pass items - paypal borks tax due to prices including tax. PayPal has no option for tax inclusive pricing sadly. Pass 1 item for the order items overall
2012-01-05 11:31:22 +00:00
$paypal_args [ 'item_name_1' ] = sprintf ( __ ( 'Order #%s' , 'woocommerce' ), $order -> id );
2011-11-30 20:43:13 +00:00
$paypal_args [ 'quantity_1' ] = 1 ;
2012-01-04 23:01:47 +00:00
$paypal_args [ 'amount_1' ] = number_format ( $order -> get_order_total () - $order -> get_shipping () - $order -> get_total_tax () + $order -> get_order_discount (), 2 , '.' , '' );
2011-11-30 20:43:13 +00:00
// Shipping Cost
2012-01-04 23:01:47 +00:00
if ( $order -> get_shipping () > 0 ) :
2012-01-05 11:31:22 +00:00
$paypal_args [ 'item_name_2' ] = __ ( 'Shipping cost' , 'woocommerce' );
2011-11-30 20:43:13 +00:00
$paypal_args [ 'quantity_2' ] = '1' ;
2012-01-04 23:01:47 +00:00
$paypal_args [ 'amount_2' ] = number_format ( $order -> get_shipping (), 2 , '.' , '' );
2011-11-30 20:43:13 +00:00
endif ;
2011-11-21 11:33:46 +00:00
2011-11-30 20:43:13 +00:00
else :
// Tax
$paypal_args [ 'tax_cart' ] = $order -> get_total_tax ();
// Cart Contents
$item_loop = 0 ;
2012-01-13 00:46:56 +00:00
if ( sizeof ( $order -> get_items ()) > 0 ) : foreach ( $order -> get_items () as $item ) :
2011-11-30 20:43:13 +00:00
if ( $item [ 'qty' ]) :
2011-11-21 16:26:06 +00:00
2011-11-30 20:43:13 +00:00
$item_loop ++ ;
2011-11-21 16:26:06 +00:00
2011-11-30 20:43:13 +00:00
$item_name = $item [ 'name' ];
2011-11-21 11:33:46 +00:00
2011-12-30 19:36:44 +00:00
$item_meta = new order_item_meta ( $item [ 'item_meta' ] );
2011-11-30 20:43:13 +00:00
if ( $meta = $item_meta -> display ( true , true )) :
$item_name .= ' (' . $meta . ')' ;
endif ;
2011-11-21 11:33:46 +00:00
$paypal_args [ 'item_name_' . $item_loop ] = $item_name ;
$paypal_args [ 'quantity_' . $item_loop ] = $item [ 'qty' ];
2012-01-04 23:01:47 +00:00
$paypal_args [ 'amount_' . $item_loop ] = $order -> get_item_cost ( $item , false );
2011-11-21 11:33:46 +00:00
endif ;
2011-11-30 20:43:13 +00:00
endforeach ; endif ;
// Shipping Cost
2012-01-04 23:01:47 +00:00
if ( $order -> get_shipping () > 0 ) :
2011-11-30 20:43:13 +00:00
$item_loop ++ ;
2012-01-05 11:31:22 +00:00
$paypal_args [ 'item_name_' . $item_loop ] = __ ( 'Shipping cost' , 'woocommerce' );
2011-11-30 20:43:13 +00:00
$paypal_args [ 'quantity_' . $item_loop ] = '1' ;
2012-01-04 23:01:47 +00:00
$paypal_args [ 'amount_' . $item_loop ] = number_format ( $order -> get_shipping (), 2 , '.' , '' );
2011-08-09 15:16:18 +00:00
endif ;
2011-11-21 11:33:46 +00:00
endif ;
2011-08-09 15:16:18 +00:00
$paypal_args_array = array ();
foreach ( $paypal_args as $key => $value ) {
2011-09-30 18:22:17 +00:00
$paypal_args_array [] = '<input type="hidden" name="' . esc_attr ( $key ) . '" value="' . esc_attr ( $value ) . '" />' ;
2011-08-09 15:16:18 +00:00
}
2011-12-07 17:36:58 +00:00
$woocommerce -> add_inline_js ( '
jQuery ( " body " ) . block ({
2012-01-05 11:31:22 +00:00
message : " <img src= \" '.esc_url( $woocommerce->plugin_url () ).'/assets/images/ajax-loader.gif \" alt= \" Redirecting... \" style= \" float:left; margin-right: 10px; \" />'.__('Thank you for your order. We are now redirecting you to PayPal to make payment.', 'woocommerce').' " ,
2011-12-07 17:36:58 +00:00
overlayCSS :
{
background : " #fff " ,
opacity : 0.6
},
css : {
padding : 20 ,
textAlign : " center " ,
color : " #555 " ,
border : " 3px solid #aaa " ,
backgroundColor : " #fff " ,
cursor : " wait " ,
lineHeight : " 32px "
}
});
jQuery ( " #submit_paypal_payment_form " ) . click ();
' );
return '<form action="' . esc_url ( $paypal_adr ) . ' " method= " post " id= " paypal_payment_form " >
2011-08-09 15:16:18 +00:00
' . implode(' ', $paypal_args_array) . '
2012-01-05 11:31:22 +00:00
< input type = " submit " class = " button-alt " id = " submit_paypal_payment_form " value = " '.__('Pay via PayPal', 'woocommerce').' " /> < a class = " button cancel " href = " '.esc_url( $order->get_cancel_order_url () ).' " > '.__(' Cancel order & amp ; restore cart ', ' woocommerce ').' </ a >
2011-08-09 15:16:18 +00:00
</ form > ' ;
}
/**
* Process the payment and return the result
**/
function process_payment ( $order_id ) {
2011-12-30 19:36:44 +00:00
$order = new woocommerce_order ( $order_id );
2011-08-09 15:16:18 +00:00
return array (
'result' => 'success' ,
2012-01-06 17:14:31 +00:00
'redirect' => add_query_arg ( 'order' , $order -> id , add_query_arg ( 'key' , $order -> order_key , get_permalink ( woocommerce_get_page_id ( 'pay' ))))
2011-08-09 15:16:18 +00:00
);
}
/**
* receipt_page
**/
function receipt_page ( $order ) {
2012-01-05 11:31:22 +00:00
echo '<p>' . __ ( 'Thank you for your order, please click the button below to pay with PayPal.' , 'woocommerce' ) . '</p>' ;
2011-08-09 15:16:18 +00:00
echo $this -> generate_paypal_form ( $order );
}
/**
* Check PayPal IPN validity
**/
function check_ipn_request_is_valid () {
2011-11-15 13:08:56 +00:00
global $woocommerce ;
2011-12-08 12:50:50 +00:00
if ( $this -> debug == 'yes' ) $this -> log -> add ( 'paypal' , 'Checking IPN response is valid...' );
2012-01-07 17:58:06 +00:00
// Get recieved values from post data
$received_values = ( array ) stripslashes_deep ( $_POST );
// Add cmd to the post array
$received_values [ 'cmd' ] = '_notify-validate' ;
2011-08-09 15:16:18 +00:00
// Send back post vars to paypal
2011-11-04 17:48:04 +00:00
$params = array (
2012-01-07 17:58:06 +00:00
'body' => $received_values ,
'sslverify' => false ,
'timeout' => 30 ,
'user-agent' => 'WooCommerce/' . $woocommerce -> version
2011-11-04 17:48:04 +00:00
);
2011-08-09 15:16:18 +00:00
// 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 );
// 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 )) {
2011-12-08 12:50:50 +00:00
if ( $this -> debug == 'yes' ) $this -> log -> add ( 'paypal' , 'Received valid response from PayPal' );
2011-08-09 15:16:18 +00:00
return true ;
}
2011-11-15 13:08:56 +00:00
if ( $this -> debug == 'yes' ) :
2011-12-08 12:50:50 +00:00
$this -> log -> add ( 'paypal' , 'Received invalid response from PayPal' );
2011-11-15 13:08:56 +00:00
if ( is_wp_error ( $response )) :
2011-12-08 12:50:50 +00:00
$this -> log -> add ( 'paypal' , 'Error response: ' . $result -> get_error_message () );
2011-11-15 13:08:56 +00:00
endif ;
endif ;
2011-08-09 15:16:18 +00:00
return false ;
}
/**
* Check for PayPal IPN Response
**/
function check_ipn_response () {
if ( isset ( $_GET [ 'paypalListener' ]) && $_GET [ 'paypalListener' ] == 'paypal_standard_IPN' ) :
2012-01-07 17:58:06 +00:00
2012-01-12 13:34:35 +00:00
@ ob_clean ();
2012-01-07 17:58:06 +00:00
2011-08-09 15:16:18 +00:00
$_POST = stripslashes_deep ( $_POST );
2011-09-06 11:11:22 +00:00
if ( $this -> check_ipn_request_is_valid ()) :
2012-01-07 17:58:06 +00:00
header ( 'HTTP/1.1 200 OK' );
2011-08-09 15:16:18 +00:00
do_action ( " valid-paypal-standard-ipn-request " , $_POST );
2012-01-07 17:58:06 +00:00
else :
wp_die ( " PayPal IPN Request Failure " );
2011-08-09 15:16:18 +00:00
endif ;
endif ;
}
/**
* Successful Payment !
**/
function successful_request ( $posted ) {
// Custom holds post ID
2011-11-26 17:29:31 +00:00
if ( ! empty ( $posted [ 'custom' ]) && ! empty ( $posted [ 'invoice' ]) ) {
2011-08-09 15:16:18 +00:00
2011-08-10 17:11:11 +00:00
$order = new woocommerce_order ( ( int ) $posted [ 'custom' ] );
2012-01-10 14:55:08 +00:00
if ( $order -> order_key !== $posted [ 'invoice' ]) :
if ( $this -> debug == 'yes' ) $this -> log -> add ( 'paypal' , 'Error: Order Key does not match invoice.' );
exit ;
endif ;
2011-08-09 15:16:18 +00:00
2012-01-09 10:06:57 +00:00
// Lowercase
$posted [ 'payment_status' ] = strtolower ( $posted [ 'payment_status' ]);
$posted [ 'txn_type' ] = strtolower ( $posted [ 'txn_type' ]);
2011-08-09 15:16:18 +00:00
// Sandbox fix
2012-01-09 10:06:57 +00:00
if ( $posted [ 'test_ipn' ] == 1 && $posted [ 'payment_status' ] == 'pending' ) $posted [ 'payment_status' ] = 'completed' ;
2011-11-26 17:29:31 +00:00
2012-01-10 14:55:08 +00:00
if ( $this -> debug == 'yes' ) $this -> log -> add ( 'paypal' , 'Payment status: ' . $posted [ 'payment_status' ] );
2011-11-26 17:29:31 +00:00
// We are here so lets check status and do actions
2012-01-09 10:06:57 +00:00
switch ( $posted [ 'payment_status' ]) :
2011-11-26 17:29:31 +00:00
case 'completed' :
// Check order not already completed
2012-01-10 14:55:08 +00:00
if ( $order -> status == 'completed' ) :
if ( $this -> debug == 'yes' ) $this -> log -> add ( 'paypal' , 'Aborting, Order #' . $posted [ 'custom' ] . ' is already complete.' );
exit ;
endif ;
2011-11-26 17:29:31 +00:00
// Check valid txn_type
$accepted_types = array ( 'cart' , 'instant' , 'express_checkout' , 'web_accept' , 'masspay' , 'send_money' );
2012-01-10 14:55:08 +00:00
if ( ! in_array ( $posted [ 'txn_type' ], $accepted_types )) :
if ( $this -> debug == 'yes' ) $this -> log -> add ( 'paypal' , 'Aborting, Invalid type:' . $posted [ 'txn_type' ] );
endif ;
2011-11-26 17:29:31 +00:00
// Payment completed
2012-01-05 11:31:22 +00:00
$order -> add_order_note ( __ ( 'IPN payment completed' , 'woocommerce' ) );
2011-11-26 17:29:31 +00:00
$order -> payment_complete ();
2012-01-10 14:55:08 +00:00
if ( $this -> debug == 'yes' ) $this -> log -> add ( 'paypal' , 'Payment complete.' );
2011-11-26 17:29:31 +00:00
// Store PP Details
update_post_meta ( ( int ) $posted [ 'custom' ], 'Payer PayPal address' , $posted [ 'payer_email' ]);
update_post_meta ( ( int ) $posted [ 'custom' ], 'Transaction ID' , $posted [ 'txn_id' ]);
update_post_meta ( ( int ) $posted [ 'custom' ], 'Payer first name' , $posted [ 'first_name' ]);
update_post_meta ( ( int ) $posted [ 'custom' ], 'Payer last name' , $posted [ 'last_name' ]);
update_post_meta ( ( int ) $posted [ 'custom' ], 'Payment type' , $posted [ 'payment_type' ]);
break ;
case 'denied' :
case 'expired' :
case 'failed' :
case 'voided' :
// Order failed
2012-01-05 11:31:22 +00:00
$order -> update_status ( 'failed' , sprintf ( __ ( 'Payment %s via IPN.' , 'woocommerce' ), strtolower ( $posted [ 'payment_status' ]) ) );
2011-11-26 17:29:31 +00:00
break ;
case " refunded " :
case " reversed " :
2011-11-28 12:07:56 +00:00
case " chargeback " :
2011-11-26 17:29:31 +00:00
// Mark order as refunded
2012-01-05 11:31:22 +00:00
$order -> update_status ( 'refunded' , sprintf ( __ ( 'Payment %s via IPN.' , 'woocommerce' ), strtolower ( $posted [ 'payment_status' ]) ) );
2011-11-28 12:07:56 +00:00
$message = woocommerce_mail_template (
2012-01-05 11:31:22 +00:00
__ ( 'Order refunded/reversed' , 'woocommerce' ),
sprintf ( __ ( 'Order #%s has been marked as refunded - PayPal reason code: %s' , 'woocommerce' ), $order -> id , $posted [ 'reason_code' ] )
2011-11-28 12:07:56 +00:00
);
// Send the mail
2012-01-05 11:31:22 +00:00
woocommerce_mail ( get_option ( 'woocommerce_new_order_email_recipient' ), sprintf ( __ ( 'Payment for order #%s refunded/reversed' , 'woocommerce' ), $order -> id ), $message );
2011-11-28 12:07:56 +00:00
2011-11-26 17:29:31 +00:00
break ;
default :
// No action
break ;
endswitch ;
2011-08-09 15:16:18 +00:00
exit ;
}
}
}
/**
2011-08-10 17:11:11 +00:00
* Add the gateway to WooCommerce
2011-08-09 15:16:18 +00:00
**/
function add_paypal_gateway ( $methods ) {
2011-08-10 17:11:11 +00:00
$methods [] = 'woocommerce_paypal' ; return $methods ;
2011-08-09 15:16:18 +00:00
}
2011-08-10 17:11:11 +00:00
add_filter ( 'woocommerce_payment_gateways' , 'add_paypal_gateway' );