Beispiel #1
0
 static function getOrCreate($member)
 {
     $email = $member['email'];
     if ($buyer = Buyer::where('email', '=', $email)->first()) {
         return $buyer;
     } else {
         $buyer = new Buyer();
         $buyer->first_name = $member['fname'] ? $member['fname'] : 'First';
         $buyer->last_name = $member['lname'] ? $member['lname'] : 'Last';
         $buyer->email = $member['email'];
         $buyer->save();
         return $buyer;
     }
 }
 /**
  * Get Stripe purchase
  */
 private function _get_stripe_customer_id($email = NULL, $product_code = NULL)
 {
     $email = $email ? $email : Input::get('email');
     $product_code = $product_code ? $product_code : Input::get('code');
     // Get product
     $product = Product::where('code', '=', $product_code)->first();
     // Get buyer
     if (!($buyer = Buyer::where('email', '=', $email)->first())) {
         $this->_invalidRequest("Buyer account was not found");
     }
     // Get purchase
     if (!($purchase = Purchase::where('buyer_id', '=', $buyer->id)->where('product_id', '=', $product->id)->where('pay_method', '=', '1')->first())) {
         $this->_invalidRequest("No purchase was found, contact support.");
     }
     // Get Stripe Customer ID
     if (!$purchase->stripe_token) {
         $this->_invalidRequest("Payment processor's customer ID is missing, contact support.");
     }
     return $purchase->stripe_token;
 }
 /**
  * Stripe IPN
  */
 private function _ipn_stripe()
 {
     // Set your secret key: remember to change this to your live secret key in production
     // See your keys here https://manage.stripe.com/account
     // Add Stripe library
     require_once app_path() . "/libraries/stripe-php-1.9.0/lib/Stripe.php";
     // Add Stripe library
     Stripe::setApiKey(Config::get('project.stripe_secret_key'));
     // Retrieve the request's body and parse it as JSON
     $body = @file_get_contents('php://input');
     $event_json = json_decode($body);
     // For extra security, retrieve from the Stripe API
     try {
         $event_id = $event_json->id;
         $event_json = Stripe_Event::retrieve($event_id);
     } catch (Exception $e) {
         exit($e->getMessage());
     }
     // Do something with $event_json
     if (isset($event_json->type)) {
         // Customer and Affiliate
         // Get user_id
         $customer_id = !empty($event_json->data->object->customer) ? $event_json->data->object->customer : NULL;
         if ($customer_id) {
             try {
                 $customer = Stripe_Customer::retrieve($customer_id);
                 $email = $customer->email;
                 $dkData = $customer->metadata;
                 $buyer = Buyer::where('email', '=', $email)->first();
                 $affiliate_id = !empty($dkData['affiliate_id']) ? $dkData['affiliate_id'] : NULL;
                 // $buyer->affiliate_id
                 $first_name = !empty($dkData['first_name']) ? $dkData['first_name'] : NULL;
                 $last_name = !empty($dkData['last_name']) ? $dkData['last_name'] : NULL;
                 // Get Product Info
                 $product = Product::where('id', '=', $dkData['product_id'])->first();
             } catch (Exception $e) {
                 header('HTTP/1.1 400 Bad Request', true, 400);
                 exit("Not able to fetch customer");
             }
         } else {
             // No customer ID was found, stop the process here
             exit('Customer was not found in object');
         }
         // If No buyer was found
         if (empty($buyer)) {
             exit($event_json->type . ' : Buyer was not found');
         }
         // If No product was found
         if (empty($product)) {
             exit($event_json->type . ' : Product was not found');
         }
         // Create subscription
         if ($event_json->type == "customer.subscription.created") {
             $plan_code = $event_json->data->object->plan->id;
             // Remove word "_split" from it
             $plan_code = str_replace('_split', '', $plan_code);
             // Get Plan and Product
             $plan = Plan::where('stripe_id', '=', $plan_code)->first();
             // Push IPN to product IPN URL
             $ipn_data = array("type" => "sales", "password" => isset($dkData['password']) ? $dkData['password'] : NULL, "plan" => $plan->code, "amount" => $plan->price, "email" => $email, "first_name" => $first_name, "last_name" => $last_name);
             // Add an encrypted key to the request
             $ipn_data['key'] = $this->_generateHash($ipn_data, $product->api_key);
             $this->_push_ipn($product->ipn_url, $ipn_data);
         }
         // Successful Charge
         if ($event_json->type == "charge.succeeded") {
             // Delay 10 seconds, so purchase can be added to database
             sleep(10);
             $pay_id = $event_json->data->object->id;
             $paid_amount = $event_json->data->object->amount / 100;
             // Check if Pay ID already exist
             if (Transaction::where('pay_id', '=', $pay_id)->first()) {
                 echo "Transaction was already recorded.";
                 return;
             }
             $chargeMetadata = $event_json->data->object->metadata;
             if (empty($chargeMetadata->plan_id)) {
                 $plan_id = $dkData['plan_id'];
             } else {
                 $plan_id = !empty($chargeMetadata->plan_id) ? $chargeMetadata->plan_id : NULL;
             }
             // Get Plan and Product
             $plan = Plan::where('id', '=', $plan_id)->first();
             $purchase = Purchase::where('product_id', '=', $product->id)->where('buyer_id', '=', $buyer->id)->first();
             if (!$purchase) {
                 header('HTTP/1.1 400 Bad Request', true, 400);
                 echo "Purchase was not found";
                 // Delete InfusionSoft Invoice
                 //$this->_delete_infusion_invoice($invoice_id);
                 return;
             }
             // User all transactions
             $user_transactions = Transaction::where('purchase_id', '=', $purchase->id)->where('plan_id', '=', $plan->id)->get();
             // If Split payment installment is received
             if ($plan->has_split_pay) {
                 if (count($user_transactions) + 1 >= $plan->total_installments) {
                     // Cancel the subscription
                     $params['stripe_customer_id'] = $customer_id;
                     $params['plan_id'] = $plan->stripe_id . '_split';
                     Log::info('Stripe Split Not Cancelled', array('product' => $product->code, 'plan' => $plan->code));
                     $this->_cancelSubscription('Stripe', $params);
                 }
             }
             // Add payment in InfusionSoft
             if ($invoice_id = $this->_infusion_sales($product, $plan, $email, $first_name, $last_name, $affiliate_id, $paid_amount)) {
                 if (!$buyer->last_used_ip) {
                     $buyer = Buyer::where('id', '=', $buyer->id)->first();
                 }
                 // Record Sales Transaction
                 $transaction = new Transaction();
                 $transaction->purchase_id = $purchase->id;
                 $transaction->plan_id = $plan->id;
                 $transaction->amount = $paid_amount;
                 //$plan->price;
                 $transaction->invoice_id = $invoice_id;
                 $transaction->pay_id = $pay_id;
                 $transaction->pay_data = '';
                 //json_encode($event_json);
                 $transaction->buyer_ip = $buyer->last_used_ip;
                 $transaction->save();
                 // Do not generate license key if this is recurring charge
                 $license_key = NULL;
                 if (count($user_transactions) + 1 === 1) {
                     // Generate and Save License Key
                     $license_key = $this->_generate_license($product, $plan, $transaction->id);
                 }
                 // Email Receipt
                 $this->_send_email_receipt($product->name, $plan->name, $email, $pay_id, $paid_amount, $license_key);
             }
             // Push IPN to product IPN URL
             $ipn_data = array("type" => "sales", "password" => isset($dkData['password']) ? $dkData['password'] : NULL, "plan" => $plan->code, "pay_id" => $event_json->data->object->id, "amount" => $plan->price, "email" => $email, "first_name" => $first_name, "last_name" => $last_name);
             // Add an encrypted key to the request
             $ipn_data['key'] = $this->_generateHash($ipn_data, $product->api_key);
             $this->_push_ipn($product->ipn_url, $ipn_data);
         }
         // Update subscription
         if ($event_json->type == "customer.subscription.updated") {
             // $event_json->data->object->cancel_at_period_end
             $stripe_plan_code = $event_json->data->object->plan->id;
             // Remove word "_split" from it
             $stripe_plan_code = str_replace('_split', '', $stripe_plan_code);
             $plan = Plan::where('stripe_id', '=', $stripe_plan_code)->first();
             // Update Customer Metadata in Stripe
             try {
                 $metadata = $customer->metadata;
                 $metadata['plan_id'] = $plan->id;
                 $customer->metadata = $metadata;
                 $customer->save();
             } catch (Exception $e) {
                 header('HTTP/1.1 400 Bad Request', true, 400);
                 echo "Customer was not update";
                 return;
             }
             // Push to IPN
             $ipn_data = array("type" => "sub-update", "plan" => $plan->code, "email" => $buyer->email);
             // Add an encrypted key to the request
             $ipn_data['key'] = $this->_generateHash($ipn_data, $product->api_key);
             $this->_push_ipn($product->ipn_url, $ipn_data);
         }
         // Delete Subscription
         if ($event_json->type == "customer.subscription.deleted") {
             $stripe_plan_code = $event_json->data->object->plan->id;
             // Remove word "_split" from it
             $stripe_plan_code = str_replace('_split', '', $stripe_plan_code);
             $plan = Plan::where('stripe_id', '=', $stripe_plan_code)->first();
             // If Split payment installment is received
             if ($plan->has_split_pay) {
                 $purchase = Purchase::where('product_id', '=', $product->id)->where('buyer_id', '=', $buyer->id)->first();
                 $total_paid_installments = Transaction::where('purchase_id', '=', $purchase->id)->where('plan_id', '=', $plan->id)->get();
                 if (count($total_paid_installments) >= $plan->total_installments) {
                     // Do not push IPN, its fine user has paid all installments
                     Log::info('Stripe Split Cancelled', array('product' => $product->code, 'plan' => $plan->code));
                     return;
                 }
             }
             // Push to IPN
             $ipn_data = array("type" => "sub-cancel", "plan" => $plan->code, "email" => $buyer->email);
             // Add an encrypted key to the request
             $ipn_data['key'] = $this->_generateHash($ipn_data, $product->api_key);
             $this->_push_ipn($product->ipn_url, $ipn_data);
         }
         // Charge Failed
         if ($event_json->type == "charge.failed") {
             // Charge failed, ask customer to update card via email
             // @TODO: Ask Mark to enable some tries after failure
         }
         // Charge refunded
         if ($event_json->type == "charge.refunded") {
             // Check if transaction has not been refunded from UI, then go ahead
             // Else stop process
             $pay_id = $event_json->data->object->id;
             $transaction = Transaction::where('pay_id', '=', $pay_id)->first();
             if ($transaction->is_refunded) {
                 return;
             }
             // Push to IPN
             $ipn_data = array("type" => "refund", "plan" => $transaction->plan->code, "email" => $buyer->email);
             // Add an encrypted key to the request
             $ipn_data['key'] = $this->_generateHash($ipn_data, $product->api_key);
             $this->_push_ipn($product->ipn_url, $ipn_data);
         }
         if (isset($error)) {
             header('HTTP/1.1 400 Bad Request', true, 400);
             echo "Unsuccessful event";
             return;
         }
     }
 }
 /**
  * Check if user has already purchase the plan
  */
 private function _check_already_purchase($buyer_email, $product, $plan)
 {
     // Get Buyer
     $buyer = Buyer::where('email', '=', $buyer_email)->first();
     if ($buyer) {
         $purchase = Purchase::where('buyer_id', '=', $buyer->id)->first();
         if ($purchase) {
             if ($transaction = Transaction::where('purchase_id', '=', $purchase->id)->where('plan_id', '=', $plan->id)->where('is_refunded', '=', 0)->first()) {
                 // Redirect to next page
                 header("location: " . $plan->next_page_url);
                 exit;
             }
         }
     }
 }
Beispiel #5
0
 public static function check_phone($phone, $mobile1, $mobile2)
 {
     $em = Buyer::where('phone', 'like', $phone)->orWhere('mobile1', 'like', $mobile1)->orWhere('mobile2', 'like', $mobile2)->first();
     if ($em) {
         return $em;
     } else {
         return false;
     }
 }