Skip to content

Commit

Permalink
refs #41746, fixes tappay card notify doesn't update recurring
Browse files Browse the repository at this point in the history
  • Loading branch information
jimyhuang committed Nov 20, 2024
1 parent 87ed97c commit ae3ca50
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 16 deletions.
53 changes: 39 additions & 14 deletions CRM/Core/Payment/TapPay.php
Original file line number Diff line number Diff line change
Expand Up @@ -1286,38 +1286,63 @@ public static function cardNotify($url_params, $request = NULL) {
if (empty($token)) {
continue;
}
$sql = "SELECT contribution_id, contribution_recur_id, expiry_date FROM civicrm_contribution_tappay WHERE card_token = %1";
$sql = "SELECT contribution_id, contribution_recur_id, expiry_date FROM civicrm_contribution_tappay WHERE card_token = %1 ORDER BY contribution_id DESC";
$params = array(
1 => array($token, 'String'),
);
$dao = CRM_Core_DAO::executeQuery($sql, $params);
$written = FALSE;
while ($dao->fetch()) {
$recordData = array(
'contribution_id' => $dao->contribution_id,
'url' => $requestURL,
'date' => date('Y-m-d H:i:s'),
'post_data' => $input,
);
CRM_Core_Payment_TapPayAPI::writeRecord(NULL, $recordData);

if (!$written) {
$recordData = array(
'contribution_id' => $dao->contribution_id,
'url' => $requestURL,
'date' => date('Y-m-d H:i:s'),
'post_data' => $input,
);
CRM_Core_Payment_TapPayAPI::writeRecord(NULL, $recordData);
$written = TRUE;
}
// Update contribution_tappay for new expiry_date only. *DO NOT* touch other fields
$year = $month = $expiry_date = NULL;
$year = $month = $expiryDate = NULL;
if ($data->card_info->expiry_date) {
$year = substr($data->card_info->expiry_date, 0, 4);
$month = substr($data->card_info->expiry_date, 4, 2);
$expiry_date = date('Y-m-d', strtotime('last day of this month', strtotime($year.'-'.$month.'-01')));
if ($expiry_date != $dao->expiry_date && strtotime($expiry_date) > strtotime($dao->expiry_date)) {
$expiryDate = date('Y-m-d', strtotime('last day of this month', strtotime($year.'-'.$month.'-01')));

// update status
if (strtolower($data->card_info->token_status) !== 'active') {
$tokenStatus = strtolower($data->card_info->token_status);
$params = array(
'id' => $dao->contribution_recur_id,
'contribution_status_id' => 7, // suspend
'auto_renew' => 9,
'message' => ts("The recurring have been suspended due to TapPay Notify token status is %1.", array(1 => $tokenStatus)),
);
CRM_Contribute_BAO_ContributionRecur::add($params, CRM_Core_DAO::$_nullObject);
$noteTitle = ts('TapPay Payment').': '.ts('Credit Card Information').' '.ts('updated');
CRM_Contribute_BAO_ContributionRecur::addNote($dao->contribution_recur_id, $noteTitle, $params['message']);
break;
}
elseif ($expiryDate != $dao->expiry_date && strtotime($expiryDate ) > strtotime($dao->expiry_date)) {
// check token_status
$sql = "UPDATE civicrm_contribution_tappay SET expiry_date = %1 WHERE card_token = %2";
CRM_Core_DAO::executeQuery($sql, array(
1 => array($expiry_date, 'String'),
1 => array($expiryDate, 'String'),
2 => array($token, 'String'),
));
$params = array(
'id' => $dao->contribution_recur_id,
'auto_renew' => 2,
'message' => ts("Update expiry date from {$dao->expiry_date} to $expiry_date"),
'message' => ts("Expiry date updated from %1 to %2", array(
1 => $dao->expiry_date,
2 => $expiryDate
)),
);
CRM_Contribute_BAO_ContributionRecur::add($params, CRM_Core_DAO::$_nullObject);
$noteTitle = ts('TapPay Payment').': '.ts('Card Expiry Date').' '.ts('updated');
CRM_Contribute_BAO_ContributionRecur::addNote($dao->contribution_recur_id, $noteTitle, ts("From").': '.$dao->expiry_date);
break;
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion l10n/pot/civicrm.pot
Original file line number Diff line number Diff line change
Expand Up @@ -49595,4 +49595,8 @@ msgstr ""

#: CRM/Batch/BAO/Batch.php
msgid "Batch running failed. Contact the site administrator for assistance."
msgstr ""
msgstr ""

#: CRM/Core/Payment/TapPay.php
msgid "The recurring have been suspended due to TapPay Notify token status is %1."
msgstr ""
Binary file modified l10n/zh_TW/LC_MESSAGES/civicrm.mo
Binary file not shown.
7 changes: 6 additions & 1 deletion l10n/zh_TW/civicrm.po
Original file line number Diff line number Diff line change
Expand Up @@ -18909,7 +18909,7 @@ msgstr "修改至"
#: templates/CRM/Event/Page/EventInfo.tpl
#: templates/CRM/Event/Page/ICalendar.tpl
msgid "When"
msgstr "何時"
msgstr ""

#: CRM/Report/Form/Contact/LoggingSummary.php
msgid "Altered Contact"
Expand Down Expand Up @@ -50165,3 +50165,8 @@ msgstr "當身分證字號呈現方式不是完全顯示時,無法啟用電子
#: CRM/Batch/BAO/Batch.php
msgid "Batch running failed. Contact the site administrator for assistance."
msgstr "批次執行失敗。請聯繫網站管理員以取得協助。"

#: CRM/Core/Payment/TapPay.php
msgid ""
"The recurring have been suspended due to TapPay Notify token status is %1."
msgstr "這個定期已暫停,因為 TapPay 拋送通知卡片代號狀態(token status)為 %1。"
24 changes: 24 additions & 0 deletions tests/phpunit/CRM/Core/Payment/TapPayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -758,5 +758,29 @@ function testUpdateExpiryDate() {
while($dao->fetch()) {
$this->assertEquals('2030-12-31', $dao->expiry_date, "In line " . __LINE__);
}
sleep(2);

$notifyJson = '{
"status" : 0,
"msg" : "OK",
"card_token" : ["'.$this->_cardToken.'"],
"card_info" : {
"bin_code" : "123456",
"last_four" : "4321",
"issuer" : "Ignore",
"funding" : "Ignore",
"type" : "Ignore",
"level" : "Ignore",
"country" : "Ignore",
"country_code" : "Ignore",
"expiry_date" : "203012",
"token_status" : "SUSPENDED"
}
}';
$_SERVER['REQUEST_URI'] = '/civicrm/tappay/cardnotify';
CRM_Core_Payment_TapPay::cardNotify(NULL, $notifyJson);
$contributionRecurID = CRM_Core_DAO::singleValueQuery("SELECT contribution_recur_id FROM civicrm_contribution_tappay WHERE card_token = %1 ORDER BY id DESC", array( 1 => array($this->_cardToken, 'String')));
$contributionStatusId = CRM_Core_DAO::singleValueQuery("SELECT contribution_status_id FROM civicrm_contribution_recur WHERE id = %1", array( 1 => array($contributionRecurID, 'Integer')));
$this->assertEquals('7', $contributionStatusId, "In line " . __LINE__);
}
}

0 comments on commit ae3ca50

Please sign in to comment.