Skip to content

Commit

Permalink
Refs #39188, Update MyPay DB, IPN, item name for word count.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chang Shu-Huai committed May 27, 2024
1 parent 5fe6962 commit a738690
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 12 deletions.
16 changes: 15 additions & 1 deletion CRM/Contribute/DAO/MyPayLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,20 @@ class CRM_Contribute_DAO_MyPayLog extends CRM_Core_DAO
* @var int unsigned
*/
public $contribution_id;
/**
* The uid in mypay transaction.
*
* @var string
*/
public $uid;
/**
* Request URL
*
* @var string
*/
public $url;
/**
* Request URL
* Request Command
*
* @var string
*/
Expand Down Expand Up @@ -184,6 +190,14 @@ static function &fields()
'default' => 'UL',
'FKClassName' => 'CRM_Contribute_DAO_Contribution',
) ,
'uid' => array(
'name' => 'uid',
'type' => CRM_Utils_Type::T_STRING,
'title' => ts('MyPay uid') ,
'maxlength' => 255,
'size' => CRM_Utils_Type::HUGE,
'default' => 'UL',
) ,
'url' => array(
'name' => 'url',
'type' => CRM_Utils_Type::T_STRING,
Expand Down
26 changes: 21 additions & 5 deletions CRM/Core/Payment/MyPay.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,18 @@ function getOrderArgs(&$vars, $component, $instrumentCode, $formKey) {
), '', '&');
$amount = $vars['currencyID'] == 'TWD' && strstr($vars['amount'], '.') ? substr($vars['amount'], 0, strpos($vars['amount'], '.')) : $vars['amount'];

if ($vars['contributionRecurID']) {
$params = array(
1 => $vars['contributionRecurID'],
2 => $vars['installments'] ? ts("%1 Periods", array(1 => $vars['installments'])) : ts('no period'),
);
$item = ts("Recur %1-%2", $params);
}
else {
$params = array(1 => $vars['contributionID']);
$item = ts("Contribution %1", $params);
}

$args = array(
'store_uid' => $paymentProcessor['user_name'],
'service' => array(
Expand All @@ -357,14 +369,15 @@ function getOrderArgs(&$vars, $component, $instrumentCode, $formKey) {
'user_id' => $vars['contact_id'],
'currency' => $vars['currencyID'],
'order_id' => $vars['trxn_id'],
'agent_sms_fee_type' => 0,
'cost' => (string) $amount,
'pfn' => '',
'ip' => CRM_Utils_System::ipAddress(),
'echo_0' => $component,
'echo_1' => $vars['contributionID'],
'items' => array( 0 => array(
'id' => $vars['trxn_id'],
'name' => preg_replace('~[^\p{L}\p{N}]++~u', ' ', $vars['item_name']),
'name' => $item,
'cost' => (string) $amount,
'total' => (string) $amount,
'amount' => 1,
Expand Down Expand Up @@ -442,11 +455,12 @@ function postData($url, $data){
$errno = curl_errno($ch);
// Record all data
// 1. Record log data.
$resultArray = json_decode($result, TRUE);
$saveData = array(
'uid' => $resultArray['uid'],
'return_data' => $result,
);
self::writeLog($this->_logId, $saveData);
$resultArray = json_decode($result, TRUE);
// 2. Record usable data.
if ($this->_contributionId) {
$transationData = array(
Expand Down Expand Up @@ -560,6 +574,7 @@ public static function doIPN($arguments, $instrument = NULL, $post = NULL, $get
if (!empty($post['uid']) && !empty($post['key']) && !empty($post['prc'])) {
// Save Data to Log.
$saveData = array(
'uid' => $post['uid'],
'date' => date('Y-m-d H:i:s'),
'post_data' => json_encode($post),
);
Expand Down Expand Up @@ -615,7 +630,7 @@ public static function doIPN($arguments, $instrument = NULL, $post = NULL, $get
$component = $post['echo_0'];
if(!empty($component)){
$ipn = new CRM_Core_Payment_MyPayIPN($post, $get);
$result = $ipn->main($component, $instrument);
$result = $ipn->main($instrument);
if(!empty($result) && $print){
echo $result;
}
Expand All @@ -638,7 +653,7 @@ public static function doIPN($arguments, $instrument = NULL, $post = NULL, $get
* @return number $id The `id` of the row.
*/
public static function writeLog($logId, $data = array()) {
$recordType = array('contribution_id', 'url', 'cmd', 'date', 'post_data', 'return_data');
$recordType = array('contribution_id', 'uid', 'url', 'cmd', 'date', 'post_data', 'return_data');

$record = new CRM_Contribute_DAO_MyPayLog();
if(!empty($logId)) {
Expand Down Expand Up @@ -700,7 +715,7 @@ static public function getTrxnIdByPost($input) {
$trxnId = NULL;
if ($input['order_id']) {
if ($input['uid'] && !empty($input['nois']) && $input['nois'] > 1) {
$trxnId = $input['order_id'].'-'.$input['uid'];
$trxnId = $input['order_id'].'-'.$input['nois'].'-'.$input['uid'];
}
else {
$trxnId = $input['order_id'];
Expand All @@ -710,3 +725,4 @@ static public function getTrxnIdByPost($input) {
}
}


19 changes: 16 additions & 3 deletions CRM/Core/Payment/MyPayIPN.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,24 @@ function main($instrument){
// set global variable for paymentProcessor
self::$_payment_processor =& $objects['paymentProcessor'];
self::$_input = $input;
if($objects['contribution']->contribution_status_id == 1 && empty($input['nois'])){
// already completed, skip
if ($objects['contribution']->contribution_status_id == 1 && empty($input['nois']) && $input['prc'] == '250') {
// Single contribution or first contribution, already completed, skip
CRM_Core_Error::debug_log_message("MyPay: The transaction uid: {$input['uid']}, associated with the contribution {$objects['contribution']->trxn_id}, has been successfully processed before. Skipped.");
return '8888';
}
else{
elseif (!empty($input['nois']) && $input['prc'] == '250') {
// Recurring and finished.
$trxnId = CRM_Core_Payment_MyPay::getTrxnIdByPost($input);
$sql = "SELECT contribution_status_id FROM civicrm_contribution WHERE trxn_id = %1";
$params = array( 1 => array($trxnId, 'String'));
$contributionStatusID = CRM_Core_DAO::singleValueQuery($sql, $params);
if ($contributionStatusID == 1) {
// already completed, skip
CRM_Core_Error::debug_log_message("MyPay: The transaction uid: {$input['uid']}, associated with the contribution {$trxnId}, has been successfully processed before. Skipped.");
return '8888';
}
}
else {
// start validation
$note = '';
if( $this->validateOthers($input, $ids, $objects, $note, $instrument) ){
Expand Down
8 changes: 6 additions & 2 deletions sql/civicrm.mysql
Original file line number Diff line number Diff line change
Expand Up @@ -4825,15 +4825,19 @@ CREATE TABLE civicrm_contribution_mypay_log (

id int unsigned NOT NULL AUTO_INCREMENT COMMENT 'MyPay Log ID',
contribution_id int unsigned DEFAULT NULL COMMENT 'Contribuution ID',
uid varchar(255) DEFAULT NULL COMMENT 'The uid in mypay transaction.',
url varchar(255) COMMENT 'Request URL',
cmd varchar(255) COMMENT 'Request URL',
cmd varchar(255) COMMENT 'Request Command',
date datetime COMMENT 'Request Date Time',
post_data text COMMENT 'Request Post Data',
return_data text COMMENT 'Request Return Data'
,
PRIMARY KEY ( id )

, INDEX UI_url(
, INDEX UI_uid(
uid
)
, INDEX UI_url(
url
)
, INDEX UI_date(
Expand Down
17 changes: 16 additions & 1 deletion xml/schema/Contribute/ContributionMyPayLog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@
<add>4.0</add>
<onDelete>SET NULL</onDelete>
</foreignKey>
<field>
<name>uid</name>
<uniqueName>uid</uniqueName>
<type>varchar</type>
<length>255</length>
<default>NULL</default>
<title>MyPay uid</title>
<comment>The uid in mypay transaction.</comment>
<add>4.0</add>
</field>
<field>
<name>url</name>
<uniqueName>url</uniqueName>
Expand All @@ -49,7 +59,7 @@
<title>service API CMD Name</title>
<type>varchar</type>
<length>255</length>
<comment>Request URL</comment>
<comment>Request Command</comment>
<add>4.0</add>
</field>
<field>
Expand All @@ -72,6 +82,11 @@
<comment>Request Return Data</comment>
<add>4.0</add>
</field>
<index>
<name>UI_uid</name>
<fieldName>uid</fieldName>
<add>4.0</add>
</index>
<index>
<name>UI_url</name>
<fieldName>url</fieldName>
Expand Down

0 comments on commit a738690

Please sign in to comment.