/**
  * Activate services imediately after payment is received.
  * Do not to wait while cron activates services
  * 
  * @param Box_Event $event 
  */
 public static function onAfterAdminInvoicePaymentReceived(Box_Event $event)
 {
     $invoice = $event->getSubject();
     $repo = Box_Db::getTable('Model_Invoice');
     try {
         $repo->activate($invoice);
     } catch (Exception $e) {
         error_log($e->getMessage());
     }
 }
 /**
  * Method to install module
  *
  * @return bool
  */
 public function install()
 {
     // execute sql script if needed
     $pdo = Box_Db::getPdo();
     $query = "SELECT NOW()";
     $stmt = $pdo->prepare($query);
     $stmt->execute();
     //throw new Box_Exception("Throw exception to terminate module installation process with a message", array(), 123);
     return true;
 }
 public static function onBeforeClientCheckout(Box_Event $event)
 {
     $cart = $event->getSubject();
     $params = $event->getParameters();
     $ip = $params['ip'];
     $client = $params['client'];
     list($t, $domain) = explode('@', $client['email']);
     $rp = array();
     $rp['i'] = $ip;
     $rp['domain'] = $domain;
     $rp['city'] = $client['city'];
     $rp['region'] = $client['state'];
     $rp['postal'] = $client['postcode'];
     $rp['txnID'] = $client['id'];
     $rp['custphone'] = $client['phone_cc'] . $client['phone'];
     $rp['country'] = $client['country'];
     $rp['emailMD5'] = md5($client['email']);
     $rp['txn_type'] = 'paypal';
     /* payment gateway */
     $rp['license_key'] = '';
     // your MaxMind license key
     $rp['requested_type'] = 'standard';
     /* your request type preference */
     $fraudscore = 25;
     /* your riskScore preference */
     $pdo = Box_Db::getPdo();
     $q = "SELECT custom_9 FROM client WHERE id = :client_id LIMIT 1";
     $stmt = $pdo->prepare($q);
     $stmt->execute(array('client_id' => $client['id']));
     $fraudtest = $stmt->fetchColumn();
     $pdo = Box_Db::getPdo();
     $q = "SELECT custom_8 FROM client WHERE id = :client_id LIMIT 1";
     $stmt = $pdo->prepare($q);
     $stmt->execute(array('client_id' => $client['id']));
     $mmchecks = $stmt->fetchColumn();
     if ($fraudtest == '"' . 'city' . '"') {
         throw new Payment_Exception('The City that you have entered does not exists. Please check spelling. ');
     } else {
         if ($fraudtest == '"' . 'fraud' . '"') {
             throw new Payment_Exception('Your order was flagged as suspicious by MaxMind. ' . 'Please contact support.');
         } else {
             if ($fraudtest == '"' . 'freemail' . '"') {
                 throw new Payment_Exception('Orders using e-mails from a free e-mail providers are disabled. Please use another e-mail to place your order. ');
             } else {
                 if ($fraudtest == '"' . 'postcode' . '"') {
                     throw new Payment_Exception('The Zip/Postcode that you have entered does not exists. Please check spelling. ');
                 } else {
                     $url = 'https://minfraud2.maxmind.com/app/ccv2r?' . http_build_query($rp);
                     $content = file_get_contents($url);
                     // enable this to debug response to the screen when clicking checkout button
                     // throw new Exception(var_export($content, 1));
                     $result = array();
                     $keyvaluepairs = explode(";", $content);
                     $numkeyvaluepairs = count($keyvaluepairs);
                     for ($i = 0; $i < $numkeyvaluepairs; $i++) {
                         list($key, $value) = explode("=", $keyvaluepairs[$i]);
                         $result[$key] = $value;
                     }
                     // Do something with maxmind result.
                     // You can throw an Exception if detected that this cliet is a fraud
                     // In this example we simple save MaxMind result to client profile custom field 10.
                     $pdo = Box_Db::getPdo();
                     $q = "UPDATE client\n            SET custom_10 = :value\n            WHERE id = :client_id\n            LIMIT 1";
                     $stmt = $pdo->prepare($q);
                     $stmt->execute(array('client_id' => $client['id'], 'value' => json_encode($result)));
                     list($mm, $mmscore) = explode("=", $keyvaluepairs['43']);
                     $mmv[$mm] = $mmscore;
                     list($mmc, $mmcity) = explode("=", $keyvaluepairs['7']);
                     $mmct[$mmc] = $mmcity;
                     list($emmc, $mmmail) = explode("=", $keyvaluepairs['3']);
                     $emmct[$emmc] = $mmmail;
                     if ($mmcity == 'CITY_NOT_FOUND') {
                         $tt = 'city';
                         $pdo = Box_Db::getPdo();
                         $q = "UPDATE client\n            SET custom_9 = :value\n            WHERE id = :client_id\n            LIMIT 1";
                         $stmt = $pdo->prepare($q);
                         $stmt->execute(array('client_id' => $client['id'], 'value' => json_encode($tt)));
                         throw new Payment_Exception('The City that you have entered does not exists. Please check spelling. ');
                     } else {
                         if ($mmcity == 'POSTAL_CODE_NOT_FOUND') {
                             $tt = 'postcode';
                             $pdo = Box_Db::getPdo();
                             $q = "UPDATE client\n            SET custom_9 = :value\n            WHERE id = :client_id\n            LIMIT 1";
                             $stmt = $pdo->prepare($q);
                             $stmt->execute(array('client_id' => $client['id'], 'value' => json_encode($tt)));
                             throw new Payment_Exception('The Zip/Postcode that you have entered does not exists. Please check spelling. ');
                         } else {
                             if ($mmscore >= $fraudscore) {
                                 $tt = 'fraud';
                                 $pdo = Box_Db::getPdo();
                                 $q = "UPDATE client\n            SET custom_9 = :value\n            WHERE id = :client_id\n            LIMIT 1";
                                 $stmt = $pdo->prepare($q);
                                 $stmt->execute(array('client_id' => $client['id'], 'value' => json_encode($tt)));
                                 throw new Payment_Exception('Your order was flagged as suspicious by MaxMind. ' . 'Please contact support.');
                             }
                         }
                     }
                 }
             }
         }
     }
 }