| 1 |
<?php
|
| 2 |
|
| 3 |
define('SIMPLE_PAYPAL_URL_LIVE', 0);
|
| 4 |
define('SIMPLE_PAYPAL_URL_TEST', 1);
|
| 5 |
|
| 6 |
define('SIMPLE_PAYPAL_URL', 'simple_paypal_url');
|
| 7 |
define('SIMPLE_PAYPAL_DEFAULT_CURRENCY', 'simple_paypal_default_currency');
|
| 8 |
|
| 9 |
function simple_paypal_get_urls() {
|
| 10 |
return array(
|
| 11 |
SIMPLE_PAYPAL_URL_LIVE => 'https://www.paypal.com/cgi-bin/webscr',
|
| 12 |
SIMPLE_PAYPAL_URL_TEST => 'https://www.sandbox.paypal.com/cgi-bin/webscr',
|
| 13 |
);
|
| 14 |
}
|
| 15 |
|
| 16 |
/**
|
| 17 |
* Implement hook_menu().
|
| 18 |
*/
|
| 19 |
function simple_paypal_menu() {
|
| 20 |
$items = array();
|
| 21 |
|
| 22 |
$items['admin/config/paypal'] = array(
|
| 23 |
'title' => 'PayPal',
|
| 24 |
'description' => 'PayPal.',
|
| 25 |
'page callback' => 'system_admin_menu_block_page',
|
| 26 |
'access arguments' => array('access administration pages'),
|
| 27 |
'file' => 'system.admin.inc',
|
| 28 |
'file path' => drupal_get_path('module', 'system'),
|
| 29 |
);
|
| 30 |
|
| 31 |
$items['admin/config/paypal/settings'] = array(
|
| 32 |
'title' => 'PayPal settings',
|
| 33 |
'page callback' => 'drupal_get_form',
|
| 34 |
'page arguments' => array('simple_paypal_admin'),
|
| 35 |
'access arguments' => array('administer site configuration'),
|
| 36 |
'description' => 'Administer PayPal.',
|
| 37 |
);
|
| 38 |
|
| 39 |
return $items;
|
| 40 |
}
|
| 41 |
|
| 42 |
function simple_paypal_admin() {
|
| 43 |
$form[SIMPLE_PAYPAL_URL] = array(
|
| 44 |
'#type' => 'select',
|
| 45 |
'#title' => t('Payment URL for PayPal'),
|
| 46 |
'#default_value' => variable_get(SIMPLE_PAYPAL_URL, SIMPLE_PAYPAL_URL_LIVE),
|
| 47 |
'#options' => simple_paypal_get_urls(),
|
| 48 |
'#description' => t('Select whether you want to use a live URL, or a test one.'),
|
| 49 |
);
|
| 50 |
$form[SIMPLE_PAYPAL_DEFAULT_CURRENCY] = array(
|
| 51 |
'#type' => 'select',
|
| 52 |
'#title' => t('Default Currency'),
|
| 53 |
'#default_value' => variable_get(SIMPLE_PAYPAL_DEFAULT_CURRENCY, 'USD'),
|
| 54 |
'#options' => simple_paypal_get_currencies(),
|
| 55 |
);
|
| 56 |
return system_settings_form($form);
|
| 57 |
}
|
| 58 |
|
| 59 |
function simple_paypal_get_url() {
|
| 60 |
$urls = simple_paypal_get_urls();
|
| 61 |
return $urls[variable_get(SIMPLE_PAYPAL_URL, SIMPLE_PAYPAL_URL_LIVE)];
|
| 62 |
}
|
| 63 |
|
| 64 |
function simple_paypal_get_currencies() {
|
| 65 |
$default_currency = variable_get(SIMPLE_PAYPAL_DEFAULT_CURRENCY, 'USD');
|
| 66 |
$country_currencies = array(
|
| 67 |
'AUD' => t('Australian Dollar'),
|
| 68 |
'GBP' => t('British Pound'),
|
| 69 |
'CAD' => t('Canadian Dollar'),
|
| 70 |
'CZK' => t('Czech Koruna'),
|
| 71 |
'DKK' => t('Danish Kroner'),
|
| 72 |
'EUR' => t('Euro'),
|
| 73 |
'HKD' => t('Hong Kong Dollar'),
|
| 74 |
'HUF' => t('Hungarian Forint'),
|
| 75 |
'ILS' => t('Israeli New Shekel'),
|
| 76 |
'JPY' => t('Japanese Yen'),
|
| 77 |
'MXN' => t('Mexican Peso'),
|
| 78 |
'NZD' => t('New Zealand Dollar'),
|
| 79 |
'NOK' => t('Norwegian Kroner'),
|
| 80 |
'PLN' => t('Polish Zlotych'),
|
| 81 |
'SGD' => t('Singapore Dollar'),
|
| 82 |
'SEK' => t('Swedish Kronor'),
|
| 83 |
'CHF' => t('Swiss Franc'),
|
| 84 |
'USD' => t('U.S. Dollar'),
|
| 85 |
);
|
| 86 |
|
| 87 |
if (variable_get(SIMPLE_PAYPAL_DEFAULT_CURRENCY, 'USD')) {
|
| 88 |
$ordered_currencies[$default_currency] = $country_currencies[$default_currency];
|
| 89 |
foreach ($country_currencies as $key => $value) {
|
| 90 |
if($key != $default_currency) {
|
| 91 |
$ordered_currencies[$key] = $value;
|
| 92 |
}
|
| 93 |
}
|
| 94 |
return $ordered_currencies;
|
| 95 |
}
|
| 96 |
return $country_currencies;
|
| 97 |
}
|
| 98 |
|
| 99 |
function simple_paypal_format_amount($amount, $currency) {
|
| 100 |
$amount = number_format($amount, 2);
|
| 101 |
switch ($currency) {
|
| 102 |
case 'EUR':
|
| 103 |
return "€ $amount";
|
| 104 |
case 'GBP':
|
| 105 |
return "£ $amount";
|
| 106 |
case 'USD':
|
| 107 |
return "$ $amount";
|
| 108 |
default:
|
| 109 |
return check_plain($currency). " $amount";
|
| 110 |
}
|
| 111 |
}
|
| 112 |
|
| 113 |
function simple_paypal_ipn_verify($vars = array()) {
|
| 114 |
// If we are in test mode, log the variables.
|
| 115 |
if (SIMPLE_PAYPAL_URL_TEST == variable_get(SIMPLE_PAYPAL_URL, SIMPLE_PAYPAL_URL_TEST)) {
|
| 116 |
watchdog('simple_paypal', 'Post variables from PayPal IPN <pre>@vars</pre>', array(
|
| 117 |
'@vars' => print_r($vars, TRUE)), WATCHDOG_DEBUG);
|
| 118 |
}
|
| 119 |
|
| 120 |
$url = simple_paypal_get_url();
|
| 121 |
$ch = curl_init();
|
| 122 |
|
| 123 |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
|
| 124 |
curl_setopt($ch, CURLOPT_URL, $url);
|
| 125 |
curl_setopt($ch, CURLOPT_POST, 1);
|
| 126 |
curl_setopt($ch, CURLOPT_POSTFIELDS, simple_paypal_post($vars));
|
| 127 |
|
| 128 |
ob_start();
|
| 129 |
|
| 130 |
if (curl_exec($ch)) {
|
| 131 |
$info = ob_get_contents();
|
| 132 |
curl_close($ch);
|
| 133 |
ob_end_clean();
|
| 134 |
|
| 135 |
if (eregi('VERIFIED', $info)) {
|
| 136 |
return TRUE;
|
| 137 |
}
|
| 138 |
else {
|
| 139 |
return FALSE;
|
| 140 |
}
|
| 141 |
}
|
| 142 |
else {
|
| 143 |
watchdog('simple_paypal', 'Call to curl_exec() failed. url=@url vars=@vars', array(
|
| 144 |
'@vars' => print_r($vars, TRUE),
|
| 145 |
'@url' => $url,
|
| 146 |
), WATCHDOG_ERROR);
|
| 147 |
return FALSE;
|
| 148 |
}
|
| 149 |
}
|
| 150 |
|
| 151 |
function simple_paypal_post($data = array()) {
|
| 152 |
$post = '';
|
| 153 |
foreach ($data as $key => $value) {
|
| 154 |
$post .= $key. '='. urlencode($value). '&';
|
| 155 |
}
|
| 156 |
$post .= 'cmd=_notify-validate';
|
| 157 |
|
| 158 |
return $post;
|
| 159 |
}
|