? contrib/paypalpro/ppp.patch
Index: contrib/paypalpro/paypalpro.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/ecommerce/contrib/paypalpro/paypalpro.module,v
retrieving revision 1.12
diff -u -F^f -r1.12 paypalpro.module
--- contrib/paypalpro/paypalpro.module 7 Apr 2006 05:21:16 -0000 1.12
+++ contrib/paypalpro/paypalpro.module 19 May 2006 20:34:58 -0000
@@ -27,15 +27,7 @@ function paypalpro_menu($may_cache) {
if ($may_cache) {
$items[] = array(
'path' => 'paypalpro/form', 'title' => t('Credit card payment'),
- 'callback' => 'paypalpro_page', 'access' => TRUE,
- 'type' => MENU_CALLBACK);
- $items[] = array(
- 'path' => 'paypalpro/redirect', 'title' => t('Express checkout redirect'),
- 'callback' => 'paypalpro_express_checkout_redirect', 'access' => TRUE,
- 'type' => MENU_CALLBACK);
- $items[] = array(
- 'path' => 'paypalpro/express', 'title' => t('PayPal Express Checkout'),
- 'callback' => 'paypalpro_express_checkout', 'access' => TRUE,
+ 'callback' => 'paypalpro_form', 'access' => TRUE,
'type' => MENU_CALLBACK);
}
@@ -43,7 +35,7 @@ function paypalpro_menu($may_cache) {
}
/**
- * Implementation of Drupal _settings() hook.
+ * Implementation of E-Commerce _settings() hook.
*
* @return form Form used to configure the paypalpro module.
*/
@@ -141,200 +133,297 @@ function paypalpro_ec_settings() {
}
/**
- * Implementation of Drupal _page() hook.
+ * Implementation of _form() hook. This form is used to collect credit card
+ * information.
*
- * @param $txnid Optional transaction id.
+ * @param $txnid Transaction id.
+ * @param form Credit card form.
*/
-function paypalpro_page($txnid = NULL) {
- $edit = $_POST['edit'];
- $op = $_POST['op'];
+function paypalpro_form($txnid = NULL) {
+ global $user;
- switch ($op) {
- case t('Place your order'):
- if (paypalpro_validate($edit)) {
- paypalpro_process($edit);
- }
- else {
- $output = paypalpro_form($edit['txnid']);
- }
- break;
+ $t = store_transaction_load($txnid);
+ // make sure the current users owns this transaction (or is the site admin)
+ // if configured, require that the user access this page via https://
+ if (($user->uid != $t->uid && $user->uid != 1) ||
+ (variable_get('paypalpro_secure', 1) && !$_SERVER['HTTPS'])) {
+ drupal_access_denied();
+ return; /* make sure no more output is returned */
+ }
- default:
- $output = paypalpro_form($txnid);
+
+ // prepare the values of the form fields
+ $years = drupal_map_assoc(range(2004, 2020));
+ $months = drupal_map_assoc(array('01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'));
+ // array includes credit card images
+ $paypalpro_cc_types = array(
+ '
'. t('Visa'),
+ '
'. t('MasterCard'),
+ '
'. t('Discover'),
+ '
'. t('American Express')
+ );
+
+
+ $form['#method'] = 'POST';
+ // display optional form help text
+ $form['help'] = array(
+ '#type' => 'markup',
+ '#prefix' => '
', + '#suffix' => '
', + '#value' => theme('item_list', $items, t('Your items')), + ); + + $form['cc'] = array( + '#type' => 'fieldset', + '#title' => t('Credit card details'), + ); + + // Credit card information + $form['cc']['name'] = array( + '#type' => 'fieldset', + '#title' => 'Enter your name as it appears on the card', + ); + $form['cc']['name']['cc_first_name'] = array( + '#type' => 'textfield', + '#title' => t('First name'), + '#default_value' => $t->billing_firstname, + '#size' => 21, + '#maxlength' => 42, + '#description' => '', + '#attributes' => NULL, + '#required' => TRUE, + ); + $form['cc']['name']['cc_middle_name'] = array( + '#type' => 'textfield', + '#title' => t('Middle name or initial'), + '#size' => 21, + '#maxlength' => 42, + '#description' => '', + ); + $form['cc']['name']['cc_last_name'] = array( + '#type' => 'textfield', + '#title' => t('Last name'), + '#default_value' => $t->billing_lastname, + '#size' => 21, + '#maxlength' => 42, + '#description' => '', + '#attributes' => NULL, + '#required' => TRUE, + ); + + + // the card type and card numbers + $form['cc']['number'] = array( + '#type' => 'fieldset', + '#title' => 'Select a credit card type and enter your card number', + ); + $form['cc']['number']['cc_type'] = array( + '#type' => 'radios', + '#title' => t('Card type'), + '#options' => $paypalpro_cc_types, + '#description' => t('Select the type of credit card you would like to use to make your payment.'), + '#required' => NULL, + '#attributes' => NULL, + ); + // todo: allow numbers with spaces and dashes (convert on-the-fly) + $form['cc']['number']['cc_number'] = array( + '#type' => 'textfield', + '#title' => t('Card number'), + '#size' => 21, + '#maxlength' => 21, + '#description' => t('Please enter your credit card number without any spaces or dashes.'), + '#attributes' => NULL, + '#required' => TRUE, + ); + $form['cc']['number']['cvv2'] = array( + '#type' => 'textfield', + '#title' => t('CCV2'), + '#size' => 3, + '#maxlength' => 4, + '#description' => t('The CCV2 is a 3 digit number located on the back of Visa, MasterCard and Discover credit cards in the signature panel, and a 4 digit number located on the front of an American Express card above and to the right of the imprinted card number. This number is used to provide additional security to internet orders.'), + '#attributes' => NULL, + '#required' => TRUE, + ); + + // the expiration date + $form['cc']['date'] = array( + '#type' => 'fieldset', + '#title' => t('Select your credit card\'s expiration date'), + ); + $form['cc']['date']['cc_month'] = array( + '#type' => 'select', + '#title' => t('Month'), + '#default_value' => date('m'), + '#options' => $months, + '#description' => NULL, + '#extra' => 0, + '#multiple' => false, + '#required' => TRUE, + ); + $form['cc']['date']['cc_year'] = array( + '#type' => 'select', + '#title' => t('Year'), + '#default_value' => date('Y'), + '#options' => $years, + '#description' => NULL, + '#extra' => 0, + '#multiple' => false, + '#required' => TRUE, + ); - print theme('page', $output); + $form['txnid'] = array( + '#type' => 'hidden', + '#value' => $txnid, + ); + $form[] = array( + '#type' => 'submit', + '#value' => t('Place your order'), + ); + + return drupal_get_form('paypalpro', $form); } /** * Implementation of Drupal _validate() hook. * + * @param $form_id Our form id * @param $edit Form array to validate. * @return boolean True if form validates, false if not. */ -function paypalpro_validate($edit) { - $valid = TRUE; +function paypalpro_validate($form_id, $form_values) { $paypalpro_cc_types = array(t('Visa'), t('MasterCard'), t('Discover'), t('American Express')); $type = 'invalid'; - if (!$edit['cc_first_name']) { - form_set_error('cc_first_name', t('Please enter your first name how it appear on your credit card.')); - $valid = FALSE; - } - if (!$edit['cc_last_name']) { - form_set_error('cc_last_name', t('Please enter your last name how it appear on your credit card.')); - $valid = FALSE; - } - if (!$edit['cc_number']) { - form_set_error('cc_number', t('Please enter a credit card number.')); - $valid = FALSE; - } - elseif (!is_numeric($edit['cc_number'])) { + if (!is_numeric($form_values['cc_number'])) { form_set_error('cc_number', t('Please enter a valid credit card number.')); - $valid = FALSE; } - elseif (($edit['cc_year'] < date('Y')) || ($edit['cc_year'] <= date('Y')) && - ($edit['cc_month'] < date('m'))) { + if (($form_values['cc_year'] < date('Y')) || ($form_values['cc_year'] <= date('Y')) && + ($form_values['cc_month'] < date('m'))) { form_set_error('cc_month', t('Your credit card has expired. Please try another card.')); - $valid = FALSE; } - else { - // Verify that the credit card type matches the number of digits in the - // credit card. - $length = strlen($edit['cc_number']); - if ($length == 13) { - if (substr($edit['cc_number'], 0, 1) == '4') { - $type = t('Visa'); - } - } - elseif ($length == 16) { - if (substr($edit['cc_number'], 0, 1) == '4') { - $type = t('Visa'); - } - if (substr($edit['cc_number'], 0, 1) == '5') { - $type = t('MasterCard'); - } - elseif (substr($edit['cc_number'], 0, 4) == '6011') { - $type = t('Discover'); - } - } - elseif ($length == 15) { - if (substr($edit['cc_number'], 0, 1) == '3') { - $type = t('American Express'); - } - } - if ($type != $paypalpro_cc_types[$edit['cc_type']]) { - form_set_error('cc_number', t('The credit card number you have entered is not a valid %type credit card number. Please fix the credit card type, or re-enter the credit card number.', array('%type' => $paypalpro_cc_types[$edit['cc_type']]))); - $valid = FALSE; + + // Verify that the credit card type matches the number of digits in the + // credit card. + $length = strlen($form_values['cc_number']); + if ($length == 13) { + if (substr($form_values['cc_number'], 0, 1) == '4') { + $type = t('Visa'); } - // TODO: Different cards refer to this number with a different term. - // Visa = CVV2 (card verification value) - // MasterCard = CVC2 (card validation code) - // Discover = Cardmember ID - // American Express = CID (Card Identification Number) - elseif (!$edit['cvv2']) { - form_set_error('cvv2', t('Please enter a CCV2 number.')); - $valid = FALSE; + } + elseif ($length == 16) { + if (substr($form_values['cc_number'], 0, 1) == '4') { + $type = t('Visa'); } - elseif (!is_numeric($edit['cvv2'])) { - form_set_error('cvv2', t('Please enter a valid CCV2 number. Non-numeric characters are not allowed.')); - $valid = FALSE; + if (substr($form_values['cc_number'], 0, 1) == '5') { + $type = t('MasterCard'); } - elseif (($edit['cc_type'] == 3) && (strlen($edit['cvv2']) != 4)) { - form_set_error('cvv2', t('Please enter a valid 4 digit CCV2 number. The CCV2 number on your %type credit card is located on the front above and to the right of the imprinted card number.', array('%type' => $paypalpro_cc_types[$edit['cc_type']]))); - $valid = FALSE; + elseif (substr($form_values['cc_number'], 0, 4) == '6011') { + $type = t('Discover'); } - elseif (($edit['cc_type'] != 3) && (strlen($edit['cvv2']) != 3)) { - form_set_error('cvv2', t('Please enter a valid 3 digit CCV2 number. The CCV2 number on your %type credit card is located on the back in the signature panel after the credit card number.', array('%type' => $paypalpro_cc_types[$edit['cc_type']]))); - $valid = FALSE; + } + elseif ($length == 15) { + if (substr($form_values['cc_number'], 0, 1) == '3') { + $type = t('American Express'); } } + if ($type != $paypalpro_cc_types[$form_values['cc_type']]) { + form_set_error('cc_number', t('The credit card number you have entered is not a valid %type credit card number. Please fix the credit card type, or re-enter the credit card number.', array('%type' => $paypalpro_cc_types[$form_values['cc_type']]))); + } - return $valid; + // TODO: Different cards refer to this number with a different term. + // Visa = CVV2 (card verification value) + // MasterCard = CVC2 (card validation code) + // Discover = Cardmember ID + // American Express = CID (Card Identification Number) + if (!is_numeric($form_values['cvv2'])) { + form_set_error('cvv2', t('Please enter a valid CCV2 number. Non-numeric characters are not allowed.')); + } + elseif (($form_values['cc_type'] == 3) && (strlen($form_values['cvv2']) != 4)) { + form_set_error('cvv2', t('Please enter a valid 4 digit CCV2 number. The CCV2 number on your %type credit card is located on the front above and to the right of the imprinted card number.', array('%type' => $paypalpro_cc_types[$form_values['cc_type']]))); + } + elseif (($form_values['cc_type'] != 3) && (strlen($form_values['cvv2']) != 3)) { + form_set_error('cvv2', t('Please enter a valid 3 digit CCV2 number. The CCV2 number on your %type credit card is located on the back in the signature panel after the credit card number.', array('%type' => $paypalpro_cc_types[$form_values['cc_type']]))); + } } /** - * Implementation of e-commerce _form() hook. This form is used to collect - * credit card information. + * Process a credit card transaction. Makes a curl connection to PayPal's API + * server to validate the credit card. We manually process the returned SOAP + * string rather than using PEAR and PayPal's PHP API. After a succesful + * transaction, the transaction information is stored in the local database. + * After a failed transaction, the user is redirected back to the credit card + * form and provided a helpful error to explain what is wrong. * - * @param $txnid Transaction id. - * @param form Credit card form. + * @param $edit The $edit array. */ -function paypalpro_form($txnid) { +function paypalpro_submit($form_id, $form_values) { global $user, $base_url; - if ($_POST['edit']) { - $edit = $_POST['edit']; - } - else if ($_SESSION['edit']) { - // paypalpro_goto saves the edit array in the session - $edit = $_SESSION['edit']; - } - // it doesn't hurt to unset this even if it's not set - unset ($_SESSION['edit']); - - // array includes credit card images - $paypalpro_cc_types = array('
'. t('Visa'), '
'. t('MasterCard'), '
'. t('Discover'), '
'. t('American Express'));
-
- $t = store_transaction_load($txnid);
-
- // make sure the current users owns this transaction (or is the site admin)
- if ($user->uid != $t->uid && $user->uid != 1) {
- drupal_access_denied();
- }
+ $t = store_transaction_load($form_values['txnid']);
- // if configured, require that the user access this page via https://
- if (variable_get('paypalpro_secure', 1) && !$_SERVER['HTTPS']) {
- drupal_access_denied();
- }
+ // TODO: validate url, cert, etc...
- // display optional form help text
- $output = t(''. theme('item_list', $items, t('Your items')). '
'; - // prepare the values of the form fields - $years = drupal_map_assoc(range(2004, 2020)); - $months = drupal_map_assoc(array('01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12')); + // parse the paymentreply SOAP message, look for 'Success' or 'Failure' + if (strpos($exec_return, 'Success')) { + drupal_set_message(t('Transaction approved. Thank you for your order!')); + $t->proid = paypalpro_parse_xml($exec_return, '
- ". t('One moment please, contacting PayPal.') ."
-
-