diff --git a/CRM/Contribute/DAO/MyPayLog.php b/CRM/Contribute/DAO/MyPayLog.php index 3209c5d29..efcc831b6 100644 --- a/CRM/Contribute/DAO/MyPayLog.php +++ b/CRM/Contribute/DAO/MyPayLog.php @@ -92,6 +92,12 @@ 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 * @@ -99,7 +105,7 @@ class CRM_Contribute_DAO_MyPayLog extends CRM_Core_DAO */ public $url; /** - * Request URL + * Request Command * * @var string */ @@ -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, diff --git a/CRM/Core/Payment/MyPay.php b/CRM/Core/Payment/MyPay.php index 4ae83ea8b..1f7a7f2bd 100644 --- a/CRM/Core/Payment/MyPay.php +++ b/CRM/Core/Payment/MyPay.php @@ -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( @@ -357,6 +369,7 @@ 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(), @@ -364,7 +377,7 @@ function getOrderArgs(&$vars, $component, $instrumentCode, $formKey) { '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, @@ -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( @@ -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), ); @@ -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; } @@ -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)) { @@ -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']; @@ -710,3 +725,4 @@ static public function getTrxnIdByPost($input) { } } + diff --git a/CRM/Core/Payment/MyPayIPN.php b/CRM/Core/Payment/MyPayIPN.php index 2d249a79e..7eaa93f83 100644 --- a/CRM/Core/Payment/MyPayIPN.php +++ b/CRM/Core/Payment/MyPayIPN.php @@ -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) ){ diff --git a/sql/civicrm.mysql b/sql/civicrm.mysql index 43433e285..6cf0e023a 100644 --- a/sql/civicrm.mysql +++ b/sql/civicrm.mysql @@ -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( diff --git a/xml/schema/Contribute/ContributionMyPayLog.xml b/xml/schema/Contribute/ContributionMyPayLog.xml index 4383a1279..e6310b1df 100644 --- a/xml/schema/Contribute/ContributionMyPayLog.xml +++ b/xml/schema/Contribute/ContributionMyPayLog.xml @@ -34,6 +34,16 @@ 4.0 SET NULL + + uid + uid + varchar + 255 + NULL + MyPay uid + The uid in mypay transaction. + 4.0 + url url @@ -49,7 +59,7 @@ service API CMD Name varchar 255 - Request URL + Request Command 4.0 @@ -72,6 +82,11 @@ Request Return Data 4.0 + + UI_uid + uid + 4.0 + UI_url url