uc_credit.admin.inc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. <?php
  2. /**
  3. * @file
  4. * Credit administration menu items.
  5. */
  6. /**
  7. * Credit card settings form.
  8. *
  9. * @see uc_credit_settings_form_validate()
  10. * @see uc_credit_settings_form_submit()
  11. */
  12. function uc_credit_settings_form($form, &$form_state) {
  13. if (!user_access('administer credit cards')) {
  14. $form['notice'] = array(
  15. '#markup' => '<div>' . t('You must have access to <b>administer credit cards</b> to adjust these settings.') . '</div>',
  16. );
  17. return $form;
  18. }
  19. $gateways = _uc_payment_gateway_list('credit');
  20. if (!count($gateways)) {
  21. $form['notice'] = array(
  22. '#markup' => '<div>' . t('Please enable a credit card gateway module for your chosen payment provider.') . '</div>',
  23. );
  24. return $form;
  25. }
  26. $form['uc_credit'] = array(
  27. '#type' => 'vertical_tabs',
  28. '#attached' => array(
  29. 'js' => array(
  30. 'vertical-tabs' => drupal_get_path('module', 'uc_credit') . '/uc_credit.admin.js',
  31. ),
  32. ),
  33. );
  34. $form['cc_basic'] = array(
  35. '#type' => 'fieldset',
  36. '#title' => t('Basic settings'),
  37. '#group' => 'uc_credit',
  38. );
  39. $options = array();
  40. foreach ($gateways as $id => $gateway) {
  41. $options[$id] = $gateway['title'];
  42. }
  43. $form['cc_basic']['uc_payment_credit_gateway'] = array(
  44. '#type' => 'radios',
  45. '#title' => t('Default gateway'),
  46. '#options' => $options,
  47. '#default_value' => uc_credit_default_gateway(),
  48. );
  49. $form['cc_basic']['uc_credit_validate_numbers'] = array(
  50. '#type' => 'checkbox',
  51. '#title' => t('Validate credit card numbers at checkout.'),
  52. '#description' => t('Invalid card numbers will show an error message to the user so they can correct it.'),
  53. '#default_value' => variable_get('uc_credit_validate_numbers', TRUE),
  54. );
  55. // Form elements that deal specifically with card number security.
  56. $form['cc_security'] = array(
  57. '#type' => 'fieldset',
  58. '#title' => t('Security settings'),
  59. '#description' => t('You are responsible for the security of your website, including the protection of credit card numbers. Please be aware that choosing some settings in this section may decrease the security of credit card data on your website and increase your liability for damages in the case of fraud.'),
  60. '#group' => 'uc_credit',
  61. );
  62. $form['cc_security']['uc_credit_encryption_path'] = array(
  63. '#type' => 'textfield',
  64. '#title' => t('Encryption key directory'),
  65. '#description' => t('The card type, expiration date and last four digits of the card number are encrypted and stored temporarily while the customer is in the process of checking out.<br /><b>You must enable encryption</b> by following the <a href="!url">encryption instructions</a> in order to accept credit card payments.<br />In short, you must enter the path of a directory outside of your document root where the encryption key may be stored.<br />Relative paths will be resolved relative to the Drupal installation directory.<br />Once this directory is set, you should not change it.', array('!url' => 'http://drupal.org/node/1309226')),
  66. '#default_value' => uc_credit_encryption_key() ? variable_get('uc_credit_encryption_path', t('Not configured.')) : t('Not configured.'),
  67. );
  68. // Form elements that deal with the type of data requested at checkout.
  69. $form['cc_fields'] = array(
  70. '#type' => 'fieldset',
  71. '#title' => t('Credit card fields'),
  72. '#description' => t('Specify what information to collect from customers in addition to the card number.'),
  73. '#group' => 'uc_credit',
  74. '#weight' => 10,
  75. );
  76. $form['cc_fields']['uc_credit_cvv_enabled'] = array(
  77. '#type' => 'checkbox',
  78. '#title' => t('Enable CVV text field on checkout form.'),
  79. '#description' => t('The CVV is an added security measure on credit cards. On Visa, Mastercard, and Discover cards it is a three digit number, and on AmEx cards it is a four digit number. If your credit card processor or payment gateway requires this information, you should enable this feature here.'),
  80. '#default_value' => variable_get('uc_credit_cvv_enabled', TRUE),
  81. );
  82. $form['cc_fields']['uc_credit_owner_enabled'] = array(
  83. '#type' => 'checkbox',
  84. '#title' => t('Enable card owner text field on checkout form.'),
  85. '#default_value' => variable_get('uc_credit_owner_enabled', FALSE),
  86. );
  87. $form['cc_fields']['uc_credit_start_enabled'] = array(
  88. '#type' => 'checkbox',
  89. '#title' => t('Enable card start date on checkout form.'),
  90. '#default_value' => variable_get('uc_credit_start_enabled', FALSE),
  91. );
  92. $form['cc_fields']['uc_credit_issue_enabled'] = array(
  93. '#type' => 'checkbox',
  94. '#title' => t('Enable card issue number text field on checkout form.'),
  95. '#default_value' => variable_get('uc_credit_issue_enabled', FALSE),
  96. );
  97. $form['cc_fields']['uc_credit_bank_enabled'] = array(
  98. '#type' => 'checkbox',
  99. '#title' => t('Enable issuing bank text field on checkout form.'),
  100. '#default_value' => variable_get('uc_credit_bank_enabled', FALSE),
  101. );
  102. $form['cc_fields']['uc_credit_type_enabled'] = array(
  103. '#type' => 'checkbox',
  104. '#title' => t('Enable card type selection on checkout form.'),
  105. '#description' => t('If enabled, specify in the textarea below which card options to populate the select box with.'),
  106. '#default_value' => variable_get('uc_credit_type_enabled', FALSE),
  107. );
  108. $form['cc_fields']['uc_credit_accepted_types'] = array(
  109. '#type' => 'textarea',
  110. '#title' => t('Card type select box options'),
  111. '#description' => t('Enter one card type per line. These fields will populate the card type select box if it is enabled.'),
  112. '#default_value' => variable_get('uc_credit_accepted_types', implode("\r\n", array(t('Visa'), t('Mastercard'), t('Discover'), t('American Express')))),
  113. );
  114. // Form elements that deal with card types accepted.
  115. $form['cc_fields']['cc_types'] = array(
  116. '#type' => 'fieldset',
  117. '#title' => t('Card types'),
  118. '#description' => t('Use the checkboxes to specify which card types you accept for payment. Selected card types will show their icons in the payment method selection list and be used for card number validation.'),
  119. );
  120. $form['cc_fields']['cc_types']['uc_credit_visa'] = array(
  121. '#type' => 'checkbox',
  122. '#title' => t('Visa'),
  123. '#default_value' => variable_get('uc_credit_visa', TRUE),
  124. );
  125. $form['cc_fields']['cc_types']['uc_credit_mastercard'] = array(
  126. '#type' => 'checkbox',
  127. '#title' => t('Mastercard'),
  128. '#default_value' => variable_get('uc_credit_mastercard', TRUE),
  129. );
  130. $form['cc_fields']['cc_types']['uc_credit_discover'] = array(
  131. '#type' => 'checkbox',
  132. '#title' => t('Discover'),
  133. '#default_value' => variable_get('uc_credit_discover', TRUE),
  134. );
  135. $form['cc_fields']['cc_types']['uc_credit_amex'] = array(
  136. '#type' => 'checkbox',
  137. '#title' => t('American Express'),
  138. '#default_value' => variable_get('uc_credit_amex', TRUE),
  139. );
  140. // Form elements that deal with credit card messages to customers.
  141. $form['cc_messages'] = array(
  142. '#type' => 'fieldset',
  143. '#title' => t('Customer messages'),
  144. '#description' => t('Here you can alter messages displayed to customers using credit cards.'),
  145. '#collapsible' => FALSE,
  146. '#group' => 'uc_credit',
  147. '#weight' => 10,
  148. );
  149. $form['cc_messages']['uc_credit_policy'] = array(
  150. '#type' => 'textarea',
  151. '#title' => t('Credit card payment policy'),
  152. '#description' => t('Instructions for customers on the checkout page above the credit card fields.'),
  153. '#default_value' => variable_get('uc_credit_policy', t('Your billing information must match the billing address for the credit card entered below or we will be unable to process your payment.')),
  154. '#rows' => 3,
  155. );
  156. $form['cc_messages']['uc_credit_fail_message'] = array(
  157. '#type' => 'textarea',
  158. '#title' => t('Card processing failure message'),
  159. '#description' => t('Error message displayed to customers when an attempted payment fails at checkout.'),
  160. '#default_value' => variable_get('uc_credit_fail_message', t('We were unable to process your credit card payment. Please verify your details and try again. If the problem persists, contact us to complete your order.')),
  161. );
  162. $txn_types = array(
  163. UC_CREDIT_AUTH_ONLY => t('Authorization only'),
  164. UC_CREDIT_AUTH_CAPTURE => t('Authorize and capture immediately'),
  165. UC_CREDIT_REFERENCE_SET => t('Set a reference only'),
  166. );
  167. foreach ($gateways as $id => $gateway) {
  168. $form['gateways'][$id] = array(
  169. '#type' => 'fieldset',
  170. '#title' => check_plain($gateway['title']),
  171. '#collapsible' => TRUE,
  172. '#collapsed' => TRUE,
  173. '#group' => 'uc_credit',
  174. '#weight' => 5,
  175. );
  176. $form['gateways'][$id]['uc_pg_' . $id . '_enabled'] = array(
  177. '#type' => 'checkbox',
  178. '#title' => t('Enable this payment gateway for use.'),
  179. '#default_value' => variable_get('uc_pg_' . $id . '_enabled', TRUE),
  180. '#weight' => -10,
  181. );
  182. // Get the transaction types associated with this gateway.
  183. $gateway_types = uc_credit_gateway_txn_types($id);
  184. $options = array();
  185. foreach ($txn_types as $type => $title) {
  186. if (in_array($type, $gateway_types)) {
  187. $options[$type] = $title;
  188. }
  189. }
  190. $form['gateways'][$id]['uc_pg_' . $id . '_cc_txn_type'] = array(
  191. '#type' => 'radios',
  192. '#title' => t('Default credit transaction type'),
  193. '#description' => t('Only available transaction types are listed. The default will be used unless an administrator chooses otherwise through the terminal.'),
  194. '#options' => $options,
  195. '#default_value' => variable_get('uc_pg_' . $id . '_cc_txn_type', UC_CREDIT_AUTH_CAPTURE),
  196. '#weight' => -5,
  197. );
  198. if (isset($gateway['settings']) && function_exists($gateway['settings'])) {
  199. $gateway_settings = $gateway['settings'](array(), $form_state);
  200. if (is_array($gateway_settings)) {
  201. $form['gateways'][$id] += $gateway_settings;
  202. }
  203. }
  204. }
  205. if (empty($_POST) && !uc_credit_encryption_key()) {
  206. drupal_set_message(t('Credit card security settings must be configured in the security settings tab.'), 'warning');
  207. }
  208. $form['#validate'][] = 'uc_credit_settings_form_validate';
  209. $form['#submit'][] = 'uc_credit_settings_form_submit';
  210. return $form;
  211. }
  212. /**
  213. * Validates the encryption key directory and key file.
  214. *
  215. * Checks that the encryption key directory has been specified, that it
  216. * exists, and is readable. and writeable so
  217. *
  218. * @see uc_credit_settings_form()
  219. * @see uc_credit_settings_form_submit()
  220. */
  221. function uc_credit_settings_form_validate($form, &$form_state) {
  222. // Trim trailing whitespace and any trailing / or \ from the key path name.
  223. $key_path = rtrim(trim($form_state['values']['uc_credit_encryption_path']), '/\\');
  224. // Test to see if a path was entered.
  225. if (empty($key_path)) {
  226. form_set_error('uc_credit_encryption_path', t('Key path must be specified in security settings tab.'));
  227. }
  228. // Construct complete key file path.
  229. $key_file = $key_path . '/' . UC_CREDIT_KEYFILE_NAME;
  230. // Shortcut - test to see if we already have a usable key file.
  231. if (file_exists($key_file)) {
  232. if (is_readable($key_file)) {
  233. // Test contents - must contain 32-character hexadecimal string.
  234. $key = uc_credit_encryption_key();
  235. if ($key) {
  236. if (!preg_match("([0-9a-fA-F]{32})", $key)) {
  237. form_set_error('uc_credit_encryption_path', t('Key file already exists in directory, but it contains an invalid key.'));
  238. }
  239. else {
  240. // Key file exists and is valid, save result of trim() back into
  241. // $form_state and proceed to submit handler.
  242. $form_state['values']['uc_credit_encryption_path'] = $key_path;
  243. return;
  244. }
  245. }
  246. }
  247. else {
  248. form_set_error('uc_credit_encryption_path', t('Key file already exists in directory, but is not readable. Please verify the file permissions.'));
  249. }
  250. }
  251. // Check if directory exists and is writeable.
  252. if (is_dir($key_path)) {
  253. // The entered directory is valid and in need of a key file.
  254. // Flag this condition for the submit handler.
  255. $form_state['uc_credit']['update_cc_encrypt_dir'] = TRUE;
  256. // Can we open for writing?
  257. $file = @fopen($key_path . '/encrypt.test', 'w');
  258. if ($file === FALSE) {
  259. form_set_error('uc_credit_encryption_path', t('Cannot write to directory, please verify the directory permissions.'));
  260. unset($form_state['uc_credit']['update_cc_encrypt_dir']);
  261. }
  262. else {
  263. // Can we actually write?
  264. if (@fwrite($file, '0123456789') === FALSE) {
  265. form_set_error('uc_credit_encryption_path', t('Cannot write to directory, please verify the directory permissions.'));
  266. unset($form_state['uc_credit']['update_cc_encrypt_dir']);
  267. fclose($file);
  268. }
  269. else {
  270. // Can we read now?
  271. fclose($file);
  272. $file = @fopen($key_path . '/encrypt.test', 'r');
  273. if ($file === FALSE) {
  274. form_set_error('uc_credit_encryption_path', t('Cannot read from directory, please verify the directory permissions.'));
  275. unset($form_state['uc_credit']['update_cc_encrypt_dir']);
  276. }
  277. else {
  278. fclose($file);
  279. }
  280. }
  281. unlink($key_path . '/encrypt.test');
  282. }
  283. }
  284. else {
  285. // Directory doesn't exist.
  286. form_set_error('uc_credit_encryption_path', t('You have specified a non-existent directory.'));
  287. }
  288. // If validation succeeds, save result of trim() back into $form_state.
  289. $form_state['values']['uc_credit_encryption_path'] = $key_path;
  290. }
  291. /**
  292. * Creates the encryption key file if it doesn't already exist.
  293. *
  294. * @see uc_credit_settings_form()
  295. * @see uc_credit_settings_form_validate()
  296. */
  297. function uc_credit_settings_form_submit($form, &$form_state) {
  298. // Check to see if we need to create a key file.
  299. if (isset($form_state['uc_credit']['update_cc_encrypt_dir']) &&
  300. $form_state['uc_credit']['update_cc_encrypt_dir'] === TRUE) {
  301. $key_path = $form_state['values']['uc_credit_encryption_path'];
  302. $key_file = $key_path . '/' . UC_CREDIT_KEYFILE_NAME;
  303. if (!file_exists($key_file)) {
  304. if (!$file = fopen($key_file, 'wb')) {
  305. drupal_set_message(t('Credit card encryption key file creation failed for file @file. Check your filepath settings and directory permissions.', array('@file' => $key_file)), 'error');
  306. watchdog('uc_credit', 'Credit card encryption key file creation failed for file @file. Check your filepath settings and directory permissions.', array('@file' => $key_file), WATCHDOG_ERROR);
  307. }
  308. else {
  309. // Replacement key generation suggested by Barry Jaspan
  310. // for increased security.
  311. fwrite($file, md5(drupal_get_token(serialize($_REQUEST) . serialize($_SERVER) . REQUEST_TIME)));
  312. fclose($file);
  313. drupal_set_message(t('Credit card encryption key file generated. Card data will now be encrypted.'));
  314. watchdog('uc_credit', 'Credit card encryption key file generated. Card data will now be encrypted.');
  315. }
  316. }
  317. }
  318. }
  319. /**
  320. * Displays the credit card terminal page.
  321. */
  322. function uc_credit_terminal($order) {
  323. $build['details']['order_total'] = array('#markup' => '<div><strong>' . t('Order total: @total', array('@total' => uc_currency_format($order->order_total))) . '</strong></div>');
  324. $build['details']['balance'] = array('#markup' => '<div><strong>' . t('Balance: @balance', array('@balance' => uc_currency_format(uc_payment_balance($order)))) . '</strong></div>');
  325. $build['form'] = drupal_get_form('uc_credit_terminal_form', $order);
  326. return $build;
  327. }
  328. /**
  329. * Displays the credit card terminal form for administrators.
  330. *
  331. * @see uc_credit_terminal_form_validate()
  332. * @see uc_credit_terminal_form_submit()
  333. */
  334. function uc_credit_terminal_form($form, &$form_state, $order, $lock_amount = FALSE) {
  335. // Get the transaction types available to our default gateway.
  336. $types = uc_credit_gateway_txn_types(uc_credit_default_gateway());
  337. // Put the order ID in the form.
  338. $form['order_id'] = array(
  339. '#type' => 'hidden',
  340. '#value' => $order->order_id,
  341. );
  342. $balance = uc_payment_balance($order);
  343. // Let the administrator set the amount to charge.
  344. $form['amount'] = array(
  345. '#type' => 'uc_price',
  346. '#title' => t('Charge Amount'),
  347. '#default_value' => $balance > 0 ? uc_currency_format($balance, FALSE, FALSE, '.') : 0,
  348. '#disabled' => $lock_amount,
  349. );
  350. // Build a credit card form.
  351. $form['specify_card'] = array(
  352. '#type' => 'fieldset',
  353. '#title' => t('Credit card details'),
  354. '#description' => t('Use the available buttons in this fieldset to process with the specified card details.'),
  355. );
  356. $form['specify_card']['cc_data'] = array(
  357. '#tree' => TRUE,
  358. '#prefix' => '<div class="payment-details-credit clearfix">',
  359. '#suffix' => '</div>',
  360. );
  361. $form['specify_card']['cc_data'] += uc_payment_method_credit_form(array(), $form_state, $order);
  362. unset($form['specify_card']['cc_data']['cc_policy']);
  363. $form['specify_card']['actions'] = array('#type' => 'actions');
  364. // If available, let the card be charged now.
  365. if (in_array(UC_CREDIT_AUTH_CAPTURE, $types)) {
  366. $form['specify_card']['actions']['charge_card'] = array(
  367. '#type' => 'submit',
  368. '#value' => t('Charge amount'),
  369. );
  370. }
  371. // If available, let the amount be authorized.
  372. if (in_array(UC_CREDIT_AUTH_ONLY, $types)) {
  373. $form['specify_card']['actions']['authorize_card'] = array(
  374. '#type' => 'submit',
  375. '#value' => t('Authorize amount only'),
  376. );
  377. }
  378. // If available, create a reference at the gateway.
  379. if (in_array(UC_CREDIT_REFERENCE_SET, $types)) {
  380. $form['specify_card']['actions']['reference_set'] = array(
  381. '#type' => 'submit',
  382. '#value' => t('Set a reference only'),
  383. );
  384. }
  385. // If available, create a reference at the gateway.
  386. if (in_array(UC_CREDIT_CREDIT, $types)) {
  387. $form['specify_card']['actions']['credit_card'] = array(
  388. '#type' => 'submit',
  389. '#value' => t('Credit amount to this card'),
  390. );
  391. }
  392. // Find any uncaptured authorizations.
  393. $options = array();
  394. if (isset($order->data['cc_txns']['authorizations'])) {
  395. foreach ($order->data['cc_txns']['authorizations'] as $auth_id => $data) {
  396. if (empty($data['captured'])) {
  397. $options[$auth_id] = t('@auth_id - @date - @amount authorized', array('@auth_id' => strtoupper($auth_id), '@date' => format_date($data['authorized'], 'short'), '@amount' => uc_currency_format($data['amount'])));
  398. }
  399. }
  400. }
  401. // If any authorizations existed...
  402. if (!empty($options)) {
  403. // Display a fieldset with the authorizations and available action buttons.
  404. $form['authorizations'] = array(
  405. '#type' => 'fieldset',
  406. '#title' => t('Prior authorizations'),
  407. '#description' => t('Use the available buttons in this fieldset to select and act on a prior authorization. The charge amount specified above will be captured against the authorization listed below. Only one capture is possible per authorization, and a capture for more than the amount of the authorization may result in additional fees to you.'),
  408. );
  409. $form['authorizations']['select_auth'] = array(
  410. '#type' => 'radios',
  411. '#title' => t('Select authorization'),
  412. '#options' => $options,
  413. );
  414. $form['authorizations']['actions'] = array('#type' => 'actions');
  415. // If available, capture a prior authorization.
  416. if (in_array(UC_CREDIT_PRIOR_AUTH_CAPTURE, $types)) {
  417. $form['authorizations']['actions']['auth_capture'] = array(
  418. '#type' => 'submit',
  419. '#value' => t('Capture amount to this authorization'),
  420. );
  421. }
  422. // If available, void a prior authorization.
  423. if (in_array(UC_CREDIT_VOID, $types)) {
  424. $form['authorizations']['actions']['auth_void'] = array(
  425. '#type' => 'submit',
  426. '#value' => t('Void authorization'),
  427. );
  428. }
  429. // Collapse this fieldset if no actions are available.
  430. if (!isset($form['authorizations']['actions']['auth_capture']) && !isset($form['authorizations']['actions']['auth_void'])) {
  431. $form['authorizations']['#collapsible'] = TRUE;
  432. $form['authorizations']['#collapsed'] = TRUE;
  433. }
  434. }
  435. // Find any uncaptured authorizations.
  436. $options = array();
  437. // Log a reference to the order for testing.
  438. // $order->data = uc_credit_log_reference($order->order_id, substr(md5(REQUEST_TIME), 0, 16), '4111111111111111');
  439. if (isset($order->data['cc_txns']['references'])) {
  440. foreach ($order->data['cc_txns']['references'] as $ref_id => $data) {
  441. $options[$ref_id] = t('@ref_id - @date - (Last 4) @card', array('@ref_id' => strtoupper($ref_id), '@date' => format_date($data['created'], 'short'), '@card' => $data['card']));
  442. }
  443. }
  444. // If any references existed...
  445. if (!empty($options)) {
  446. // Display a fieldset with the authorizations and available action buttons.
  447. $form['references'] = array(
  448. '#type' => 'fieldset',
  449. '#title' => t('Customer references'),
  450. '#description' => t('Use the available buttons in this fieldset to select and act on a customer reference.'),
  451. );
  452. $form['references']['select_ref'] = array(
  453. '#type' => 'radios',
  454. '#title' => t('Select references'),
  455. '#options' => $options,
  456. );
  457. $form['references']['actions'] = array('#type' => 'actions');
  458. // If available, capture a prior references.
  459. if (in_array(UC_CREDIT_REFERENCE_TXN, $types)) {
  460. $form['references']['actions']['ref_capture'] = array(
  461. '#type' => 'submit',
  462. '#value' => t('Charge amount to this reference'),
  463. );
  464. }
  465. // If available, remove a previously stored reference.
  466. if (in_array(UC_CREDIT_REFERENCE_REMOVE, $types)) {
  467. $form['references']['actions']['ref_remove'] = array(
  468. '#type' => 'submit',
  469. '#value' => t('Remove reference'),
  470. );
  471. }
  472. // If available, remove a previously stored reference.
  473. if (in_array(UC_CREDIT_REFERENCE_CREDIT, $types)) {
  474. $form['references']['actions']['ref_credit'] = array(
  475. '#type' => 'submit',
  476. '#value' => t('Credit amount to this reference'),
  477. );
  478. }
  479. // Collapse this fieldset if no actions are available.
  480. if (!isset($form['references']['actions']['ref_capture']) && !isset($form['references']['actions']['ref_remove']) && !isset($form['references']['actions']['ref_credit'])) {
  481. $form['references']['#collapsible'] = TRUE;
  482. $form['references']['#collapsed'] = TRUE;
  483. }
  484. }
  485. $form['#attached']['css'][] = drupal_get_path('module', 'uc_payment') . '/uc_payment.css';
  486. return $form;
  487. }
  488. /**
  489. * Validation handler for credit terminal form.
  490. *
  491. * @see uc_credit_terminal_form()
  492. * @see uc_credit_terminal_form_submit()
  493. */
  494. function uc_credit_terminal_form_validate($form, &$form_state) {
  495. if (uc_order_load($form_state['values']['order_id']) === FALSE) {
  496. form_set_error('', t('Invalid order ID. Unable to process payment.'));
  497. }
  498. }
  499. /**
  500. * Submit handler for credit terminal form.
  501. *
  502. * @see uc_credit_terminal_form()
  503. * @see uc_credit_terminal_form_validate()
  504. */
  505. function uc_credit_terminal_form_submit($form, &$form_state) {
  506. // Load the order.
  507. $order = uc_order_load($form_state['values']['order_id']);
  508. // Get the data from the form and replace masked data from the order.
  509. $cc_data = $form_state['values']['cc_data'];
  510. if (strpos($cc_data['cc_number'], t('(Last 4) ')) === 0) {
  511. $cc_data['cc_number'] = $order->payment_details['cc_number'];
  512. }
  513. if (isset($cc_data['cc_cvv']) && isset($order->payment_details['cc_cvv'])) {
  514. if ($cc_data['cc_cvv'] == str_repeat('-', strlen($cc_data['cc_cvv']))) {
  515. $cc_data['cc_cvv'] = $order->payment_details['cc_cvv'];
  516. }
  517. }
  518. // Cache the values for use during processing.
  519. uc_credit_cache('save', $cc_data, FALSE);
  520. // Build the data array passed on to the payment gateway.
  521. $data = array();
  522. switch ($form_state['values']['op']) {
  523. case t('Charge amount'):
  524. $data['txn_type'] = UC_CREDIT_AUTH_CAPTURE;
  525. break;
  526. case t('Authorize amount only'):
  527. $data['txn_type'] = UC_CREDIT_AUTH_ONLY;
  528. break;
  529. case t('Set a reference only'):
  530. $data['txn_type'] = UC_CREDIT_REFERENCE_SET;
  531. break;
  532. case t('Credit amount to this card'):
  533. $data['txn_type'] = UC_CREDIT_CREDIT;
  534. break;
  535. case t('Capture amount to this authorization'):
  536. $data['txn_type'] = UC_CREDIT_PRIOR_AUTH_CAPTURE;
  537. $data['auth_id'] = $form_state['values']['select_auth'];
  538. break;
  539. case t('Void authorization'):
  540. $data['txn_type'] = UC_CREDIT_VOID;
  541. $data['auth_id'] = $form_state['values']['select_auth'];
  542. break;
  543. case t('Charge amount to this reference'):
  544. $data['txn_type'] = UC_CREDIT_REFERENCE_TXN;
  545. $data['ref_id'] = $form_state['values']['select_ref'];
  546. break;
  547. case t('Remove reference'):
  548. $data['txn_type'] = UC_CREDIT_REFERENCE_REMOVE;
  549. $data['ref_id'] = $form_state['values']['select_ref'];
  550. break;
  551. case t('Credit amount to this reference'):
  552. $data['txn_type'] = UC_CREDIT_REFERENCE_CREDIT;
  553. $data['ref_id'] = $form_state['values']['select_ref'];
  554. }
  555. $result = uc_payment_process_payment('credit', $form_state['values']['order_id'], $form_state['values']['amount'], $data, TRUE, NULL, FALSE);
  556. _uc_credit_save_cc_data_to_order(uc_credit_cache('load'), $form_state['values']['order_id']);
  557. if ($result) {
  558. drupal_set_message(t('The credit card was processed successfully. See the admin comments for more details.'));
  559. }
  560. else {
  561. drupal_set_message(t('There was an error processing the credit card. See the admin comments for details.'), 'error');
  562. }
  563. $form_state['redirect'] = 'admin/store/orders/' . $form_state['values']['order_id'];
  564. }