示例#1
0
 /**
  * Post manual additions transacton form
  */
 static function addManually($params)
 {
     extract($params);
     $ipn_url = Config::get('project.paypal_ipn_url');
     $params = array('first_name' => Input::get('first_name'), 'last_name' => Input::get('last_name'), 'email' => Input::get('email'), 'password' => Input::get('password'), 'product_id' => Input::get('product_id'), 'plan_id' => Input::get('plan_id'), 'pay_id' => Input::get('pay_id'), 'stripe_token' => Input::get('stripe_token'), 'paypal_sub_id' => Input::get('paypal_sub_id'), 'amount' => Input::get('amount'), 'affiliate_id' => Input::get('affiliate_id'));
     $members[] = array('email' => $email, 'fname' => $first_name, 'lname' => $last_name);
     $data = array();
     if ($members) {
         foreach ($members as $member) {
             // Add or get buyer
             if ($buyer = Buyer::getOrCreate($member)) {
                 // Get Plan
                 $plan = Plan::find($plan_id);
                 if ($plan and !$plan->is_oto) {
                     // Add purchase for the buyer
                     $purchase = new Purchase();
                     $purchase->buyer_id = $buyer->id;
                     $purchase->product_id = $product_id;
                     $purchase->plan_id = $plan_id;
                     $purchase->stripe_token = $stripe_token ? $stripe_token : NULL;
                     $purchase->paypal_sub_id = $paypal_sub_id ? $paypal_sub_id : NULL;
                     $purchase->pay_method = 2;
                     $purchase->affiliate_id = $affiliate_id;
                     // If method is Stripe
                     if ($pay_id and DKHelpers::GetPayMethod($pay_id) == 'Stripe') {
                         $purchase->pay_method = 1;
                     }
                     $purchase->save();
                 }
                 // Push to PayPal IPN of DK
                 $ipn_data = array('plan_id' => $plan_id, 'product_id' => $product_id, 'email' => $buyer->email, 'first_name' => $buyer->first_name, 'last_name' => $buyer->last_name, 'password' => $password, 'transaction_id' => $pay_id ? $pay_id : 'MNL-' . time(), 'amount' => $amount, 'manual_transaction' => TRUE);
                 if ($password) {
                     $ipn_data['dk_new_user'] = TRUE;
                 } else {
                     $ipn_data['dk_new_charge'] = TRUE;
                     $ipn_data['buyer_id'] = $buyer->id;
                 }
                 // Add Curl library
                 require_once app_path() . "/libraries/Curl/Curl.php";
                 // Post data to IPN
                 $curl = new Curl();
                 $curl->simple_post($ipn_url, $ipn_data, array(CURLOPT_BUFFERSIZE => 10));
             }
         }
     }
     return TRUE;
 }
 /**
  * Cancel Subscription
  */
 public function getCancelSubscription($id, $at_period_end = FALSE)
 {
     // Page Title
     $this->_data['page_title'] = "Cancel Subscription";
     $transaction = Transaction::find($id);
     $this->_data['transaction'] = $transaction;
     $pay_id = $transaction->pay_id;
     $pay_method = DKHelpers::GetPayMethod($pay_id);
     // If Stripe
     if ($pay_method == 'Stripe') {
         $plan_id = $transaction->plan->stripe_id;
         $stripe_sub_id = NULL;
         $is_stripe_sub_active = 0;
         // 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'));
         try {
             $ch = Stripe_Charge::retrieve($pay_id);
             $subscriptions = Stripe_Customer::retrieve($ch->customer)->subscriptions->all(array('count' => 100));
             foreach ($subscriptions->data as $subscription) {
                 if ($subscription->plan->id == $plan_id) {
                     $stripe_sub_id = $subscription->id;
                     if ($subscription->status == 'active') {
                         $is_stripe_sub_active = 1;
                     }
                     break;
                 }
             }
             $this->_data['stripe_customer_id'] = $ch->customer;
         } catch (Exception $e) {
             Session::flash('alert_error', '<strong>Ooops!</strong> Subscription was not retreived, try again.');
             return Redirect::to("admin/transactions/detail/{$transaction->id}");
         }
     }
     // If PayPal
     if ($pay_method == 'PayPal') {
         $paypal_sub_id = NULL;
         $is_paypal_sub_active = 0;
         $purchase = $transaction->purchase;
         $paypal_sub_id = $purchase->paypal_sub_id;
         $config = array('mode' => Config::get('project.paypal_mode'), 'acct1.UserName' => Config::get('project.paypal_api_username'), 'acct1.Password' => Config::get('project.paypal_api_password'), 'acct1.Signature' => Config::get('project.paypal_api_signature'));
         /*
          * Obtain information about a recurring payments profile. 
          */
         $getRPPDetailsReqest = new GetRecurringPaymentsProfileDetailsRequestType();
         /*
          * (Required) Recurring payments profile ID returned in the CreateRecurringPaymentsProfile response. 19-character profile IDs are supported for compatibility with previous versions of the PayPal API.
          */
         $getRPPDetailsReqest->ProfileID = $paypal_sub_id;
         $getRPPDetailsReq = new GetRecurringPaymentsProfileDetailsReq();
         $getRPPDetailsReq->GetRecurringPaymentsProfileDetailsRequest = $getRPPDetailsReqest;
         /*
          * 	 ## Creating service wrapper object
         Creating service wrapper object to make API call and loading
         Configuration::getAcctAndConfig() returns array that contains credential and config parameters
         */
         $paypalService = new PayPalAPIInterfaceServiceService($config);
         try {
             /* wrap API method calls on the service object with a try catch */
             $getRPPDetailsResponse = $paypalService->GetRecurringPaymentsProfileDetails($getRPPDetailsReq);
         } catch (Exception $ex) {
             Session::flash('alert_error', '<strong>Ooops!</strong> Subscription was not retreived, try again.');
             return Redirect::to("admin/transactions/detail/{$transaction->id}");
         }
         if (isset($getRPPDetailsResponse) and $getRPPDetailsResponse->Ack == 'Success') {
             $status = $getRPPDetailsResponse->GetRecurringPaymentsProfileDetailsResponseDetails->ProfileStatus;
             if ($status == 'ActiveProfile') {
                 $is_paypal_sub_active = 1;
             }
         }
         //exit;
     }
     $this->_data['subscription_id'] = $pay_method == 'Stripe' ? $stripe_sub_id : $paypal_sub_id;
     $this->_data['is_sub_active'] = $pay_method == 'Stripe' ? $is_stripe_sub_active : $is_paypal_sub_active;
     $this->_data['at_period_end'] = $at_period_end;
     $this->_data['pay_method'] = $pay_method;
     return View::make('admin.transactions.cancel-subscription', $this->_data)->nest('header', 'admin.common.header', $this->_data)->nest('footer', 'admin.common.footer', $this->_data);
 }