Skip to content

Commit

Permalink
dev/core#2717 Use Same order ->payment flow for non recurring back of…
Browse files Browse the repository at this point in the history
…fice memberships as back office
  • Loading branch information
eileenmcnaughton committed Jul 30, 2021
1 parent 49f03de commit 4eef168
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 58 deletions.
23 changes: 23 additions & 0 deletions CRM/Member/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,4 +568,27 @@ protected function getPaymentInstrumentID(): int {
return (int) $this->getSubmittedValue('payment_instrument_id') ?: $this->_paymentProcessor['object']->getPaymentInstrumentID();
}

/**
* Get the last 4 numbers of the card.
*
* @return int|null
*/
protected function getPanTruncation(): ?int {
$card = $this->getSubmittedValue('credit_card_number');
return $card ? (int) substr($card, -4) : NULL;
}

/**
* Get the card_type_id.
*
* This value is the integer representing the option value for
* the credit card type (visa, mastercard). It is stored as part of the
* payment record in civicrm_financial_trxn.
*
* @return int|null
*/
protected function getCardTypeID(): ?int {
return CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_FinancialTrxn', 'card_type_id', $this->getSubmittedValue('credit_card_type'));
}

}
100 changes: 43 additions & 57 deletions CRM/Member/Form/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -1103,10 +1103,6 @@ public function submit(): void {

if ($this->_mode) {
$params['total_amount'] = $this->order->getTotalAmount();

//CRM-20264 : Store CC type and number (last 4 digit) during backoffice or online payment
$params['card_type_id'] = $this->_params['card_type_id'] ?? NULL;
$params['pan_truncation'] = $this->_params['pan_truncation'] ?? NULL;
$params['financial_type_id'] = $this->getFinancialTypeID();

//get the payment processor id as per mode. Try removing in favour of beginPostProcess.
Expand Down Expand Up @@ -1146,46 +1142,46 @@ public function submit(): void {
// CRM-7137 -for recurring membership,
// we do need contribution and recurring records.
$result = NULL;
if ($this->isCreateRecurringContribution()) {
$this->_params = $formValues;
$contribution = civicrm_api3('Order', 'create',
[
'contact_id' => $this->_contributorContactID,
'line_items' => $this->getLineItemForOrderApi(),
'is_test' => $this->isTest(),
'campaign_id' => $this->getSubmittedValue('campaign_id'),
'source' => CRM_Utils_Array::value('source', $paymentParams, CRM_Utils_Array::value('description', $paymentParams)),
'payment_instrument_id' => $this->getPaymentInstrumentID(),
'financial_type_id' => $this->getFinancialTypeID(),
'receive_date' => $this->getReceiveDate(),
'tax_amount' => $this->order->getTotalTaxAmount(),
'total_amount' => $this->order->getTotalAmount(),
'invoice_id' => $this->getInvoiceID(),
'currency' => $this->getCurrency(),
'receipt_date' => $this->getSubmittedValue('send_receipt') ? date('YmdHis') : NULL,
'contribution_recur_id' => $this->getContributionRecurID(),
'skipCleanMoney' => TRUE,
]
);
$this->ids['Contribution'] = $contribution['id'];
$this->setMembershipIDsFromOrder($contribution);

//create new soft-credit record, CRM-13981
if ($softParams) {
$softParams['contribution_id'] = $contribution['id'];
$softParams['currency'] = $this->getCurrency();
$softParams['amount'] = $this->order->getTotalAmount();
CRM_Contribute_BAO_ContributionSoft::add($softParams);
}

$paymentParams['contactID'] = $this->_contactID;
$paymentParams['contributionID'] = $contribution['id'];

$paymentParams['contributionRecurID'] = $this->getContributionRecurID();
$paymentParams['is_recur'] = $this->isCreateRecurringContribution();
$params['contribution_id'] = $paymentParams['contributionID'];
$params['contribution_recur_id'] = $this->getContributionRecurID();
$this->_params = $formValues;
$contribution = civicrm_api3('Order', 'create',
[
'contact_id' => $this->_contributorContactID,
'line_items' => $this->getLineItemForOrderApi(),
'is_test' => $this->isTest(),
'campaign_id' => $this->getSubmittedValue('campaign_id'),
'source' => CRM_Utils_Array::value('source', $paymentParams, CRM_Utils_Array::value('description', $paymentParams)),
'payment_instrument_id' => $this->getPaymentInstrumentID(),
'financial_type_id' => $this->getFinancialTypeID(),
'receive_date' => $this->getReceiveDate(),
'tax_amount' => $this->order->getTotalTaxAmount(),
'total_amount' => $this->order->getTotalAmount(),
'invoice_id' => $this->getInvoiceID(),
'currency' => $this->getCurrency(),
'receipt_date' => $this->getSubmittedValue('send_receipt') ? date('YmdHis') : NULL,
'contribution_recur_id' => $this->getContributionRecurID(),
'skipCleanMoney' => TRUE,
]
);
$this->ids['Contribution'] = $contribution['id'];
$this->setMembershipIDsFromOrder($contribution);

//create new soft-credit record, CRM-13981
if ($softParams) {
$softParams['contribution_id'] = $contribution['id'];
$softParams['currency'] = $this->getCurrency();
$softParams['amount'] = $this->order->getTotalAmount();
CRM_Contribute_BAO_ContributionSoft::add($softParams);
}

$paymentParams['contactID'] = $this->_contactID;
$paymentParams['contributionID'] = $contribution['id'];

$paymentParams['contributionRecurID'] = $this->getContributionRecurID();
$paymentParams['is_recur'] = $this->isCreateRecurringContribution();
$params['contribution_id'] = $paymentParams['contributionID'];
$params['contribution_recur_id'] = $this->getContributionRecurID();

$paymentStatus = NULL;

if ($this->order->getTotalAmount() > 0.0) {
Expand All @@ -1202,6 +1198,8 @@ public function submit(): void {
'trxn_id' => $result['trxn_id'],
'contribution_id' => $params['contribution_id'],
'is_send_contribution_notification' => FALSE,
'card_type_id' => $this->getCardTypeID(),
'pan_truncation' => $this->getPanTruncation(),
]);
}
}
Expand Down Expand Up @@ -1260,10 +1258,6 @@ public function submit(): void {
$count = 0;
foreach ($this->_memTypeSelected as $memType) {
$membershipParams = array_merge($membershipTypeValues[$memType], $params);
//CRM-15366
if (!empty($softParams) && !$this->isCreateRecurringContribution()) {
$membershipParams['soft_credit'] = $softParams;
}
if (isset($result['fee_amount'])) {
$membershipParams['fee_amount'] = $result['fee_amount'];
}
Expand All @@ -1274,19 +1268,11 @@ public function submit(): void {
// process -
// @see http://wiki.civicrm.org/confluence/pages/viewpage.action?pageId=261062657#Payments&AccountsRoadmap-Movetowardsalwaysusinga2-steppaymentprocess
$membershipParams['contribution_status_id'] = $result['payment_status_id'] ?? NULL;
if ($this->isCreateRecurringContribution()) {
// The earlier process created the line items (although we want to get rid of the earlier one in favour
// of a single path!
unset($membershipParams['lineItems']);
}
// The earlier process created the line items (although we want to get rid of the earlier one in favour
// of a single path!
unset($membershipParams['lineItems']);
$membershipParams['payment_instrument_id'] = $this->getPaymentInstrumentID();
// @todo stop passing $ids (membership and userId only are set above)
if (!$this->isCreateRecurringContribution()) {
// For recurring we already created it 'the right way' (order api).
// In time we will do that for all paths through this code but for now
// we have not migrated the other paths.
$this->setMembership((array) CRM_Member_BAO_Membership::create($membershipParams, $ids));
}
$params['contribution'] = $membershipParams['contribution'] ?? NULL;
unset($params['lineItems']);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/CRM/Member/Form/MembershipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,7 @@ protected function createTwoMembershipsViaPriceSetInBackEnd($contactId): void {
'trxn_id' => 777,
'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_DAO_Contribution', 'contribution_status_id', 'Pending'),
'billing_first_name' => 'Test',
'billing_middlename' => 'Last',
'billing_middle_name' => 'Last',
'billing_street_address-5' => '10 Test St',
'billing_city-5' => 'Test',
'billing_state_province_id-5' => '1003',
Expand Down

0 comments on commit 4eef168

Please sign in to comment.