'commerce_paypal_wps',
'title' => t('PayPal WPS'),
'short_title' => t('PayPal'),
'description' => t('PayPal Website Payments Standard'),
'terminal' => FALSE,
'offsite' => TRUE,
'offsite_autoredirect' => TRUE,
// Because the order form generation code does not have access to a payment
// method info array, we set the bn directly there instead of making use of
// this buttonsource variable. It's here for consistency with other payment
// methods in this package.
'buttonsource' => 'CommerceGuys_Cart_PPS',
);
return $payment_methods;
}
/**
* Returns the default settings for the PayPal WPS payment method.
*/
function commerce_paypal_wps_default_settings() {
$default_currency = commerce_default_currency();
return array(
'business' => '',
'currency_code' => in_array($default_currency, array_keys(commerce_paypal_currencies('paypal_wps'))) ? $default_currency : 'USD',
'allow_supported_currencies' => FALSE,
'language' => 'US',
'server' => 'sandbox',
'payment_action' => 'sale',
'ipn_logging' => 'notification',
'cart_type' => 'summary',
'receiver_emails' => '',
'ipn_create_billing_profile' => FALSE,
'show_payment_instructions' => FALSE,
);
}
/**
* Payment method callback: settings form.
*/
function commerce_paypal_wps_settings_form($settings = array()) {
$form = array();
// Merge default settings into the stored settings array.
$settings = (array) $settings + commerce_paypal_wps_default_settings();
$form['business'] = array(
'#type' => 'textfield',
'#title' => t('PayPal e-mail address'),
'#description' => t('The primary e-mail address of the PayPal account you want to use to receive payments.'),
'#default_value' => $settings['business'],
'#required' => TRUE,
);
$form['cart_type'] = array(
'#type' => 'radios',
'#title' => t('Select the cart options'),
'#options' => array(
'summary' => t('Send a summary of cart contents as one line item to PayPal.'),
'itemized' => t('Send itemized list of cart items to PayPal.'),
),
'#default_value' => $settings['cart_type'],
);
$form['currency_code'] = array(
'#type' => 'select',
'#title' => t('Default currency'),
'#description' => t('Transactions in other currencies will be converted to this currency, so multi-currency sites must be configured to use appropriate conversion rates.'),
'#options' => commerce_paypal_currencies('paypal_wps'),
'#default_value' => $settings['currency_code'],
);
$form['allow_supported_currencies'] = array(
'#type' => 'checkbox',
'#title' => t('Allow transactions to use any currency in the options list above.'),
'#description' => t('Transactions in unsupported currencies will still be converted into the default currency.'),
'#default_value' => $settings['allow_supported_currencies'],
);
$form['language'] = array(
'#type' => 'select',
'#title' => t('PayPal login page language / locale'),
'#options' => commerce_paypal_wps_languages(),
'#default_value' => $settings['language'],
);
$form['server'] = array(
'#type' => 'radios',
'#title' => t('PayPal server'),
'#options' => array(
'sandbox' => ('Sandbox - use for testing, requires a PayPal Sandbox account'),
'live' => ('Live - use for processing real transactions'),
),
'#default_value' => $settings['server'],
);
$form['payment_action'] = array(
'#type' => 'radios',
'#title' => t('Payment action'),
'#options' => array(
'sale' => t('Sale - authorize and capture the funds at the time the payment is processed'),
'authorization' => t('Authorization - reserve funds on the card to be captured later through your PayPal account'),
),
'#default_value' => $settings['payment_action'],
);
$form['ipn_logging'] = array(
'#type' => 'radios',
'#title' => t('IPN logging'),
'#options' => array(
'notification' => t('Log notifications during IPN validation and processing.'),
'full_ipn' => t('Log notifications with the full IPN during validation and processing (used for debugging).'),
),
'#default_value' => $settings['ipn_logging'],
);
$form['receiver_emails'] = array(
'#type' => 'textfield',
'#title' => t('PayPal receiver e-mail addresses'),
'#description' => t('Enter the primary e-mail address for your PayPal account if different from the one entered above or a comma separated list of all valid e-mail addresses on the account.') . '
' . t('IPNs that originate from payments made to a PayPal account whose e-mail address is not in this list will not be processed.'),
'#default_value' => $settings['receiver_emails'],
);
$form['ipn_create_billing_profile'] = array(
'#type' => 'checkbox',
'#title' => t('Create a billing profile based on name and country data in the IPN for any order that does not have one yet.'),
'#description' => t('This is most useful for sites that do not collect billing information locally but still want to have customer names on orders.'),
'#default_value' => $settings['ipn_create_billing_profile'],
);
$form['show_payment_instructions'] = array(
'#type' => 'checkbox',
'#title' => t('Show a message on the checkout form when PayPal WPS is selected telling the customer to "Continue with checkout to complete payment via PayPal."'),
'#default_value' => $settings['show_payment_instructions'],
);
return $form;
}
/**
* Payment method callback: adds a message to the submission form if enabled in
* the payment method settings.
*/
function commerce_paypal_wps_submit_form($payment_method, $pane_values, $checkout_pane, $order) {
$form = array();
if (!empty($payment_method['settings']['show_payment_instructions'])) {
$form['paypal_wps_information'] = array(
'#markup' => '' . t('(Continue with checkout to complete payment via PayPal.)') . '',
);
}
return $form;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function commerce_paypal_wps_form_commerce_checkout_form_alter(&$form, &$form_state) {
// If this checkout form contains the payment method radios...
if (!empty($form['commerce_payment']['payment_method']['#options'])) {
// Loop over its options array looking for a PayPal WPS option.
foreach ($form['commerce_payment']['payment_method']['#options'] as $key => &$value) {
list($method_id, $rule_name) = explode('|', $key);
// If we find PayPal WPS...
if ($method_id == 'paypal_wps') {
// Prepare the replacement radio button text with icons.
$icons = commerce_paypal_icons();
$value = t('!logo PayPal - pay securely without sharing your financial information', array('!logo' => $icons['paypal']));
$value .= '
' . check_plain(print_r($_POST, TRUE)) . ''), WATCHDOG_NOTICE); } // This may be an unnecessary step, but if for some reason the user does end // up returning at the success URL with a Failed payment, go back. if (!empty($_POST['payment_status']) && $_POST['payment_status'] == 'Failed') { return FALSE; } } /** * Payment method callback: validate an IPN based on receiver e-mail address, * price, and other parameters as possible. */ function commerce_paypal_wps_paypal_ipn_validate($order, $payment_method, $ipn) { // Prepare a trimmed list of receiver e-mail addresses. if (!empty($payment_method['settings']['receiver_emails'])) { $receiver_emails = explode(',', $payment_method['settings']['receiver_emails']); } else { $receiver_emails = array(); } // Add the business e-mail address to the list of addresses. $receiver_emails[] = $payment_method['settings']['business']; foreach ($receiver_emails as $key => &$email) { $email = trim(strtolower($email)); } // Return FALSE if the receiver e-mail does not match one specified by the // payment method instance. if (!in_array(trim(strtolower($ipn['receiver_email'])), $receiver_emails)) { commerce_payment_redirect_pane_previous_page($order); watchdog('commerce_paypal_wps', 'IPN rejected: invalid receiver e-mail specified (@receiver_email); must match the primary e-mail address on the PayPal account.', array('@receiver_email' => $ipn['receiver_email']), WATCHDOG_NOTICE); return FALSE; } // Prepare the IPN data for inclusion in the watchdog message if enabled. $ipn_data = ''; if (!empty($payment_method['settings']['ipn_logging']) && $payment_method['settings']['ipn_logging'] == 'full_ipn') { $ipn_data = '
' . check_plain(print_r($ipn, TRUE)) . ''; } // Log a message including the PayPal transaction ID if available. if (!empty($ipn['txn_id'])) { watchdog('commerce_paypal_wps', 'IPN validated for Order @order_number with ID @txn_id.!ipn_data', array('@order_number' => $order->order_number, '@txn_id' => $ipn['txn_id'], '!ipn_data' => $ipn_data), WATCHDOG_NOTICE); } else { watchdog('commerce_paypal_wps', 'IPN validated for Order @order_number.!ipn_data', array('@order_number' => $order->order_number, '!ipn_data' => $ipn_data), WATCHDOG_NOTICE); } } /** * Payment method callback: process an IPN once it's been validated. */ function commerce_paypal_wps_paypal_ipn_process($order, $payment_method, &$ipn) { // Do not perform any processing on WPS transactions here that do not have // transaction IDs, indicating they are non-payment IPNs such as those used // for subscription signup requests. if (empty($ipn['txn_id'])) { return FALSE; } // Exit when we don't get a payment status we recognize. if (!in_array($ipn['payment_status'], array('Failed', 'Voided', 'Pending', 'Completed', 'Refunded'))) { commerce_payment_redirect_pane_previous_page($order); return FALSE; } // If this is a prior authorization capture IPN for which we've already // created a transaction... if (in_array($ipn['payment_status'], array('Voided', 'Completed')) && !empty($ipn['auth_id']) && $auth_ipn = commerce_paypal_ipn_load($ipn['auth_id'])) { // Load the prior IPN's transaction and update that with the capture values. $transaction = commerce_payment_transaction_load($auth_ipn['transaction_id']); } else { // Create a new payment transaction for the order. $transaction = commerce_payment_transaction_new('paypal_wps', $order->order_id); $transaction->instance_id = $payment_method['instance_id']; } $transaction->remote_id = $ipn['txn_id']; $transaction->amount = commerce_currency_decimal_to_amount($ipn['mc_gross'], $ipn['mc_currency']); $transaction->currency_code = $ipn['mc_currency']; $transaction->payload[REQUEST_TIME . '-ipn'] = $ipn; // Set the transaction's statuses based on the IPN's payment_status. $transaction->remote_status = $ipn['payment_status']; // If we didn't get an approval response code... switch ($ipn['payment_status']) { case 'Failed': $transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE; $transaction->message = t("The payment has failed. This happens only if the payment was made from your customer’s bank account."); break; case 'Voided': $transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE; $transaction->message = t('The authorization was voided.'); break; case 'Pending': $transaction->status = COMMERCE_PAYMENT_STATUS_PENDING; $transaction->message = commerce_paypal_ipn_pending_reason($ipn['pending_reason']); break; case 'Completed': $transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS; $transaction->message = t('The payment has completed.'); break; case 'Refunded': $transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS; $transaction->message = t('Refund for transaction @txn_id', array('@txn_id' => $ipn['parent_txn_id'])); break; } // Save the transaction information. commerce_payment_transaction_save($transaction); $ipn['transaction_id'] = $transaction->transaction_id; // Create a billing profile based on the IPN if enabled. if (!empty($payment_method['settings']['ipn_create_billing_profile']) && isset($order->commerce_customer_billing)) { $order_wrapper = entity_metadata_wrapper('commerce_order', $order); // If this order does not have a billing profile yet... if ($order_wrapper->commerce_customer_billing->value() === NULL) { // Ensure we have the required data in the IPN. if (empty($ipn['residence_country']) || empty($ipn['first_name']) || empty($ipn['last_name'])) { $data = array_intersect_key($ipn, drupal_map_assoc(array('residence_country', 'first_name', 'last_name'))); watchdog('commerce_paypal_wps', 'A billing profile for Order @order_number could not be created due to insufficient data in the IPN:!data', array('@order_number' => $order->order_number, '!data' => '
'. check_plain(print_r($data, TRUE)) .''), WATCHDOG_WARNING); } else { // Create the new profile now. $profile = commerce_customer_profile_new('billing', $order->uid); // Add the address value. $profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $profile); $profile_wrapper->commerce_customer_address = array_merge(addressfield_default_values(), array( 'country' => $ipn['residence_country'], 'name_line' => $ipn['first_name'] . ' ' . $ipn['last_name'], 'first_name' => $ipn['first_name'], 'last_name' => $ipn['last_name'], )); // Save the profile, reference it from the order, and save the order. $profile_wrapper->save(); $order_wrapper->commerce_customer_billing = $profile_wrapper; $order_wrapper->save(); watchdog('commerce_paypal_wps', 'Billing profile created for Order @order_number containing the first and last names and residence country of the customer based on IPN data.', array('@order_number' => $order->order_number)); } } } commerce_payment_redirect_pane_next_page($order); watchdog('commerce_paypal_wps', 'IPN processed for Order @order_number with ID @txn_id.', array('@txn_id' => $ipn['txn_id'], '@order_number' => $order->order_number), WATCHDOG_INFO); } /** * Builds a Website Payments Standard form from an order object. * * @param $order * The fully loaded order being paid for. * @param $settings * An array of settings used to build out the form, including: * - server: which server to use, either sandbox or live * - business: the PayPal e-mail address the payment submits to * - cancel_return: the URL PayPal should send the user to on cancellation * - return: the URL PayPal should send the user to on successful payment * - currency_code: the PayPal currency code to use for this payment if the * total for the order is in a non-PayPal supported currency * - language: the PayPal language code to use on the payment form * - payment_action: the PayPal payment action to use: sale, authorization, * or order * - payment_method: optionally a payment method instance ID to include in the * IPN notify_url * * @return * A renderable form array. */ function commerce_paypal_wps_order_form($form, &$form_state, $order, $settings) { $wrapper = entity_metadata_wrapper('commerce_order', $order); // Determine the currency code to use to actually process the transaction, // which will either be the default currency code or the currency code of the // order if it's supported by PayPal if that option is enabled. $currency_code = $settings['currency_code']; $order_currency_code = $wrapper->commerce_order_total->currency_code->value(); if (!empty($settings['allow_supported_currencies']) && in_array($order_currency_code, array_keys(commerce_paypal_currencies('paypal_wps')))) { $currency_code = $order_currency_code; } $amount = $wrapper->commerce_order_total->amount->value(); // Ensure a default value for the payment_method setting. $settings += array('payment_method' => ''); $order_tax_amount = 0; if (module_exists('commerce_tax')) { $order_price = $wrapper->commerce_order_total->value(); $order_tax_amount = commerce_tax_total_amount($order_price['data']['components'], FALSE, $currency_code); } if ($settings['cart_type'] == 'itemized') { // If an itemized shopping cart should be sent to PayPal. $wrapper = entity_metadata_wrapper('commerce_order', $order); $count = 1; $shipping_total = 0; foreach ($wrapper->commerce_line_items as $delta => $line_item_wrapper) { $line_item_price = $line_item_wrapper->commerce_unit_price->value(); $line_item_amount = commerce_currency_convert($line_item_price['amount'], $currency_code, $line_item_price['currency_code']); $line_item_name = commerce_line_item_title($line_item_wrapper->value()); // Apply to cart-wide handling_cart if this is a shipping line item. if (module_exists('commerce_shipping') && ($line_item_wrapper->type->value() == 'shipping')) { $shipping_total += commerce_currency_amount_to_decimal($line_item_amount, $currency_code); continue; } $data['item_name_' . $count] = empty($line_item_name) ? t('Item @number', array('@number' => $count)) : $line_item_name; $data['item_number_' . $count] = $line_item_wrapper->line_item_label->value(); $data['quantity_' . $count] = round($line_item_wrapper->quantity->value()); $data['amount_' . $count] = commerce_currency_amount_to_decimal($line_item_amount, $currency_code); $data['shipping_' . $count] = 0; // Add product attributes. if (module_exists('commerce_option')) { $product_options = ''; $options_count = 0; $iterated_line_item = $line_item_wrapper->value(); $options = commerce_option_load_by_line_item($iterated_line_item->line_item_id); foreach ($options as $option) { field_attach_prepare_view('commerce_option', array($option->option_id => $option), 'attribute_view'); $option_view = field_attach_view('commerce_option', $option, 'attribute_view'); $product_options .= check_markup(drupal_render($option_view), 'crop_html'); $product_options = explode(':', $product_options); $data['on' . $options_count . '_' . $count] = $product_options[0]; $data['os' . $options_count . '_' . $count] = $product_options[1]; $options_count++; } } $count++; } $data['tax_cart'] = commerce_currency_amount_to_decimal($order_tax_amount, $currency_code); if ($shipping_total > 0) { $data['handling_cart'] = $shipping_total; } } else { // Define a single item in the cart representing the whole order. $order_amount = commerce_currency_convert($amount, $order_currency_code, $currency_code); $order_shipping_amount = 0; if (module_exists('commerce_shipping')) { foreach ($wrapper->commerce_line_items as $delta => $line_item_wrapper) { if ($line_item_wrapper->type->value() == 'shipping') { $line_item_price = $line_item_wrapper->commerce_unit_price->value(); $line_item_amount = commerce_currency_amount_to_decimal(commerce_currency_convert($line_item_price['amount'], $line_item_price['currency_code'], $currency_code), $currency_code); $order_shipping_amount += $line_item_amount; } } } $data = array( 'amount' => commerce_currency_amount_to_decimal($order_amount - $order_tax_amount - $order_shipping_amount, $currency_code), 'shipping' => commerce_currency_amount_to_decimal($order_shipping_amount, $currency_code), 'tax' => commerce_currency_amount_to_decimal($order_tax_amount, $currency_code), 'item_name' => t('Order @order_number at @store', array('@order_number' => $order->order_number, '@store' => variable_get('site_name', url('