/**
  * Processes the post request.
  * 
  * Request needs to contain: 
  * 
  */
 public function process_payment()
 {
     $debug = Config::get('sdv2.debug');
     $data = Input::all();
     if ($debug) {
         var_dump($data);
     }
     if ($debug) {
         echo "</br>";
     }
     //Get the payment provider and check if the provider can handle the currency
     $provider = SDPaymentProvider::find($data["provider_id"]);
     if ($debug) {
         echo $provider->currencies;
     }
     if ($debug) {
         echo "</br>";
     }
     $ava_curr = json_decode($provider->currencies);
     if ($debug) {
         var_dump($ava_curr);
     }
     if ($debug) {
         echo "</br>";
     }
     if ($ava_curr == false) {
         exit("Provider Currency JSON invalid");
     }
     if ($ava_curr->{$data}["currency"] != "true") {
         exit("Currency not supported by provider");
     }
     //query the items db to get the price of the plan
     if ($debug) {
         echo "Item-id:" . $data["item_id"] . "</br>";
     }
     $item = SDItem::find($data['item_id']);
     if ($debug) {
         var_dump($item);
     }
     if ($debug) {
         echo "</br>";
     }
     //get the price of the item in the selected currency
     $price_array = json_decode($item->price);
     $price = $price_array->{$data}["currency"];
     Log::info("price: " . $price);
     //Generate a transaction id and check if it exists
     $got_transaction_id = false;
     while ($got_transaction_id == false) {
         $transaction_id = $this->generate_transaction_id();
         if (!($check_id = SDPaymentTransaction::find($transaction_id))) {
             $got_transaction_id = true;
         } else {
             Log::info("transaction id " . $transaction_id . " already exists - genearating a new one");
         }
     }
     Log::info("transaction id: " . $transaction_id);
     //Check if a user with this mail adress exists or a user is logged in
     if ($user = Sentinel::check()) {
         Log::info("User logged in - UserID: " . $user->id);
     } else {
         Log::info("User not logged in - Redirected to the login page");
         redirect::to('/user/require_login');
     }
     //Generate the item json
     $items = array();
     $items[] = array("id" => $item->id, "count" => "1");
     $items = json_encode($items);
     //Check if a steamid is added to the users account
     //save the transaction to the transaction db
     $transaction = new SDPaymentTransaction();
     $transaction->id = $transaction_id;
     $transaction->user_id = $user->id;
     $transaction->payment_provider = $provider->id;
     $transaction->currency = $data["currency"];
     $transaction->price = $price;
     $transaction->items = $items;
     $transaction->status = "sent";
     $transaction->save();
     //Create the payment with the provider, the transaction code and the price
     $payment_provider = $provider->provider_class;
     Log::info("payment_provider_class:" . $payment_provider);
     $payment = new $payment_provider();
     $payment->initiate_payment($price, $transaction_id, $data["currency"]);
 }
 public function fire($job, $data)
 {
     $transaction_id = $data["transaction"];
     Log::info("Queue Worker Fired");
     Log::info("Transaction-ID: " . $transaction_id);
     //Get the transaction from the db
     $transaction = SDPaymentTransaction::find($transaction_id);
     if (!$transaction) {
         Log::warning("Could not load Transaction from Database");
         exit;
     }
     if ($transaction->status != "confirmed") {
         Log::error("Transaction" . $transaction->id . "not confirmed - aborting");
     }
     Log::info("Transaction loaded from DB");
     Log::info("Transaction user_id: " . $transaction->user_id);
     Log::info("Transaction payment provider: " . $transaction->payment_provider);
     Log::info("Transaction Items: " . $transaction->items);
     //Get the user from the DB
     $user = Sentinel::findById($transaction->user_id);
     if (!$user) {
         Log::error("Could not find the user " . $transaction->user_id . " in the DB");
         exit;
     }
     //Get the transaction items
     $trans_items = json_decode($transaction->items);
     if ($trans_items == false) {
         Log::error("Invalid Item JSON at the Transaction " . $transaction->id);
         exit;
     }
     //Get the User info from the DB
     $user_infos = DB::table('sd_user_infos')->where('user_id', $user->id)->get();
     if (!$user_infos) {
         Log::error("Could not get the User Infos from the db for transaction: " . $transaction->id);
         exit;
     }
     //Go through the items and assign them to the user
     foreach ($trans_items as $trans_item) {
         Log::info("Got Trans Item id:" . $trans_item->id);
         Log::info("Got Trans Item count:" . $trans_item->count);
         //Get the item info from the db
         $item = SDItem::find($trans_item->id);
         if (!$item) {
             Log::error("Could not get SD Item with id " . $trans_item->id . " from the DB");
             exit;
         }
         Log::info("Got SDItem: " . $item->id . " - " . $item->name_short);
         //Get the handlers for the item and call them
         $handlers = json_decode($item->handlers);
         if ($handlers == false) {
             Log::error("Invalid Handler JSON at Item: " . $item->id);
         }
         //Call the item multiple times according to the item count
         for ($i = 1; $i == $trans_item->count; $i++) {
             foreach ($handlers as $handler) {
                 Log::info("Got Handler Class: " . $handler->class);
                 $item_handler = new $handler->class();
                 $result = $item_handler->add_item($user, $user_infos, $handler->params);
                 if ($result == true) {
                     Log::info('Successfully executed ' . $handler->class . ' for ' . $transaction->id);
                     $sd_user_item = new SDUseritem();
                     $sd_user_item->user_id = $user->id;
                     $sd_user_item->item_id = $item->id;
                     $sd_user_item->save();
                 } else {
                     Log::error('Exection of ' . $handler->class . ' for ' . $transaction->id . ' failed');
                 }
             }
         }
     }
     //Update the transaciton
     $transaction->status = "completed";
     $transaction->save();
     //Check for errors
     $job->delete();
 }