'https://www.paypal.com/cgi-bin/webscr', SIMPLE_PAYPAL_URL_TEST => 'https://www.sandbox.paypal.com/cgi-bin/webscr', ); } /** * Implementation of hook_menu(). */ function simple_paypal_menu() { $items = array(); $items['admin/settings/paypal'] = array( 'title' => 'Paypal', 'page callback' => 'drupal_get_form', 'page arguments' => array('simple_paypal_admin'), 'description' => 'Administer Paypal', 'access arguments' => array('access administration pages'), 'type' => MENU_NORMAL_ITEM, ); return $items; } /** * Implementation of hook_admin(). */ function simple_paypal_admin() { $form[SIMPLE_PAYPAL_URL] = array( '#type' => 'select', '#title' => t('Payment URL for Paypal'), '#default_value' => variable_get(SIMPLE_PAYPAL_URL, SIMPLE_PAYPAL_URL_LIVE), '#options' => simple_paypal_get_urls(), '#description' => t('Select whether you want to use a live URL, or a test one.'), ); $form[SIMPLE_PAYPAL_DEFAULT_CURRENCY] = array( '#type' => 'select', '#title' => t('Default Currency'), '#default_value' => variable_get(SIMPLE_PAYPAL_DEFAULT_CURRENCY, 'USD'), '#options' => simple_paypal_get_currencies(), ); return system_settings_form($form); } function simple_paypal_get_url() { $urls = simple_paypal_get_urls(); return $urls[variable_get(SIMPLE_PAYPAL_URL, SIMPLE_PAYPAL_URL_LIVE)]; } function simple_paypal_get_currencies() { $default_currency = variable_get(SIMPLE_PAYPAL_DEFAULT_CURRENCY, 'USD'); $country_currencies = array( 'AUD' => t('Australian Dollar'), 'GBP' => t('British Pound'), 'CAD' => t('Canadian Dollar'), 'CZK' => t('Czech Koruna'), 'DKK' => t('Danish Kroner'), 'EUR' => t('Euro'), 'HKD' => t('Hong Kong Dollar'), 'HUF' => t('Hungarian Forint'), 'ILS' => t('Israeli New Shekel'), 'JPY' => t('Japanese Yen'), 'MXN' => t('Mexican Peso'), 'NZD' => t('New Zealand Dollar'), 'NOK' => t('Norwegian Kroner'), 'PLN' => t('Polish Zlotych'), 'SGD' => t('Singapore Dollar'), 'SEK' => t('Swedish Kronor'), 'CHF' => t('Swiss Franc'), 'USD' => t('U.S. Dollar'), ); if (variable_get(SIMPLE_PAYPAL_DEFAULT_CURRENCY, 'USD')) { $ordered_currencies[$default_currency] = $country_currencies[$default_currency]; foreach ($country_currencies as $key => $value) { if($key != $default_currency) { $ordered_currencies[$key] = $value; } } return $ordered_currencies; } return $country_currencies; } function simple_paypal_format_amount($amount, $currency) { $amount = number_format($amount, 2); switch ($currency) { case 'EUR': return "€ $amount"; case 'GBP': return "£ $amount"; case 'USD': return "$ $amount"; default: return check_plain($currency). " $amount"; } } function simple_paypal_ipn_verify($vars = array()) { // If we are in test mode, log the variables. if (SIMPLE_PAYPAL_URL_TEST == variable_get(SIMPLE_PAYPAL_URL, SIMPLE_PAYPAL_URL_TEST)) { watchdog('simple_paypal', 'Post variables from Paypal IPN
@vars', array( '@vars' => print_r($vars, TRUE)), WATCHDOG_DEBUG); } $url = simple_paypal_get_url(); $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, simple_paypal_post($vars)); ob_start(); if (curl_exec($ch)) { $info = ob_get_contents(); curl_close($ch); ob_end_clean(); if (eregi('VERIFIED', $info)) { return TRUE; } else { return FALSE; } } else { watchdog('simple_paypal', 'Call to curl_exec() failed. url=@url vars=@vars', array( '@vars' => print_r($vars, TRUE), '@url' => $url, ), WATCHDOG_ERROR); return FALSE; } } function simple_paypal_post($data = array()) { $post = ''; foreach ($data as $key => $value) { $post .= $key. '='. urlencode($value). '&'; } $post .= 'cmd=_notify-validate'; return $post; }