Exemplo n.º 1
1
 /**
  * Record sales on InfusionSoft
  */
 private function _infusion_sales($product, $plan, $email, $first_name, $last_name, $affiliate_id, $paid_amount = NULL)
 {
     // Add or Get buyer from InfusionSoft
     require_once app_path() . "/libraries/infusionsoft/isdk.php";
     // Add InfusionSoft Library
     $isapp = new iSDK();
     // Create Connection
     if ($isapp->cfgCon("comissionTracker")) {
         // find contact by email
         $contacts = $isapp->findByEmail($email, array('Id', 'Email'));
         // If contact found
         if (!empty($contacts[0]['Id'])) {
             $contact_id = $contacts[0]['Id'];
         } else {
             // Create new contact
             $contactData = array('Email' => $email, 'FirstName' => $first_name, 'LastName' => $last_name);
             $contact_id = $isapp->addCon($contactData);
         }
         // Sets current date
         $currentDate = date("d-m-Y");
         $oDate = $isapp->infuDate($currentDate);
         // Creates blank order
         $newOrder = $isapp->blankOrder($contact_id, "{$product->name} - {$plan->name} ({$contact_id})", $oDate, NULL, $affiliate_id);
         // Add Order Item - Product ID
         // type = 4 or 9 (Product or Subscription)
         $infusion_product_type = $product->type == 1 ? 4 : 9;
         if ($paid_amount === NULL or $paid_amount === '') {
             $paid_amount = $plan->price;
         }
         $orderPrice = $paid_amount;
         //$paid_amount ? $paid_amount : $plan->price;
         $orderPrice = floatval(round($orderPrice, 2));
         $result = $isapp->addOrderItem($newOrder, $plan->infusion_id, $infusion_product_type, $orderPrice, 1, "Sales Made From DK Solution", "Generated Through API");
         // Add Manual Payment - since CC charged with Stripe
         $payment = $isapp->manualPmt($newOrder, $orderPrice, $oDate, "Credit Card", "Payment via DK Solution", false);
         //credit
         // Add Affiliate in our database
         if ($affiliate_id) {
             // Get Affiliate
             $affiliate = Affiliate::find($affiliate_id);
             if (!$affiliate or empty($affiliate->email)) {
                 $affData = $isapp->dsFind('Affiliate', 1, 0, 'Id', $affiliate_id, array('AffName', 'ContactId'));
             }
             if (!$affiliate) {
                 $affName = !empty($affData[0]['AffName']) ? $affData[0]['AffName'] : NULL;
                 // Save Affiliate name
                 $affiliate = new Affiliate();
                 $affiliate->id = $affiliate_id;
                 $affiliate->name = $affName;
                 $affiliate->save();
             }
             if ($affiliate and empty($affiliate->email)) {
                 $affContactId = !empty($affData[0]['ContactId']) ? $affData[0]['ContactId'] : NULL;
                 if ($affContactId) {
                     $affContactData = $isapp->dsFind('Contact', 1, 0, 'Id', $affContactId, array('Email'));
                     $affEmail = !empty($affContactData[0]['Email']) ? $affContactData[0]['Email'] : NULL;
                     $affiliate->email = $affEmail;
                     $affiliate->save();
                 }
             }
             // Send Commission Email to Affiliate
             if ($affiliate->email) {
                 // Get earned commission
                 $from = strtotime("midnight", time());
                 $to = strtotime("tomorrow", time()) - 1;
                 $start = date('Ymd\\TH:i:s', $from);
                 $finish = date('Ymd\\TH:i:s', $to);
                 $commissions = $isapp->affCommissions($affiliate->id, $start, $finish);
                 if (!empty($commissions) and is_array($commissions)) {
                     foreach ($commissions as $commission) {
                         if ($commission['InvoiceId'] == $newOrder) {
                             $AffEarnedCommission = $commission['AmtEarned'];
                         }
                     }
                 }
                 // Send email to Affiliate
                 if (!empty($AffEarnedCommission)) {
                     $this->_send_email_commission($product->name, $plan->name, $affiliate->email, $affiliate->name, $AffEarnedCommission);
                 }
             }
         }
         return $newOrder;
     } else {
         // Error
         // echo "Connection Failed";
         return FALSE;
     }
 }
 public function do_license_payment()
 {
     $testMode = Session::get('test_mode') === 'true';
     $rules = array('first_name' => 'required', 'last_name' => 'required', 'card_number' => 'required', 'expiration_month' => 'required', 'expiration_year' => 'required', 'cvv' => 'required', 'address1' => 'required', 'city' => 'required', 'state' => 'required', 'postal_code' => 'required');
     $validator = Validator::make(Input::all(), $rules);
     if ($validator->fails()) {
         return Redirect::to('license')->withErrors($validator);
     }
     $account = $this->accountRepo->getNinjaAccount();
     $account->load('account_gateways.gateway');
     $accountGateway = $account->account_gateways[0];
     try {
         if ($testMode) {
             $ref = 'TEST_MODE';
         } else {
             $gateway = self::createGateway($accountGateway);
             $details = self::getLicensePaymentDetails(Input::all());
             $response = $gateway->purchase($details)->send();
             $ref = $response->getTransactionReference();
             if (!$ref) {
                 Session::flash('error', $response->getMessage());
                 return Redirect::to('license')->withInput();
             }
             if (!$response->isSuccessful()) {
                 Session::flash('error', $response->getMessage());
                 Utils::logError($response->getMessage());
                 return Redirect::to('license')->withInput();
             }
         }
         $licenseKey = Utils::generateLicense();
         $license = new License();
         $license->first_name = Input::get('first_name');
         $license->last_name = Input::get('last_name');
         $license->email = Input::get('email');
         $license->transaction_reference = $ref;
         $license->license_key = $licenseKey;
         $license->affiliate_id = Session::get('affiliate_id');
         $license->save();
         $affiliate = Affiliate::find(Session::get('affiliate_id'));
         $data = ['message' => $affiliate->payment_subtitle, 'license' => $licenseKey, 'hideHeader' => true];
         return View::make('public.license', $data);
         //return Redirect::away(Session::get('return_url') . "?license_key={$license->license_key}");
     } catch (\Exception $e) {
         $errorMessage = trans('texts.payment_error');
         Session::flash('error', $errorMessage);
         Utils::logError($e->getMessage());
         return Redirect::to('license')->withInput();
     }
 }