uc_2checkout.pages.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * @file
  4. * 2Checkout menu items.
  5. */
  6. /**
  7. * Finalizes 2Checkout transaction.
  8. */
  9. function uc_2checkout_complete($cart_id = 0) {
  10. watchdog('uc_2checkout', 'Receiving new order notification for order !order_id.', array('!order_id' => check_plain($_REQUEST['merchant_order_id'])));
  11. $order = uc_order_load($_REQUEST['merchant_order_id']);
  12. if ($order === FALSE) {
  13. return t('An error has occurred during payment. Please contact us to ensure your order has submitted.');
  14. }
  15. $key = $_REQUEST['key'];
  16. $order_number = variable_get('uc_2checkout_demo', TRUE) ? 1 : $_REQUEST['order_number'];
  17. $valid = md5(variable_get('uc_2checkout_secret_word', 'tango') . $_REQUEST['sid'] . $order_number . $_REQUEST['total']);
  18. if (drupal_strtolower($key) != drupal_strtolower($valid)) {
  19. uc_order_comment_save($order->order_id, 0, t('Attempted unverified 2Checkout completion for this order.'), 'admin');
  20. return MENU_ACCESS_DENIED;
  21. }
  22. if ($_REQUEST['demo'] == 'Y' xor variable_get('uc_2checkout_demo', TRUE)) {
  23. watchdog('uc_2checkout', 'The 2Checkout payment for order <a href="@order_url">@order_id</a> demo flag was set to %flag, but the module is set to %mode mode.', array(
  24. '@order_url' => url('admin/store/orders/' . $order->order_id),
  25. '@order_id' => $order->order_id,
  26. '%flag' => $_REQUEST['demo'] == 'Y' ? 'Y' : 'N',
  27. '%mode' => variable_get('uc_2checkout_demo', TRUE) ? 'Y' : 'N',
  28. ), WATCHDOG_ERROR);
  29. if (!variable_get('uc_2checkout_demo', TRUE)) {
  30. return MENU_ACCESS_DENIED;
  31. }
  32. }
  33. $order->billing_street1 = $_REQUEST['street_address'];
  34. $order->billing_street2 = $_REQUEST['street_address2'];
  35. $order->city = $_REQUEST['city'];
  36. $order->billing_postal_code = $_REQUEST['zip'];
  37. $order->billing_phone = $_REQUEST['phone'];
  38. $zone_id = db_query("SELECT zone_id FROM {uc_zones} WHERE zone_code LIKE :code", array(':code' => $_REQUEST['state']))->fetchField();
  39. if (!empty($zone_id)) {
  40. $order->billing_zone = $zone_id;
  41. }
  42. $country_id = db_query("SELECT country_id FROM {uc_countries} WHERE country_name LIKE :name", array(':name' => $_REQUEST['country']))->fetchField();
  43. if (!empty($country_id)) {
  44. $order->billing_country = $country_id;
  45. }
  46. // Save changes to order without it's completion.
  47. uc_order_save($order);
  48. if (drupal_strtolower($_REQUEST['email']) !== drupal_strtolower($order->primary_email)) {
  49. uc_order_comment_save($order->order_id, 0, t('Customer used a different e-mail address during payment: !email', array('!email' => check_plain($_REQUEST['email']))), 'admin');
  50. }
  51. if ($_REQUEST['credit_card_processed'] == 'Y' && is_numeric($_REQUEST['total'])) {
  52. $comment = t('Paid by !type, 2Checkout.com order #!order.', array('!type' => $_REQUEST['pay_method'] == 'CC' ? t('credit card') : t('echeck'), '!order' => check_plain($_REQUEST['order_number'])));
  53. uc_payment_enter($order->order_id, '2Checkout', $_REQUEST['total'], 0, NULL, $comment);
  54. }
  55. else {
  56. drupal_set_message(t('Your order will be processed as soon as your payment clears at 2Checkout.com.'));
  57. uc_order_comment_save($order->order_id, 0, t('!type payment is pending approval at 2Checkout.com.', array('!type' => $_REQUEST['pay_method'] == 'CC' ? t('Credit card') : t('eCheck'))), 'admin');
  58. }
  59. // Empty that cart...
  60. uc_cart_empty($cart_id);
  61. // Add a comment to let sales team know this came in through the site.
  62. uc_order_comment_save($order->order_id, 0, t('Order created through website.'), 'admin');
  63. $build = uc_cart_complete_sale($order, variable_get('uc_new_customer_login', FALSE));
  64. $page = variable_get('uc_cart_checkout_complete_page', '');
  65. if (!empty($page)) {
  66. drupal_goto($page);
  67. }
  68. return $build;
  69. }
  70. /**
  71. * React on status changes from 2CO.
  72. */
  73. function uc_2checkout_process_notification() {
  74. $values = $_POST;
  75. watchdog('uc_2checkout', 'Received 2Checkout notification with following data: !data', array('!data' => print_r($values, TRUE)));
  76. if (!empty($values['message_type']) && !empty($values['md5_hash']) && !empty($values['message_id'])) {
  77. // Validate the hash.
  78. $secret_word = variable_get('uc_2checkout_secret_word', 'tango');
  79. $sid = variable_get('uc_2checkout_sid', '');
  80. $twocheckout_order_id = $values['sale_id'];
  81. $twocheckout_invoice_id = $values['invoice_id'];
  82. $hash = strtoupper(md5($twocheckout_order_id . $sid . $twocheckout_invoice_id . $secret_word));
  83. if ($hash != $values['md5_hash']) {
  84. watchdog('uc_2checkout', '2CO notification #@num had a wrong hash.', array('@num' => $values['message_id']));
  85. die('Hash Incorrect');
  86. }
  87. $order_id = $values['vendor_order_id'];
  88. if ($values['message_type'] == 'FRAUD_STATUS_CHANGED') {
  89. switch ($values['fraud_status']) {
  90. case 'fail':
  91. uc_order_update_status($order_id, uc_order_state_default('canceled'));
  92. uc_order_comment_save($order_id, 0, t('Order have not passed 2Checkout fraud review.'));
  93. die('fraud');
  94. }
  95. }
  96. elseif ($values['message_type'] == 'REFUND_ISSUED') {
  97. uc_order_update_status($order_id, uc_order_state_default('canceled'));
  98. uc_order_comment_save($order_id, 0, t('Order have been refunded through 2Checkout.'));
  99. die('refund');
  100. }
  101. }
  102. die('ok');
  103. }