PageRenderTime 35ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/system/application/controllers/payment.php

https://github.com/shivaas/TMWL
PHP | 319 lines | 205 code | 50 blank | 64 comment | 23 complexity | 303f8ff7d2e5c2c9e7168dff1263f9f9 MD5 | raw file
  1. <?php
  2. class Payment extends Controller {
  3. // If set to true SANDBOX is being used, else FALSE live credentials
  4. var $sandbox = false;
  5. var $domain = "http://tomamawithlove.org";
  6. /**
  7. * Method to process new payment request.
  8. * @author shivaas
  9. */
  10. function new_payment(){
  11. // Create new PayPal object
  12. $paypal_config = array('Sandbox' => $this->sandbox);
  13. $paypal = new PayPal($paypal_config);
  14. $this->output->enable_profiler(TRUE);
  15. ####[ SESSION AND CONSTANT VARIABLES ]############################
  16. $amount = $this->input->post('amount');
  17. if($this->input->post('give_epic')){
  18. $amount = $amount + $amount*0.10;
  19. }
  20. // echo $amount;
  21. // return;
  22. $session_data = array(
  23. 'amount' => '',
  24. 'item_name' => '',
  25. 'currency' =>'',
  26. 'postcard_id' =>'',
  27. 'payment_source' =>'',
  28. 'invoice' =>'',
  29. 'personname' =>'',
  30. 'email' => ''
  31. );
  32. $this->session->unset_userdata($session_data);
  33. $postcard_id = $this->input->post('postcard_id');
  34. $session_data = array(
  35. 'amount' => $amount,
  36. 'item_name' => $this->input->post('item_name'),
  37. 'currency' =>$this->input->post('currencyCodeType'),
  38. 'postcard_id' =>$postcard_id,
  39. 'payment_source' =>$this->input->post('payment_type'),
  40. 'invoice' => $postcard_id . '-' . rand(99999,9999999),
  41. 'personname' =>'',
  42. 'email' => ''
  43. );
  44. $this->session->set_userdata($session_data);
  45. /*
  46. $this->session->set_userdata('amount', $amount);
  47. $this->session->set_userdata('item_name', $this->input->post('item_name'));
  48. $this->session->set_userdata('currency', $this->input->post('currencyCodeType'));
  49. $this->session->set_userdata('personname','');
  50. $this->session->set_userdata('email', '');
  51. $this->session->set_userdata('postcard_id', $this->input->post('postcard_id'));
  52. $this->session->set_userdata('payment_source', $this->input->post('payment_type'));
  53. $this->session->set_userdata('invoice', $this->input->post('postcard_id') .'-' . rand());
  54. */
  55. ####[ SET EXPRESS CHECKOUT SETTINGS ]#############################
  56. $SECFields = array(
  57. 'returnurl' => site_url() . 'payment/complete/'. $postcard_id, // Required. URL to which the customer will be returned after returning from PayPal. 2048 char max.
  58. 'cancelurl' => site_url(), // Required. URL to which the customer will be returned if they cancel payment on PayPal's site
  59. 'noshipping' => '0', // The value 1 indiciates that on the PayPal pages, no shipping address fields should be displayed. Maybe 1 or 0.
  60. 'allownote' => '1', // The value 1 indiciates that the customer may enter a note to the merchant on the PayPal page during checkout. Must be 1 or 0
  61. 'hdrimg' => '', // URL for the image displayed as the header during checkout. Max size of 750x90. Should be stored on an https:// server
  62. 'skipdetails' => '1', // It's used to specify whether you want to skip the GetExpressCheckoutDetails part of checkout or not. See PayPal docs for more info.
  63. 'paymentaction' => 'Sale', // How you want to obtain payment. Sale, Authorization, Order
  64. 'pagestyle' => 'Epic_Thanks'
  65. );
  66. if(isset($_POST['pay_option'])){
  67. if( $_POST['pay_option'] == 2)
  68. {
  69. // for credit card payments
  70. $SECFields['landingpage'] = 'Billing';
  71. $SECFields['solutiontype'] = 'Sole';
  72. }else {
  73. // for paying via paypal account
  74. $SECFields['landingpage'] = 'Login';
  75. }
  76. }
  77. $OrderItems = array();
  78. $Item = array(
  79. 'l_name' => $this->session->userdata('item_name'), // Item Name. 127 char max.
  80. 'l_amt' => $this->session->userdata('amount'), // Cost of individual item.
  81. 'l_number' => $this->session->userdata('postcard_id'), // Item Number. 127 char max.
  82. 'l_qty' => '1', // Item quantity. Must be any positive integer.
  83. 'l_itemweightvalue' => '', // The weight value of the item.
  84. 'l_itemweightunit' => '' // The weight unit of the item.
  85. );
  86. array_push($OrderItems, $Item);
  87. $PaymentDetails = array(
  88. 'amt' => $this->session->userdata('amount'), // Required. Total amount of the order, including shipping, handling, and tax.
  89. 'itemamt' => '',
  90. 'maxamt' => $this->session->userdata('amount')+100,
  91. 'shippingamt' => '',
  92. 'currencycode' => $this->session->userdata('currency'), // A three-character currency code. Default is USD.
  93. 'desc' => 'Epic Thanks Web Order', // Description of items on the order. 127 char max.
  94. 'invnum' => strtotime(time()), // Your own invoice or tracking number. 127 char max.
  95. );
  96. $SECData = array(
  97. 'SECFields' => $SECFields,
  98. 'PaymentDetails' => $PaymentDetails,
  99. 'OrderItems' => $OrderItems
  100. );
  101. ####[ CALL SET EXPRESS CHECKOUT ]################################
  102. $this->session->set_userdata('PayPalResult',$paypal -> SetExpressCheckout($SECData));
  103. $paypalresult = $this->session->userdata('PayPalResult');
  104. //var_dump($paypalresult);
  105. $this->session->set_userdata('paypal_errors', $paypalresult['ERRORS']);
  106. $this->session->set_userdata('Token',isset($paypalresult['TOKEN']) ? $paypalresult['TOKEN'] : '');
  107. ####[ REDIRECT DEPENDING ON RESPONSE ]###########################
  108. #echo "<pre>";
  109. #print_r($_SESSION);
  110. #exit;
  111. if(strtolower($paypalresult['ACK']) != 'success')
  112. {
  113. //header('Location: error.php');
  114. redirect('payment/error?type=1&ack=' . $paypalresult['ACK']);
  115. exit();
  116. }
  117. else
  118. {
  119. header('Location: ' . $paypalresult['REDIRECTURL']);
  120. exit();
  121. }
  122. }
  123. function error(){
  124. // Create new PayPal object
  125. $paypal_config = array('Sandbox' => $this->sandbox);
  126. $paypal = new PayPal($paypal_config);
  127. echo $paypal->DisplayErrors($this->session->userdata('paypal_errors'));
  128. }
  129. function complete($postcard_id = null){
  130. // Create new PayPal object
  131. $paypal_config = array('Sandbox' => $this->sandbox);
  132. $paypal = new PayPal($paypal_config);
  133. #####[ CALL GET EXPRESS CHECKOUT DETAILS ]###########################
  134. #echo "<pre>";
  135. #print_r($_SESSION);
  136. #echo "</pre>";
  137. #exit;
  138. $GECDResult = $paypal -> GetExpressCheckoutDetails($this->session->userdata('Token'));
  139. $this->session->set_userdata('paypal_errors', $GECDResult['ERRORS']);
  140. if(strtolower($GECDResult['ACK']) != 'success' && strtolower($GECDResult['ACK']) != 'successwithwarning')
  141. {
  142. redirect('payment/error');
  143. exit();
  144. }
  145. #####[ SET EXPRESS CHECKOUT ]#######################################
  146. // DoExpressCheckout
  147. $DECPFields = array(
  148. 'token' => $this->session->userdata('Token'), // Required. A timestamped token, the value of which was returned by a previous SetExpressCheckout call.
  149. 'paymentaction' => 'Sale', // Required. How you want to obtain payment. Values can be: Authorization, Order, Sale. Auth indiciates that the payment is a basic auth subject to settlement with Auth and Capture. Order indiciates that this payment is an order auth subject to settlement with Auth & Capture. Sale indiciates that this is a final sale for which you are requesting payment.
  150. 'payerid' => isset($GECDResult['PAYERID']) ? $GECDResult['PAYERID'] : '', // Required. Unique PayPal customer id of the payer. Returned by GetExpressCheckoutDetails, or if you used SKIPDETAILS it's returned in the URL back to your RETURNURL.
  151. 'payerid' => isset($GECDResult['PAYERID']) ? $GECDResult['PAYERID'] : '',
  152. 'returnfmfdetails' => '1' // Flag to indiciate whether you want the results returned by Fraud Management Filters or not. 1 or 0.
  153. );
  154. $PaymentDetails = array(
  155. 'amt' => $this->session->userdata('amount'), // Required. Total amount of the order, including shipping, handling, and tax.
  156. 'currencycode' => $this->session->userdata('currency'), // A three-character currency code. Default is USD.
  157. 'itemamt' => '', // Required if you specify itemized L_AMT fields. Sum of cost of all items in this order.
  158. 'shippingamt' => '', // Total shipping costs for this order. If you specify SHIPPINGAMT you mut also specify a value for ITEMAMT.
  159. 'handlingamt' => '', // Total handling costs for this order. If you specify HANDLINGAMT you mut also specify a value for ITEMAMT.
  160. 'taxamt' => '', // Required if you specify itemized L_TAXAMT fields. Sum of all tax items in this order.
  161. 'desc' => 'Epic Thanks, 501(c)(3) Epic Change', // Description of items on the order. 127 char max.
  162. 'custom' => $this->session->userdata('postcard_id'), // Free-form field for your own use. 256 char max.
  163. 'invnum' => $this->session->userdata('invoice'), // Your own invoice or tracking number. 127 char max.
  164. //'notifyurl' => site_url() ."/payment/ipn" // URL for receiving Instant Payment Notifications
  165. );
  166. $OrderItems = array();
  167. $Item = array(
  168. 'l_name' => $this->session->userdata('item_name'), // Item name. 127 char max.
  169. 'l_amt' => $this->session->userdata('amount'), // Cost of item.
  170. 'l_number' => $this->session->userdata('postcard_id'), // Item number. 127 char max.
  171. 'l_qty' => '1' // Item qty on order. Any positive integer.
  172. );
  173. array_push($OrderItems, $Item);
  174. $DECPData = array(
  175. 'DECPFields' => $DECPFields,
  176. 'PaymentDetails' => $PaymentDetails,
  177. 'OrderItems' => $OrderItems
  178. );
  179. #####[ CALL DO EXPRESS CHECKOUT PAYMENT ]#############################
  180. $DECPResult = $paypal -> DoExpressCheckoutPayment($DECPData);
  181. $this->session->set_userdata('paypal_errors', $DECPResult['ERRORS']);
  182. #####[ REDIRECT DEPENDING ON RESPONSE ]###############################
  183. if(strtolower($DECPResult['ACK']) != 'success' && strtolower($DECPResult['ACK']) != 'successwithwarning')
  184. {
  185. redirect('payment/error?type=2');
  186. exit();
  187. }
  188. /*
  189. $_SESSION['PayerEmailAddress'] = isset($GECDResult['EMAIL']) ? $GECDResult['EMAIL'] : '';
  190. $_SESSION['FirstName'] = isset($GECDResult['FIRSTNAME']) ? $GECDResult['FIRSTNAME'] : '';
  191. $_SESSION['LastName'] = isset($GECDResult['LASTNAME']) ? $GECDResult['LASTNAME'] : '';
  192. $_SESSION['Street'] = isset($GECDResult['SHIPTOSTREET']) ? $GECDResult['SHIPTOSTREET'] : '';
  193. $_SESSION['City'] = isset($GECDResult['SHIPTOCITY']) ? $GECDResult['SHIPTOCITY'] : '';
  194. $_SESSION['State'] = isset($GECDResult['SHIPTOSTATE']) ? $GECDResult['SHIPTOSTATE'] : '';
  195. $_SESSION['Zip'] = isset($GECDResult['SHIPTOZIP']) ? $GECDResult['SHIPTOZIP'] : '';
  196. $_SESSION['Country'] = isset($GECDResult['SHIPTOCOUNTRYNAME']) ? $GECDResult['SHIPTOCOUNTRYNAME'] : '';
  197. $_SESSION['transaction_id'] = isset($DECPResult['TRANSACTIONID']) ? $DECPResult['TRANSACTIONID'] : '';
  198. $_SESSION['CustomerNotes'] = isset($DECPResult['NOTE']) ? $DECPResult['NOTE'] : '';
  199. $_SESSION['PaymentStatus'] = isset($DECPResult['PAYMENTSTATUS']) ? $DECPResult['PAYMENTSTATUS'] : '';
  200. $_SESSION['PendingReason'] = isset($DECPResult['PENDINGREASON']) ? $DECPResult['PENDINGREASON'] : '';
  201. $_SESSION['payment_type'] = isset($DECPResult['PAYMENTTYPE']) ? $DECPResult['PAYMENTTYPE'] : '';
  202. */
  203. #echo "<pre>";
  204. #print_r($_SESSION);
  205. #echo "</pre>";
  206. #exit;
  207. // Everything went fine, so redirect to completed page.
  208. $type = $this->session->userdata('payment_source');
  209. $d = new Donations();
  210. $d->donation_amount = $this->session->userdata('amount');
  211. $d->type = $type;
  212. $d->save();
  213. $session_data = array(
  214. 'donor_name' => $GECDResult['FIRSTNAME'] . ' ' . $GECDResult['LASTNAME'],
  215. 'donation_id' => $d->donation_id
  216. );
  217. $this->session->set_userdata($session_data);
  218. if( $type == 'postcard'){
  219. $r = new PostDonationRel();
  220. $r->post_id = $postcard_id;
  221. $r->donation_id = $d->donation_id;
  222. $r->save();
  223. if($this->session->userdata('amount') >= PARADE_CUTOFF){
  224. $g = new GratitudeParade();
  225. $g->donation_id = $d->donation_id;
  226. $g->name = Users::user()->username;
  227. $g->image_url = Users::user()->profile_avatar;
  228. $g->url = 'http://www.' . Users::user()->oauth_provider . '.com/' . Users::user()->username;
  229. $g->save();
  230. $this->session->set_flashdata('added_to_parade', true);
  231. }
  232. $this->session->set_flashdata('added_to_parade', false);
  233. redirect('postcard/send/' . $postcard_id . '/'. md5($postcard_id));
  234. }
  235. else
  236. redirect('payment/thankyou');
  237. }
  238. function thankyou(){
  239. if(!$this->session->userdata('donation_id'))
  240. show_error('You are not allowed to access this page', 501);
  241. //$this->session->set_userdata('amount', PARADE_CUTOFF);
  242. if($this->input->get('type') && $this->session->userdata('amount') >= PARADE_CUTOFF){
  243. $data['added_to_parade'] = true;
  244. $d = new GratitudeParade();
  245. $d->donation_id = $this->session->userdata('donation_id');
  246. if(Users::user()){
  247. $d->name = Users::user()->username;
  248. $d->image_url = Users::user()->profile_avatar;
  249. $d->url = 'http://www.' . Users::user()->oauth_provider . '.com/' . Users::user()->username;
  250. }else
  251. {
  252. $d->name = 'Anonymous'; //$this->session->userdata('donor_name');
  253. $d->image_url = site_url() . 'images/postcards/avatar_placeholder.jpg';
  254. $this->session->set_flashdata('anon_donation',true);
  255. }
  256. $d->save();
  257. $session_data = array(
  258. 'donor_name' => '',
  259. 'donation_id' => ''
  260. );
  261. //$this->session->unset_userdata($session_data);
  262. }else if($this->session->userdata('amount') >= PARADE_CUTOFF){
  263. $data['added_to_parade'] = false;
  264. }
  265. $data['content'] = 'statics/thankyou';
  266. $this->load->view('template', $data);
  267. }
  268. }