id = 'paypal';
$this->icon = jigoshop::assets_url() . '/assets/images/icons/paypal.png';
$this->has_fields = false;
$this->enabled = get_option('jigoshop_paypal_enabled');
$this->title = get_option('jigoshop_paypal_title');
$this->email = get_option('jigoshop_paypal_email');
$this->description = get_option('jigoshop_paypal_description');
$this->liveurl = 'https://www.paypal.com/webscr';
$this->testurl = 'https://www.sandbox.paypal.com/webscr';
$this->testmode = get_option('jigoshop_paypal_testmode');
$this->send_shipping = get_option('jigoshop_paypal_send_shipping');
add_action( 'init', array(&$this, 'check_ipn_response') );
add_action('valid-paypal-standard-ipn-request', array(&$this, 'successful_request') );
add_action('jigoshop_update_options', array(&$this, 'process_admin_options'));
add_option('jigoshop_paypal_enabled', 'yes');
add_option('jigoshop_paypal_email', '');
add_option('jigoshop_paypal_title', __('PayPal', 'jigoshop') );
add_option('jigoshop_paypal_description', __("Pay via PayPal; you can pay with your credit card if you don't have a PayPal account", 'jigoshop') );
add_option('jigoshop_paypal_testmode', 'no');
add_option('jigoshop_paypal_send_shipping', 'no');
add_action('receipt_paypal', array(&$this, 'receipt_page'));
}
/**
* Admin Panel Options
* - Options for bits like 'title' and availability on a country-by-country basis
**/
public function admin_options() {
?>
| PayPal to enter their payment information.', 'jigoshop'); ?> |
: |
|
: |
|
: |
|
: |
|
: |
|
: |
|
testmode == 'yes' ):
$paypal_adr = $this->testurl . '?test_ipn=1&';
else :
$paypal_adr = $this->liveurl . '?';
endif;
$shipping_name = explode(' ', $order->shipping_method);
if (in_array($order->billing_country, array('US','CA'))) :
$order->billing_phone = str_replace(array('(', '-', ' ', ')'), '', $order->billing_phone);
$phone_args = array(
'night_phone_a' => substr($order->billing_phone,0,3),
'night_phone_b' => substr($order->billing_phone,3,3),
'night_phone_c' => substr($order->billing_phone,6,4),
'day_phone_a' => substr($order->billing_phone,0,3),
'day_phone_b' => substr($order->billing_phone,3,3),
'day_phone_c' => substr($order->billing_phone,6,4)
);
else :
$phone_args = array(
'night_phone_b' => $order->billing_phone,
'day_phone_b' => $order->billing_phone
);
endif;
// filter redirect page
$checkout_redirect = apply_filters( 'jigoshop_get_checkout_redirect_page_id', jigoshop_get_page_id('thanks') );
$paypal_args = array_merge(
array(
'cmd' => '_cart',
'business' => $this->email,
'no_note' => 1,
'currency_code' => get_option('jigoshop_currency'),
'charset' => 'UTF-8',
'rm' => 2,
'upload' => 1,
'return' => add_query_arg('key', $order->order_key, add_query_arg('order', $order_id, get_permalink( $checkout_redirect ))),
'cancel_return' => $order->get_cancel_order_url(),
//'cancel_return' => home_url(),
// Order key
'custom' => $order_id,
// IPN
'notify_url' => trailingslashit(get_bloginfo('wpurl')).'?paypalListener=paypal_standard_IPN',
// 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,
'amount' => $order->order_total,
'discount_amount_cart' => $order->order_discount
),
$phone_args
);
// only include tax if prices don't include tax
if (get_option('jigoshop_prices_include_tax') != 'yes') :
$paypal_args['tax'] = $order->get_total_tax();
$paypal_args['tax_cart'] = $order->get_total_tax();
endif;
if ($this->send_shipping=='yes') :
$paypal_args['no_shipping'] = 0;
$paypal_args['address_override'] = 1;
else :
$paypal_args['no_shipping'] = 1;
endif;
// Cart Contents
$item_loop = 0;
if (sizeof($order->items)>0) : foreach ($order->items as $item) :
if(!empty($item['variation_id'])) {
$_product = new jigoshop_product_variation($item['variation_id']);
} else {
$_product = new jigoshop_product($item['id']);
}
if ($_product->exists() && $item['qty']) :
$item_loop++;
$title = $_product->get_title();
//if variation, insert variation details into product title
if ($_product instanceof jigoshop_product_variation) {
$variation_details = array();
foreach ($_product->get_variation_attributes() as $name => $value) {
$variation_details[] = ucfirst(str_replace('tax_', '', $name)) . ': ' . ucfirst($value);
}
if (count($variation_details) > 0) {
$title .= ' (' . implode(', ', $variation_details) . ')';
}
}
$paypal_args['item_name_'.$item_loop] = $title;
$paypal_args['quantity_'.$item_loop] = $item['qty'];
// use product price since we want the base price if it's including tax or if it's not including tax
$paypal_args['amount_'.$item_loop] = number_format($_product->get_price(), 2); //Apparently, Paypal did not like "28.4525" as the amount. Changing that to "28.45" fixed the issue.
endif;
endforeach; endif;
// Shipping Cost
$item_loop++;
$paypal_args['item_name_'.$item_loop] = __('Shipping cost', 'jigoshop');
$paypal_args['quantity_'.$item_loop] = '1';
$shipping_tax = ($order->order_shipping_tax ? $order->order_shipping_tax : 0);
$paypal_args['amount_'.$item_loop] = (get_option('jigoshop_prices_include_tax') == 'yes' ? number_format($order->order_shipping + $shipping_tax, 2) : number_format($order->order_shipping, 2));
$paypal_args_array = array();
foreach ($paypal_args as $key => $value) {
$paypal_args_array[] = '';
}
return '';
}
/**
* Process the payment and return the result
**/
function process_payment( $order_id ) {
$order = new jigoshop_order( $order_id );
return array(
'result' => 'success',
'redirect' => add_query_arg('order', $order->id, add_query_arg('key', $order->order_key, get_permalink(jigoshop_get_page_id('pay'))))
);
}
/**
* receipt_page
**/
function receipt_page( $order ) {
echo ''.__('Thank you for your order, please click the button below to pay with PayPal.', 'jigoshop').'
';
echo $this->generate_paypal_form( $order );
}
/**
* Check PayPal IPN validity
**/
function check_ipn_request_is_valid() {
// Add cmd to the post array
$_POST['cmd'] = '_notify-validate';
// Send back post vars to paypal
$params = array( 'body' => $_POST, 'sslverify' => apply_filters('https_local_ssl_verify', 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)) {
return true;
}
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 (self::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 jigoshop_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', 'jigoshop') );
$order->payment_complete();
break;
case 'denied' :
case 'expired' :
case 'failed' :
case 'voided' :
// Hold order
$order->update_status('on-hold', sprintf(__('Payment %s via IPN.', 'jigoshop'), strtolower($posted['payment_status']) ) );
break;
default:
// No action
break;
endswitch;
endif;
exit;
}
}
}
/**
* Add the gateway to JigoShop
**/
function add_paypal_gateway( $methods ) {
$methods[] = 'paypal'; return $methods;
}
add_filter('jigoshop_payment_gateways', 'add_paypal_gateway' );