Example #1
0
 public function pay($id = NULL)
 {
     if ($id == NULL) {
         flashmsg('You must select an invoice to make a payment', 'error');
         redirect('client/invoices');
     }
     $user = $this->data['user'] = $this->ion_auth->get_user(user_id());
     $gateways = $this->data['gateways'] = $this->core->get_gateways();
     $invoice = $this->data['invoice'] = $this->core->get_invoice($id);
     $settings = $this->data['settings'] = $this->settings->get_settings();
     if ($invoice->client_id != $user->id) {
         flashmsg('Invoice does not exist', 'error');
         redirect('client/invoices');
     }
     if (isset($_POST['submit'])) {
         if (!isset($_POST['gateway'])) {
             flashmsg('You must select a gateway to make a payment with', 'error');
             redirect('client/invoices/pay/' . $id);
         }
         if ($_POST['gateway'] == 'paypal') {
             if (intval(str_replace('$', '', $_POST['amount'])) > intval(str_replace('$', '', $invoice->amount_due))) {
                 flashmsg('You cannot make a payment for more than the amount due', 'error');
                 redirect('client/invoices/pay/' . $id);
             }
             $this->paypal_lib->add_field('business', $gateways['paypal']->login);
             $this->paypal_lib->add_field('return', site_url('client/invoices/success/paypal'));
             $this->paypal_lib->add_field('cancel_return', site_url('client/invoices/cancel/paypal'));
             $this->paypal_lib->add_field('notify_url', site_url('client/invoices/ipn/paypal'));
             $this->paypal_lib->add_field('item_name', $invoice->invoice_description);
             $this->paypal_lib->add_field('item_number', $invoice->invoice_id);
             $this->paypal_lib->add_field('amount', $_POST['amount']);
             $this->paypal_lib->button('Continue & Pay!');
             $this->data['paypal_form'] = $this->paypal_lib->paypal_form('paypal_form');
         } else {
             if ($_POST['gateway'] == 'stripe') {
                 require_once APPPATH . 'libraries/Stripe.php';
                 /* Testing config only, you can use your own if you want
                 				$config['stripe_key_test_public'] = 'pk_OyHpP2uvEQIInEC6ghAvIg9dexjne';
                 				$config['stripe_key_test_secret'] = 'xuRKxPH0GLEU6VwEeqI5L3VFiayQiiiA';
                 			*/
                 $config['stripe_key_live_public'] = $gateways['stripe']->auth2;
                 $config['stripe_key_live_secret'] = $gateways['stripe']->auth1;
                 $config['stripe_verify_ssl'] = true;
                 // this needs to be ran in an ssl environment
                 $config['stripe_test_mode'] = false;
                 // if you want to test the gateway, set true or false
                 $stripe = new Stripe($config);
                 if (isset($_POST['stripe_charge'])) {
                     $charge = $stripe->charge_card(intval(str_replace('$', '', $_POST['amount'])) * 100, array('number' => $_POST['number'], 'exp_month' => $_POST['exp_month'], 'exp_year' => $_POST['exp_year'], 'cvc' => $_POST['cvc'], 'name' => $user->first_name . ' ' . $user->last_name), $invoice->invoice_description);
                     $result = json_decode($charge);
                     if (!$result->error) {
                         $this->core->make_stripe_payment($invoice, $result, $_POST['amount']);
                         $pay_data['user'] = $user->username;
                         $pay_data['payment_amount'] = $_POST['amount'];
                         $pay_data['invoice_id'] = $invoice->invoice_id;
                         foreach ($this->core->get_admin_emails() as $email) {
                             $this->email->from($settings['company_email'], $settings['site_name']);
                             $this->email->to($email);
                             $this->email->subject('New Payment!');
                             $this->email->message($this->load->view('emails/new_payment', $pay_data, true));
                             $this->email->send();
                         }
                         flashmsg('Your payment of <b>' . $_POST['amount'] . '</b> to invoice #' . $invoice->invoice_id . ' has been successfully processed via Stripe', 'success');
                         redirect('client/invoices');
                     } else {
                         flashmsg($result->error->message, 'error');
                         redirect('client/invoices/pay/' . $id);
                     }
                 } else {
                     $this->data['stripe_form'] = true;
                 }
             }
         }
     }
     $this->data['meta_title'] = 'Make Payment on Invoice #' . $this->data['invoice']->invoice_id;
 }