PageRenderTime 30ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/modules/contrib/civicrm/ext/iatspayments/CRM/iATS/Form/IATSOneTimeCharge.php

https://gitlab.com/virtualrealms/d7civicrm
PHP | 253 lines | 181 code | 12 blank | 60 comment | 7 complexity | 871d614eb3e13ea9ce429eea1180bb47 MD5 | raw file
  1. <?php
  2. /**
  3. * @file
  4. */
  5. require_once 'CRM/Core/Form.php';
  6. /**
  7. * Form controller class.
  8. *
  9. * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
  10. * A form to generate new one-time charges on an existing recurring schedule.
  11. */
  12. class CRM_iATS_Form_IATSOneTimeCharge extends CRM_Core_Form {
  13. /**
  14. *
  15. */
  16. public function getFields() {
  17. $civicrm_fields = array(
  18. 'firstName' => 'billing_first_name',
  19. 'lastName' => 'billing_last_name',
  20. 'address' => 'street_address',
  21. 'city' => 'city',
  22. 'state' => 'state_province',
  23. 'zipCode' => 'postal_code',
  24. 'creditCardNum' => 'credit_card_number',
  25. 'creditCardExpiry' => 'credit_card_expiry',
  26. 'mop' => 'credit_card_type',
  27. );
  28. // When querying using CustomerLink.
  29. $iats_fields = array(
  30. // FLN.
  31. 'creditCardCustomerName' => 'CSTN',
  32. 'address' => 'ADD',
  33. 'city' => 'CTY',
  34. 'state' => 'ST',
  35. 'zipCode' => 'ZC',
  36. 'creditCardNum' => 'CCN',
  37. 'creditCardExpiry' => 'EXP',
  38. 'mop' => 'MP',
  39. );
  40. $labels = array(
  41. // 'firstName' => 'First Name',
  42. // 'lastName' => 'Last Name',.
  43. 'creditCardCustomerName' => 'Name on Card',
  44. 'address' => 'Street Address',
  45. 'city' => 'City',
  46. 'state' => 'State or Province',
  47. 'zipCode' => 'Postal Code or Zip Code',
  48. 'creditCardNum' => 'Credit Card Number',
  49. 'creditCardExpiry' => 'Credit Card Expiry Date',
  50. 'mop' => 'Credit Card Type',
  51. );
  52. return array($civicrm_fields, $iats_fields, $labels);
  53. }
  54. /**
  55. *
  56. */
  57. protected function getCustomerCodeDetail($params) {
  58. require_once "CRM/iATS/iATSService.php";
  59. $credentials = iATS_Service_Request::credentials($params['paymentProcessorId'], $params['is_test']);
  60. $iats_service_params = array('type' => 'customer', 'iats_domain' => $credentials['domain'], 'method' => 'get_customer_code_detail');
  61. $iats = new iATS_Service_Request($iats_service_params);
  62. // print_r($iats); die();
  63. $request = array('customerCode' => $params['customerCode']);
  64. // Make the soap request.
  65. $response = $iats->request($credentials, $request);
  66. // note: don't log this to the iats_response table.
  67. $customer = $iats->result($response, FALSE);
  68. // print_r($customer); die();
  69. if (empty($customer['ac1'])) {
  70. $alert = ts('Unable to retrieve card details from iATS.<br />%1', array(1 => $customer['AUTHORIZATIONRESULT']));
  71. throw new Exception($alert);
  72. }
  73. // This is a SimpleXMLElement Object.
  74. $ac1 = $customer['ac1'];
  75. $card = get_object_vars($ac1->CC);
  76. return $customer + $card;
  77. }
  78. /**
  79. *
  80. */
  81. protected function processCreditCardCustomer($values) {
  82. // Generate another (possibly) recurring contribution, matching our recurring template with submitted value.
  83. $is_recurrence = !empty($values['is_recurrence']);
  84. $total_amount = $values['amount'];
  85. $contribution_template = _iats_civicrm_getContributionTemplate(array('contribution_recur_id' => $values['crid']));
  86. $contact_id = $values['cid'];
  87. $hash = md5(uniqid(rand(), TRUE));
  88. $contribution_recur_id = $values['crid'];
  89. $payment_processor_id = $values['paymentProcessorId'];
  90. $type = _iats_civicrm_is_iats($payment_processor_id);
  91. $subtype = substr($type, 11);
  92. // i.e. now.
  93. $receive_date = date("YmdHis", time());
  94. $contribution = array(
  95. 'version' => 3,
  96. 'contact_id' => $contact_id,
  97. 'receive_date' => $receive_date,
  98. 'total_amount' => $total_amount,
  99. 'contribution_recur_id' => $contribution_recur_id,
  100. 'invoice_id' => $hash,
  101. 'contribution_status_id' => 2, /* initialize as pending, so we can run completetransaction after taking the money */
  102. 'payment_processor' => $payment_processor_id,
  103. 'is_test' => $values['is_test'], /* propagate the is_test value from the form */
  104. );
  105. foreach (array('payment_instrument_id', 'currency', 'financial_type_id') as $key) {
  106. $contribution[$key] = $contribution_template[$key];
  107. }
  108. $options = array(
  109. 'is_email_receipt' => (empty($values['is_email_receipt']) ? '0' : '1'),
  110. 'customer_code' => $values['customerCode'],
  111. 'subtype' => $subtype,
  112. );
  113. if ($is_recurrence) {
  114. $contribution['source'] = "iATS Payments $subtype Recurring Contribution (id=$contribution_recur_id)";
  115. // We'll use the repeattransaction if the total amount is the same
  116. $original_contribution_id = ($contribution_template['total_amount'] == $total_amount) ? $contribution_template['original_contribution_id'] : NULL;
  117. }
  118. else {
  119. $original_contribution_id = NULL;
  120. unset($contribution['contribution_recur_id']);
  121. $contribution['source'] = "iATS Payments $subtype One-Time Contribution (using id=$contribution_recur_id)";
  122. }
  123. // Now all the hard work in this function, recycled from the original recurring payment job.
  124. $result = _iats_process_contribution_payment($contribution, $options, $original_contribution_id);
  125. return $result;
  126. }
  127. /**
  128. *
  129. */
  130. public function buildQuickForm() {
  131. list($civicrm_fields, $iats_fields, $labels) = $this->getFields();
  132. $this->add('hidden', 'cid');
  133. $this->add('hidden', 'crid');
  134. $this->add('hidden', 'customerCode');
  135. $this->add('hidden', 'paymentProcessorId');
  136. $this->add('hidden', 'is_test');
  137. $cid = CRM_Utils_Request::retrieve('cid', 'Integer');
  138. $crid = CRM_Utils_Request::retrieve('crid', 'Integer');
  139. $customerCode = CRM_Utils_Request::retrieve('customerCode', 'String');
  140. $paymentProcessorId = CRM_Utils_Request::retrieve('paymentProcessorId', 'Positive');
  141. $is_test = CRM_Utils_Request::retrieve('is_test', 'Integer');
  142. $is_recurrence = CRM_Utils_Request::retrieve('is_recurrence', 'Integer');
  143. $defaults = array(
  144. 'cid' => $cid,
  145. 'crid' => $crid,
  146. 'customerCode' => $customerCode,
  147. 'paymentProcessorId' => $paymentProcessorId,
  148. 'is_test' => $is_test,
  149. 'is_recurrence' => 1,
  150. );
  151. $this->setDefaults($defaults);
  152. /* always show lots of detail about the card about to be charged or just charged */
  153. try {
  154. $customer = $this->getCustomerCodeDetail($defaults);
  155. }
  156. catch (Exception $e) {
  157. CRM_Core_Session::setStatus($e->getMessage(), ts('Warning'), 'alert');
  158. return;
  159. }
  160. foreach ($labels as $name => $label) {
  161. $iats_field = $iats_fields[$name];
  162. if (is_string($customer[$iats_field])) {
  163. $this->add('static', $name, $label, $customer[$iats_field]);
  164. }
  165. }
  166. // todo: show past charges/dates ?
  167. // Add form elements.
  168. $this->addMoney(
  169. // Field name.
  170. 'amount',
  171. // Field label.
  172. 'Amount',
  173. TRUE, NULL, FALSE
  174. );
  175. $this->add(
  176. // Field type.
  177. 'checkbox',
  178. // Field name.
  179. 'is_email_receipt',
  180. ts('Automated email receipt for this contribution.')
  181. );
  182. $this->add(
  183. // Field type.
  184. 'checkbox',
  185. // Field name.
  186. 'is_recurrence',
  187. ts('Create this as a contribution in the recurring series.')
  188. );
  189. $this->addButtons(array(
  190. array(
  191. 'type' => 'submit',
  192. 'name' => ts('Charge this card'),
  193. 'isDefault' => TRUE,
  194. ),
  195. array(
  196. 'type' => 'cancel',
  197. 'name' => ts('Back'),
  198. ),
  199. ));
  200. // Export form elements.
  201. $this->assign('elementNames', $this->getRenderableElementNames());
  202. // If necessary, warn the user about the nature of what they are about to do.
  203. if (0 !== $is_recurrence) { // this if is not working!
  204. $message = ts('The contribution created by this form will be saved as contribution in the existing recurring series unless you uncheck the corresponding setting.'); // , $type, $options);.
  205. CRM_Core_Session::setStatus($message, 'One-Time Charge');
  206. }
  207. parent::buildQuickForm();
  208. }
  209. /**
  210. *
  211. */
  212. public function postProcess() {
  213. $values = $this->exportValues();
  214. // print_r($values); die();
  215. // send charge request to iATS.
  216. $result = $this->processCreditCardCustomer($values);
  217. $message = print_r($result, TRUE);
  218. // , $type, $options);.
  219. CRM_Core_Session::setStatus($message, 'Customer Card Charged');
  220. parent::postProcess();
  221. }
  222. /**
  223. * Get the fields/elements defined in this form.
  224. *
  225. * @return array (string)
  226. */
  227. public function getRenderableElementNames() {
  228. // The _elements list includes some items which should not be
  229. // auto-rendered in the loop -- such as "qfKey" and "buttons". These
  230. // items don't have labels. We'll identify renderable by filtering on
  231. // the 'label'.
  232. $elementNames = array();
  233. foreach ($this->_elements as $element) {
  234. $label = $element->getLabel();
  235. if (!empty($label)) {
  236. $elementNames[] = $element->getName();
  237. }
  238. }
  239. return $elementNames;
  240. }
  241. }