Skip to content

Commit

Permalink
Added Transaction id posting for Auth.Net
Browse files Browse the repository at this point in the history
Order will also update payment info if transaction data comes later
(usually due to settings)
  • Loading branch information
rbpotter committed Oct 20, 2015
1 parent 3e5e10c commit f69b600
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 14 deletions.
76 changes: 66 additions & 10 deletions www/magento/app/code/community/Signifyd/Connect/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

class Signifyd_Connect_Helper_Data extends Mage_Core_Helper_Abstract
{
const UNPROCESSED_STATUS = 0;
const CASE_CREATED_STATUS = 1;
const TRANSACTION_SENT_STATUS = 2;

public function getProducts($quote)
{
$products = array();
Expand Down Expand Up @@ -160,6 +164,11 @@ public function getCvvResponse($payment)
return null;
}

private function getTransactionId($payment)
{
return $payment->getCcTransId();
}

public function getPurchase($order)
{
$purchase = array();
Expand All @@ -174,7 +183,7 @@ public function getPurchase($order)
$purchase['shippingPrice'] = floatval($order->getShippingAmount());
$purchase['products'] = $this->getProducts($order);
$purchase['paymentGateway'] = $payment->getMethod();
$purchase['transactionId'] = $payment->getTransactionId();
$purchase['transactionId'] = $this->getTransactionId($payment);

$purchase['avsResponseCode'] = $this->getAvsResponse($payment);
$purchase['cvvResponseCode'] = $this->getCvvResponse($payment);
Expand Down Expand Up @@ -369,6 +378,43 @@ public function getAuth()
return Mage::getStoreConfig('signifyd_connect/settings/key');
}

public function sendOrderUpdateToSignifyd($order)
{
if ($order && $order->getId()) {
$case = Mage::getModel('signifyd_connect/case')->load($order->getIncrementId());
$caseId = $case->getCode();

$updateData = array();
$payment = $order->getPayment();

// These are the only supported update fields
$purchase = array();
$purchase['paymentGateway'] = $payment->getMethod();
$purchase['transactionId'] = $this->getTransactionId($payment);
$purchase['avsResponseCode'] = $this->getAvsResponse($payment);
$purchase['cvvResponseCode'] = $this->getCvvResponse($payment);
$updateData['purchase'] = $purchase;

$data = json_encode($updateData);

$response = $this->request($this->getUrl() . "/$caseId", $data, $this->getAuth(), 'application/json', null, true);

try {
$response_code = $response->getHttpCode();

if (substr($response_code, 0, 1) == '2') {
$case->setUpdated(strftime('%Y-%m-%d %H:%M:%S', time()));
$case->setTransactionId($updateData['purchase']['transactionId']);
$case->save();
return "sent";
}
} catch (Exception $e) {
Mage::log($e->__toString(), null, 'signifyd_connect.log');
return "error";
}
}
}

public function bulkSend($controller)
{
try {
Expand Down Expand Up @@ -404,8 +450,10 @@ public function bulkSend($controller)
public function buildAndSendOrderToSignifyd($order, $forceSend = false)
{
if ($order && $order->getId()) {
if ($this->isProcessed($order) && !$forceSend) {
return "exists";
$processStatus = $this->processedStatus($order);
if ($processStatus > 0 && !$forceSend) {
if($processStatus == self::TRANSACTION_SENT_STATUS) return "exists";
else return $this->sendOrderUpdateToSignifyd($order);
}

$payments = $order->getPaymentsCollection();
Expand Down Expand Up @@ -440,6 +488,7 @@ public function buildAndSendOrderToSignifyd($order, $forceSend = false)
$case_object = Mage::getModel('signifyd_connect/case')->load($case_object->getOrderIncrement());
$case_object->setUpdated(strftime('%Y-%m-%d %H:%M:%S', time()));
$case_object->setCode($response_data['investigationId']);
$case_object->setTransactionId($case['purchase']['transactionId']);
$case_object->save();
return "sent";
}
Expand Down Expand Up @@ -504,16 +553,20 @@ public function getStoreUrl()
return Mage::getBaseUrl();
}

public function isProcessed($order)
public function processedStatus($order)
{
$case = Mage::getModel('signifyd_connect/case')->load($order->getIncrementId());
if ($case->getId())

if ($case->getTransactionId)
{
return true;
return self::TRANSACTION_SENT_STATUS;
}
else if ($case->getId())
{
return self::CASE_CREATED_STATUS;
}

return false;
return self::UNPROCESSED_STATUS;
}

public function markProcessed($order)
Expand All @@ -536,7 +589,8 @@ public function unmarkProcessed($order)
}
}

public function request($url, $data=null, $auth=null, $contenttype="application/x-www-form-urlencoded", $accept=null)
public function request($url, $data = null, $auth = null, $contenttype = "application/x-www-form-urlencoded",
$accept = null, $is_update = false)
{
if (Mage::getStoreConfig('signifyd_connect/log/request')) {
$authMask = preg_replace ( "/\S/", "*", $auth, strlen($auth) - 4 );
Expand Down Expand Up @@ -566,7 +620,9 @@ public function request($url, $data=null, $auth=null, $contenttype="application/
}

if ($data) {
curl_setopt($curl, CURLOPT_POST, 1);
if($is_update) curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
else curl_setopt($curl, CURLOPT_POST, 1);

$headers[] = "Content-Type: $contenttype";
$headers[] = "Content-length: " . strlen($data);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
Expand Down
17 changes: 17 additions & 0 deletions www/magento/app/code/community/Signifyd/Connect/Model/Authnet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

class Signifyd_Connect_Model_Authnet extends Mage_Paygate_Model_Authorizenet
{
protected function _registercard(varien_object $response, mage_sales_model_order_payment $payment)
{
Mage::log(">: ".$response->getTransactionId(), null, 'signifyd_connect.log');
$card = parent::_registercard($response,$payment);
$card->setCcAvsResultCode($response->getAvsResultCode());
$card->setCcResponseCode($response->getCardCodeResponseCode());
$payment->setCcAvsStatus($response->getAvsResultCode());
$payment->setCcCidStatus($response->getCardCodeResponseCode());
$payment->setCcTransId($response->getTransactionId());
$payment->getCcTransId();
return $card;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
<config>
<modules>
<Signifyd_Connect>
<version>3.10.1</version>
<version>3.12.0</version>
</Signifyd_Connect>
</modules>
<global>
<models>
<paygate>
<rewrite>
<authorizenet>Signifyd_Connect_Model_Authnet</authorizenet>
</rewrite>
</paygate>
<signifyd_connect>
<class>Signifyd_Connect_Model</class>
<resourceModel>signifyd_connect_resource</resourceModel>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

$this->startSetup();
$this->register();
$this->run("
DROP TABLE IF EXISTS `{$this->getTable('signifyd_connect_case')}`;
DROP TABLE IF EXISTS `{$this->getTable('signifyd_connect_retries')}`;
CREATE TABLE IF NOT EXISTS `{$this->getTable('signifyd_connect_case')}` (
`order_increment` varchar(255) NOT NULL,
`signifyd_status` varchar(64) NOT NULL DEFAULT 'PENDING',
`code` varchar(255) NOT NULL,
`score` float DEFAULT NULL,
`guarantee` VARCHAR( 64 ) NOT NULL DEFAULT 'N/A',
`entries` text NOT NULL,
`transaction_id` varchar(64) NULL,
`created` timestamp NULL DEFAULT NULL,
`updated` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`order_increment`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
CREATE TABLE IF NOT EXISTS `{$this->getTable('signifyd_connect_retries')}` (
`order_increment` varchar(255) NOT NULL,
`created` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`order_increment`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
");
$this->endSetup();
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

try {
$this->startSetup();
$this->endSetup();
} catch (Exception $e) {
Mage::log('Signifyd_Connect upgrade: ' . $e->__toString(), null, 'signifyd_connect.log');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

try {
$this->startSetup();
$this->endSetup();
} catch (Exception $e) {
Mage::log('Signifyd_Connect upgrade: ' . $e->__toString(), null, 'signifyd_connect.log');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

try {
$this->startSetup();
$this->endSetup();
} catch (Exception $e) {
Mage::log('Signifyd_Connect upgrade: ' . $e->__toString(), null, 'signifyd_connect.log');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

try {
$this->startSetup();
$this->run("
ALTER TABLE `{$this->getTable('signifyd_connect_case')}` ADD `transaction_id` VARCHAR(64) NULL;
CREATE TABLE IF NOT EXISTS `{$this->getTable('signifyd_connect_retries')}` (
`order_increment` varchar(255) NOT NULL,
`created` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`order_increment`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
");
$this->endSetup();
} catch (Exception $e) {
Mage::log('Signifyd_Connect upgrade: ' . $e->__toString(), null, 'signifyd_connect.log');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

try {
$this->startSetup();
$this->endSetup();
} catch (Exception $e) {
Mage::log('Signifyd_Connect upgrade: ' . $e->__toString(), null, 'signifyd_connect.log');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

try {
$this->startSetup();
$this->endSetup();
} catch (Exception $e) {
Mage::log('Signifyd_Connect upgrade: ' . $e->__toString(), null, 'signifyd_connect.log');
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,17 @@ public function testRunOnce()

$helper->unmarkProcessed($order);

$is_processed = $helper->isProcessed($order);
$is_processed = $helper->processedStatus($order);
$this->assertEquals(0, $is_processed);

$helper->markProcessed($order);

$is_processed = $helper->isProcessed($order);
$is_processed = $helper->processedStatus($order);
$this->assertEquals(1, $is_processed);

$helper->unmarkProcessed($order);

$is_processed = $helper->isProcessed($order);
$is_processed = $helper->processedStatus($order);
$this->assertEquals(0, $is_processed);
}

Expand Down

0 comments on commit f69b600

Please sign in to comment.