From aefd7f6b9a531399a8d8b7eca3d69e45c76fcb1e Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 10 Jun 2015 12:10:13 +1200 Subject: [PATCH] CRM-16555 move paypal code into payment subsystem as part of getting js working --- CRM/Contribute/Form/AdditionalPayment.php | 2 +- CRM/Contribute/Form/Contribution.php | 2 +- CRM/Contribute/Form/Contribution/Confirm.php | 3 - CRM/Contribute/Form/Contribution/Main.php | 98 +++++++++++--------- CRM/Core/Payment.php | 59 ++++++++++++ CRM/Core/Payment/FirstData.php | 2 +- CRM/Core/Payment/Form.php | 8 +- CRM/Core/Payment/Moneris.php | 1 - CRM/Core/Payment/PayPalImpl.php | 18 ++-- CRM/Core/Region.php | 7 +- CRM/Event/Cart/Form/Checkout/Payment.php | 1 - CRM/Event/Form/Participant.php | 1 - CRM/Event/Form/Registration/Confirm.php | 2 - CRM/Event/Form/Registration/Register.php | 4 - CRM/Member/Form/Membership.php | 1 - CRM/Member/Form/MembershipRenewal.php | 1 - 16 files changed, 131 insertions(+), 79 deletions(-) diff --git a/CRM/Contribute/Form/AdditionalPayment.php b/CRM/Contribute/Form/AdditionalPayment.php index 1ae7a5d275..3b6d0f0420 100644 --- a/CRM/Contribute/Form/AdditionalPayment.php +++ b/CRM/Contribute/Form/AdditionalPayment.php @@ -491,7 +491,7 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract $this->_params, $config->defaultCurrency ); - $this->_params['payment_action'] = 'Sale'; + if (!empty($this->_params['trxn_date'])) { $this->_params['receive_date'] = CRM_Utils_Date::processDate($this->_params['trxn_date'], $this->_params['trxn_date_time']); } diff --git a/CRM/Contribute/Form/Contribution.php b/CRM/Contribute/Form/Contribution.php index bc688f57c7..b80604ff1c 100644 --- a/CRM/Contribute/Form/Contribution.php +++ b/CRM/Contribute/Form/Contribution.php @@ -1143,7 +1143,7 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP $this->_params, CRM_Core_Config::singleton()->defaultCurrency ); - $this->_params['payment_action'] = 'Sale'; + if (!empty($this->_params['receive_date'])) { $this->_params['receive_date'] = CRM_Utils_Date::processDate($this->_params['receive_date'], $this->_params['receive_date_time']); } diff --git a/CRM/Contribute/Form/Contribution/Confirm.php b/CRM/Contribute/Form/Contribution/Confirm.php index 272f7e1372..5e38c9fde0 100644 --- a/CRM/Contribute/Form/Contribution/Confirm.php +++ b/CRM/Contribute/Form/Contribution/Confirm.php @@ -246,8 +246,6 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr // we use this here to incorporate any changes made by folks in hooks $this->_params['currencyID'] = $config->defaultCurrency; - $this->_params['payment_action'] = 'Sale'; - // also merge all the other values from the profile fields $values = $this->controller->exportValues('Main'); $skipFields = array( @@ -296,7 +294,6 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr $this->setFormAmountFields($this->_params['priceSetId']); } $this->_params['currencyID'] = $config->defaultCurrency; - $this->_params['payment_action'] = 'Sale'; } $this->_params['is_pay_later'] = $this->get('is_pay_later'); diff --git a/CRM/Contribute/Form/Contribution/Main.php b/CRM/Contribute/Form/Contribution/Main.php index 08f9e23388..b45551ce20 100644 --- a/CRM/Contribute/Form/Contribution/Main.php +++ b/CRM/Contribute/Form/Contribution/Main.php @@ -231,18 +231,20 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu } } - // // hack to simplify credit card entry for testing - // $this->_defaults['credit_card_type'] = 'Visa'; - // $this->_defaults['amount'] = 168; - // $this->_defaults['credit_card_number'] = '4111111111111111'; - // $this->_defaults['cvv2'] = '000'; - // $this->_defaults['credit_card_exp_date'] = array('Y' => date('Y')+1, 'M' => '05'); - - // // hack to simplify direct debit entry for testing - // $this->_defaults['account_holder'] = 'Max Müller'; - // $this->_defaults['bank_account_number'] = '12345678'; - // $this->_defaults['bank_identification_number'] = '12030000'; - // $this->_defaults['bank_name'] = 'Bankname'; + /* + * hack to simplify credit card entry for testing + * + * $this->_defaults['credit_card_type'] = 'Visa'; + * $this->_defaults['amount'] = 168; + * $this->_defaults['credit_card_number'] = '4111111111111111'; + * $this->_defaults['cvv2'] = '000'; + * $this->_defaults['credit_card_exp_date'] = array('Y' => date('Y')+1, 'M' => '05'); + * // hack to simplify direct debit entry for testing + * $this->_defaults['account_holder'] = 'Max Müller'; + * $this->_defaults['bank_account_number'] = '12345678'; + * $this->_defaults['bank_identification_number'] = '12030000'; + * $this->_defaults['bank_name'] = 'Bankname'; + */ //build set default for pledge overdue payment. if (!empty($this->_values['pledge_id'])) { @@ -722,7 +724,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu */ public static function formRule($fields, $files, $self) { $errors = array(); - $amount = self::computeAmount($fields, $self); + $amount = self::computeAmount($fields, $self->_values); if ((!empty($fields['selectMembership']) && $fields['selectMembership'] != 'no_thanks' @@ -1078,15 +1080,16 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu } /** + * Compute amount to be paid. + * * @param array $params - * @param CRM_Core_Form $form + * @param array $formValues * * @return int|mixed|null|string */ - public static function computeAmount(&$params, &$form) { - $amount = NULL; + public static function computeAmount($params, $formValues) { - // first clean up the other amount field if present + // First clean up the other amount field if present. if (isset($params['amount_other'])) { $params['amount_other'] = CRM_Utils_Rule::cleanMoney($params['amount_other']); } @@ -1101,12 +1104,12 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu } } else { - if (!empty($form->_values['amount'])) { + if (!empty($formValues['amount'])) { $amountID = CRM_Utils_Array::value('amount', $params); if ($amountID) { - $params['amount_level'] = CRM_Utils_Array::value('label', $form->_values[$amountID]); - $amount = CRM_Utils_Array::value('value', $form->_values[$amountID]); + $params['amount_level'] = CRM_Utils_Array::value('label', $formValues[$amountID]); + $amount = CRM_Utils_Array::value('value', $formValues[$amountID]); } } } @@ -1195,7 +1198,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu } // from here on down, $params['amount'] holds a monetary value (or null) rather than an option ID - $params['amount'] = self::computeAmount($params, $this); + $params['amount'] = self::computeAmount($params, $this->_values); $params['separate_amount'] = $params['amount']; $memFee = NULL; if (!empty($params['selectMembership'])) { @@ -1252,7 +1255,8 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu if ($is_quick_config) { foreach ($this->_values['fee'] as $key => & $val) { if ($val['name'] == 'other_amount' && $val['html_type'] == 'Text' && !empty($params['price_' . $key])) { - $params['price_' . $key] = CRM_Utils_Rule::cleanMoney($params['price_' . $key]); //Clean out any currency symbols + // Clean out any currency symbols. + $params['price_' . $key] = CRM_Utils_Rule::cleanMoney($params['price_' . $key]); if ($params['price_' . $key] != 0) { foreach ($val['options'] as $optionKey => & $options) { $options['amount'] = CRM_Utils_Array::value('price_' . $key, $params); @@ -1281,7 +1285,8 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu $proceFieldAmount += $lineItem[$params['priceSetId']][$fieldOption]['tax_amount']; } if (!$this->_membershipBlock['is_separate_payment']) { - $params['amount'] = $proceFieldAmount; //require when separate membership not used + //require when separate membership not used + $params['amount'] = $proceFieldAmount; } } $this->set('lineItem', $lineItem); @@ -1316,28 +1321,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu array($this->_expressButtonName, $this->_expressButtonName . '_x', $this->_expressButtonName . '_y') ) && empty($params['is_pay_later']) ) { - $this->set('contributeMode', 'express'); - - $donateURL = CRM_Utils_System::url('civicrm/contribute', '_qf_Contribute_display=1'); - $params['cancelURL'] = CRM_Utils_System::url('civicrm/contribute/transact', "_qf_Main_display=1&qfKey={$params['qfKey']}", TRUE, NULL, FALSE); - $params['returnURL'] = CRM_Utils_System::url('civicrm/contribute/transact', "_qf_Confirm_display=1&rfp=1&qfKey={$params['qfKey']}", TRUE, NULL, FALSE); - $params['invoiceID'] = $invoiceID; - $params['description'] = ts('Online Contribution') . ': ' . (($this->_pcpInfo['title']) ? $this->_pcpInfo['title'] : $this->_values['title']); - - //default action is Sale - $params['payment_action'] = 'Sale'; - - $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this); - $token = $payment->setExpressCheckout($params); - if (is_a($token, 'CRM_Core_Error')) { - CRM_Core_Error::displaySessionError($token); - CRM_Utils_System::redirect($params['cancelURL']); - } - - $this->set('token', $token); - - $paymentURL = $this->_paymentProcessor['url_site'] . "/cgi-bin/webscr?cmd=_express-checkout&token=$token"; - CRM_Utils_System::redirect($paymentURL); + $this->handlePaypalExpress($invoiceID, $params); } } elseif ($this->_paymentProcessor && @@ -1411,4 +1395,28 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu $form->assign('paymentProcessorID', $form->_paymentProcessorID); } + /** + * @param $invoiceID + * @param $params + * + * @return mixed + */ + protected function handlePaypalExpress($invoiceID, $params) { + $this->set('contributeMode', 'express'); + + $params['invoiceID'] = $invoiceID; + $params['description'] = ts('Online Contribution') . ': ' . (($this->_pcpInfo['title']) ? $this->_pcpInfo['title'] : $this->_values['title']); + + $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this); + $token = $payment->setExpressCheckout($params); + if (is_a($token, 'CRM_Core_Error')) { + CRM_Core_Error::displaySessionError($token); + CRM_Utils_System::redirect($params['cancelURL']); + } + + $this->set('token', $token); + $paymentURL = $this->_paymentProcessor['url_site'] . "/cgi-bin/webscr?cmd=_express-checkout&token=$token"; + CRM_Utils_System::redirect($paymentURL); + } + } diff --git a/CRM/Core/Payment.php b/CRM/Core/Payment.php index 7d70cd484c..d57b98460f 100644 --- a/CRM/Core/Payment.php +++ b/CRM/Core/Payment.php @@ -490,6 +490,65 @@ abstract class CRM_Core_Payment { ); } + /** + * Get base url dependent on component. + * + * @return string|void + */ + protected function getBaseReturnUrl() { + if ($this->_component == 'event') { + $baseURL = 'civicrm/event/register'; + } + else { + $baseURL = 'civicrm/contribute/transact'; + } + return $baseURL; + } + + /** + * Get url to return to after cancelled or failed transaction + * + * @param $qfKey + * @param $participantID + * + * @return string cancel url + */ + protected function getCancelUrl($qfKey, $participantID) { + if ($this->_component == 'event') { + return CRM_Utils_System::url($this->getBaseReturnUrl(), array( + 'reset' => 1, + 'cc' => 'fail', + 'participantId' => $participantID, + ), + TRUE, NULL, FALSE + ); + } + + return CRM_Utils_System::url($this->getBaseReturnUrl(), array( + '_qf_Main_display' => 1, + 'qfKey' => $qfKey, + 'cancel' => 1, + ), + TRUE, NULL, FALSE + ); + } + + /** + * Get URL to return the browser to on success + * + * @param $qfKey + * + * @return string + */ + protected function getReturnSuccessUrl($qfKey) { + return CRM_Utils_System::url($this->getBaseReturnUrl(), array( + '_qf_ThankYou_display' => 1, + 'qfKey' => $qfKey + ), + TRUE, NULL, FALSE + ); + } + /** * Calling this from outside the payment subsystem is deprecated - use doPayment. * diff --git a/CRM/Core/Payment/FirstData.php b/CRM/Core/Payment/FirstData.php index 792d23db15..9055fcb509 100644 --- a/CRM/Core/Payment/FirstData.php +++ b/CRM/Core/Payment/FirstData.php @@ -118,7 +118,7 @@ class CRM_Core_Payment_FirstData extends CRM_Core_Payment { $requestFields['transactionorigin'] = "Eci"; #32 character string $requestFields['invoice_number'] = $params['invoiceID']; - $requestFields['ordertype'] = $params['payment_action']; + $requestFields['ordertype'] = 'Sale'; $requestFields['comments'] = $params['description']; //**********************set 'result' for live testing ************************** // $requestFields[ 'result' ] = ""; #set to "Good", "Decline" or "Duplicate" diff --git a/CRM/Core/Payment/Form.php b/CRM/Core/Payment/Form.php index 2251c4608d..19599e6dfb 100644 --- a/CRM/Core/Payment/Form.php +++ b/CRM/Core/Payment/Form.php @@ -260,12 +260,8 @@ class CRM_Core_Payment_Form { $form->assign('profileAddressFields', $profileAddressFields); } - // $processor->buildForm appears to be an undocumented (possibly unused) option for payment processors - // which was previously available only in some form flows - if (!empty($form->_paymentProcessor) && !empty($form->_paymentProcessor['object']) && $form->_paymentProcessor['object']->isSupported('buildForm')) { - if ($form->_paymentProcessor['object']->buildForm($form)) { - return NULL; - } + if ($processor['object']->buildForm($form)) { + return NULL; } self::setPaymentFieldsByProcessor($form, $processor, empty($isBillingDataOptional)); diff --git a/CRM/Core/Payment/Moneris.php b/CRM/Core/Payment/Moneris.php index e6a1cde54f..e132f4e240 100644 --- a/CRM/Core/Payment/Moneris.php +++ b/CRM/Core/Payment/Moneris.php @@ -103,7 +103,6 @@ class CRM_Core_Payment_Moneris extends CRM_Core_Payment { /* unused params: cvv not yet implemented, payment action ingored (should test for 'Sale' value?) [cvv2] => 000 [ip_address] => 192.168.0.103 - [payment_action] => Sale [contact_type] => Individual [geo_coord_id] => 1 */ diff --git a/CRM/Core/Payment/PayPalImpl.php b/CRM/Core/Payment/PayPalImpl.php index db61e765fb..7f257c6aa3 100644 --- a/CRM/Core/Payment/PayPalImpl.php +++ b/CRM/Core/Payment/PayPalImpl.php @@ -99,6 +99,7 @@ class CRM_Core_Payment_PayPalImpl extends CRM_Core_Payment { if ($this->_processorName == 'PayPal Express') { CRM_Core_Region::instance('billing-block-post')->add(array( 'template' => 'CRM/Financial/Form/PaypalExpress.tpl', + 'name' => 'paypal_express', )); } if ($this->_processorName == 'PayPal Pro') { @@ -143,13 +144,13 @@ class CRM_Core_Payment_PayPalImpl extends CRM_Core_Payment { $this->initialize($args, 'SetExpressCheckout'); - $args['paymentAction'] = $params['payment_action']; + $args['paymentAction'] = 'Sale'; $args['amt'] = $params['amount']; $args['currencyCode'] = $params['currencyID']; $args['desc'] = CRM_Utils_Array::value('description', $params); $args['invnum'] = $params['invoiceID']; - $args['returnURL'] = $params['returnURL']; - $args['cancelURL'] = $params['cancelURL']; + $args['returnURL'] = $this->getReturnSuccessUrl($params['qfKey']); + $args['cancelURL'] = $this->getCancelUrl($params['qfKey'], NULL); $args['version'] = '56.0'; //LCD if recurring, collect additional data and set some values @@ -230,15 +231,14 @@ class CRM_Core_Payment_PayPalImpl extends CRM_Core_Payment { $args = array(); $this->initialize($args, 'DoExpressCheckoutPayment'); - $args['token'] = $params['token']; - $args['paymentAction'] = $params['payment_action']; + $args['paymentAction'] = 'Sale'; $args['amt'] = $params['amount']; $args['currencyCode'] = $params['currencyID']; $args['payerID'] = $params['payer_id']; $args['invnum'] = $params['invoiceID']; - $args['returnURL'] = CRM_Utils_Array::value('returnURL', $params); - $args['cancelURL'] = CRM_Utils_Array::value('cancelURL', $params); + $args['returnURL'] = $this->getReturnSuccessUrl($params['qfKey']); + $args['cancelURL'] = $this->getCancelUrl($params['qfKey'], NULL); $args['desc'] = $params['description']; // add CiviCRM BN code @@ -280,7 +280,7 @@ class CRM_Core_Payment_PayPalImpl extends CRM_Core_Payment { $start_date = date('Y-m-d\T00:00:00\Z', $start_time); $args['token'] = $params['token']; - $args['paymentAction'] = $params['payment_action']; + $args['paymentAction'] = 'Sale'; $args['amt'] = $params['amount']; $args['currencyCode'] = $params['currencyID']; $args['payerID'] = $params['payer_id']; @@ -354,7 +354,7 @@ class CRM_Core_Payment_PayPalImpl extends CRM_Core_Payment { $this->initialize($args, 'DoDirectPayment'); - $args['paymentAction'] = $params['payment_action']; + $args['paymentAction'] = 'Sale'; $args['amt'] = $params['amount']; $args['currencyCode'] = $params['currencyID']; $args['invnum'] = $params['invoiceID']; diff --git a/CRM/Core/Region.php b/CRM/Core/Region.php index 7868cee238..77b3e6065b 100644 --- a/CRM/Core/Region.php +++ b/CRM/Core/Region.php @@ -122,6 +122,7 @@ class CRM_Core_Region { if (!isset($snippet['name'])) { $snippet['name'] = count($this->_snippets); } + $this->_snippets[$snippet['name']] = $snippet; $this->_isSorted = FALSE; return $snippet; @@ -137,12 +138,14 @@ class CRM_Core_Region { } /** + * Get snippet. + * * @param string $name * * @return mixed */ - public function &get($name) { - return @$this->_snippets[$name]; + public function get($name) { + return !empty($this->_snippets[$name]) ? $this->_snippets[$name] : NULL; } /** diff --git a/CRM/Event/Cart/Form/Checkout/Payment.php b/CRM/Event/Cart/Form/Checkout/Payment.php index b646e12909..c16c47c601 100644 --- a/CRM/Event/Cart/Form/Checkout/Payment.php +++ b/CRM/Event/Cart/Form/Checkout/Payment.php @@ -575,7 +575,6 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { } $params['ip_address'] = CRM_Utils_System::ipAddress(); $params['currencyID'] = $config->defaultCurrency; - $params['payment_action'] = 'Sale'; $payment = &CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this); CRM_Core_Payment_Form::mapParams($this->_bltID, $params, $params, TRUE); diff --git a/CRM/Event/Form/Participant.php b/CRM/Event/Form/Participant.php index 1bd751a848..cd4a5497f6 100644 --- a/CRM/Event/Form/Participant.php +++ b/CRM/Event/Form/Participant.php @@ -1185,7 +1185,6 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment $this->_params['amount'] = $params['fee_amount']; $this->_params['amount_level'] = $params['amount_level']; $this->_params['currencyID'] = $config->defaultCurrency; - $this->_params['payment_action'] = 'Sale'; $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE)); // at this point we've created a contact and stored its address etc diff --git a/CRM/Event/Form/Registration/Confirm.php b/CRM/Event/Form/Registration/Confirm.php index 72b90c6502..11dcbcf4bb 100644 --- a/CRM/Event/Form/Registration/Confirm.php +++ b/CRM/Event/Form/Registration/Confirm.php @@ -126,7 +126,6 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration { } $params['amount_level'] = $this->_params[0]['amount_level']; $params['currencyID'] = $this->_params[0]['currencyID']; - $params['payment_action'] = 'Sale'; // also merge all the other values from the profile fields $values = $this->controller->exportValues('Register'); @@ -172,7 +171,6 @@ class CRM_Event_Form_Registration_Confirm extends CRM_Event_Form_Registration { if ($this->_values['event']['is_monetary']) { $registerParams['ip_address'] = CRM_Utils_System::ipAddress(); $registerParams['currencyID'] = $this->_params[0]['currencyID']; - $registerParams['payment_action'] = 'Sale'; } //assign back primary participant params. $this->_params[0] = $registerParams; diff --git a/CRM/Event/Form/Registration/Register.php b/CRM/Event/Form/Registration/Register.php index 8aa260e2e8..56b2342211 100644 --- a/CRM/Event/Form/Registration/Register.php +++ b/CRM/Event/Form/Registration/Register.php @@ -1137,7 +1137,6 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration { if ($this->_values['event']['is_monetary']) { $params['ip_address'] = CRM_Utils_System::ipAddress(); $params['currencyID'] = $config->defaultCurrency; - $params['payment_action'] = 'Sale'; $params['invoiceID'] = $invoiceID; } $this->_params = $this->get('params'); @@ -1187,9 +1186,6 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration { ); $params['invoiceID'] = $invoiceID; - //default action is Sale - $params['payment_action'] = 'Sale'; - $token = $payment->setExpressCheckout($params); if (is_a($token, 'CRM_Core_Error')) { CRM_Core_Error::displaySessionError($token); diff --git a/CRM/Member/Form/Membership.php b/CRM/Member/Form/Membership.php index 88d22638e5..8d51eb4342 100644 --- a/CRM/Member/Form/Membership.php +++ b/CRM/Member/Form/Membership.php @@ -1483,7 +1483,6 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { $this->_params['amount'] = $params['total_amount']; $this->_params['currencyID'] = $config->defaultCurrency; $this->_params['description'] = ts('Office Credit Card Membership Signup Contribution'); - $this->_params['payment_action'] = 'Sale'; $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE)); $this->_params['financial_type_id'] = $params['financial_type_id']; diff --git a/CRM/Member/Form/MembershipRenewal.php b/CRM/Member/Form/MembershipRenewal.php index 77af3103d9..0314ba27e4 100644 --- a/CRM/Member/Form/MembershipRenewal.php +++ b/CRM/Member/Form/MembershipRenewal.php @@ -534,7 +534,6 @@ class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form { $this->_params['ip_address'] = CRM_Utils_System::ipAddress(); $this->_params['amount'] = $formValues['total_amount']; $this->_params['currencyID'] = $config->defaultCurrency; - $this->_params['payment_action'] = 'Sale'; $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE)); // at this point we've created a contact and stored its address etc -- 2.25.1