Commit 655e2de0 authored by mattwire's avatar mattwire
Browse files

Merge branch 'fixnogeteventtype' into 'master'

Fix retrieving eventType for webhooks

See merge request !4
parents 4a48d113 bc9aed8c
......@@ -53,8 +53,12 @@ class CRM_Core_Payment_AuthNetIPN extends CRM_Core_Payment_BaseIPN {
$webhook = new AuthnetWebhook(CRM_Core_Payment_AuthorizeNetCommon::getSignature($this->_paymentProcessor->getPaymentProcessor()), $ipnData);
if ($webhook->isValid()) {
// Get the transaction ID
$this->eventType = $webhook->eventType;
$this->trxnId = $webhook->payload->id;
}
else {
$this->exception('Webhook not valid');
}
parent::__construct();
}
......@@ -72,45 +76,47 @@ class CRM_Core_Payment_AuthNetIPN extends CRM_Core_Payment_BaseIPN {
CRM_Core_Payment_AuthorizeNetCommon::getTransactionKey($this->_paymentProcessor->getPaymentProcessor()),
$this->_paymentProcessor->getIsTestMode() ? AuthnetApiFactory::USE_DEVELOPMENT_SERVER : AuthnetApiFactory::USE_PRODUCTION_SERVER
);
/** @var AuthnetWebhooksResponse $response */
/** @var \JohnConde\Authnet\AuthnetJsonResponse $response */
$response = $request->getTransactionDetailsRequest(['transId' => $this->getTransactionId()]);
if ($response->messages->resultCode !== 'Ok') {
$this->exception('Bad response from getTransactionDetailsRequest in IPN handler');
$this->exception('Bad response from getTransactionDetailsRequest in IPN handler. ' . $response->getErrorText());
}
// Set parameters required for IPN functions
if ($this->getParamFromResponse($response, 'is_recur')) {
$this->contribution_recur_id = $this->getRecurringContributionIDFromSubscriptionID($this->getParamFromResponse($response, 'subscription_id'));
}
$this->event_type = $response->getEventType();
$this->event_date = date('YmdHis', strtotime($response->getEventDate()));
$this->event_date = $this->getParamFromResponse($response, 'trxn_date');
// Process the event
switch ($response->getEventType()) {
switch ($this->eventType) {
case 'net.authorize.payment.authcapture.created':
// Notifies you that an authorization and capture transaction was created.
if ($this->getParamFromResponse($response, 'is_recur')) {
$contributionID = $this->getContributionIDFromInvoiceID($this->getParamFromResponse($response, 'invoice_id'));
$contribution = civicrm_api3('Contribution', 'getsingle', ['id' => $contributionID]);
$pendingStatusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending');
if ($contribution['contribution_status_id'] == $pendingStatusId) {
$params = [
'contribution_id' => $this->getContributionIDFromInvoiceID($this->getParamFromResponse($response, 'invoice_id')),
'contribution_id' => $contributionID,
'trxn_id' => $this->getParamFromResponse($response, 'transaction_id'),
'order_reference' => $this->getParamFromResponse($response, 'invoice_id'),
'receive_date' => $this->event_date,
'contribution_recur_id' => $this->contribution_recur_id,
'payment_processor_transaction_id' => $this->getParamFromResponse($response, 'transaction_id'),
'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'),
'trxn_date' => $this->event_date,
'total_amount' => $this->getParamFromResponse($response, 'total_amount'),
];
$this->repeatContribution($params);
$this->updateContributionCompleted($params);
}
else {
elseif ($this->getParamFromResponse($response, 'is_recur')) {
$params = [
'contribution_id' => $this->getContributionIDFromInvoiceID($this->getParamFromResponse($response, 'invoice_id')),
'trxn_id' => $this->getParamFromResponse($response, 'transaction_id'),
'order_reference' => $this->getParamFromResponse($response, 'invoice_id'),
'trxn_date' => $this->event_date,
'receive_date' => $this->event_date,
'contribution_recur_id' => $this->contribution_recur_id,
'payment_processor_transaction_id' => $this->getParamFromResponse($response, 'transaction_id'),
'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'),
'total_amount' => $this->getParamFromResponse($response, 'total_amount'),
];
$this->updateContributionCompleted($params);
$this->repeatContribution($params);
}
break;
......@@ -182,7 +188,7 @@ class CRM_Core_Payment_AuthNetIPN extends CRM_Core_Payment_BaseIPN {
/**
* Retrieve parameters from IPN response
*
* @param AuthnetWebhooksResponse $response
* @param \JohnConde\Authnet\AuthnetJsonResponse $response
* @param string $param
*
* @return mixed
......@@ -208,6 +214,9 @@ class CRM_Core_Payment_AuthNetIPN extends CRM_Core_Payment_BaseIPN {
case 'subscription_payment_number':
return $response->transaction->subscription->payNum;
case 'trxn_date':
return date('YmdHis', strtotime($response->transaction->submitTimeLocal));
}
}
......@@ -221,10 +230,22 @@ class CRM_Core_Payment_AuthNetIPN extends CRM_Core_Payment_BaseIPN {
* @throws \CiviCRM_API3_Exception
*/
protected function getContributionIDFromInvoiceID($invoiceID) {
$contribution = civicrm_api3('Contribution', 'get', [
'trxn_id' => ['LIKE' => "{$invoiceID}%"],
// invoiceNumber (refID) should be set on trxn_id of matching contribution
$contributionParams = [
'trxn_id' => $invoiceID,
'options' => ['limit' => 1, 'sort' => "id DESC"],
]);
'contribution_test' => $this->_paymentProcessor->getIsTestMode(),
];
$contribution = civicrm_api3('Contribution', 'get', $contributionParams);
// But if it is not we derived from first 20 chars of invoice_id so check there
if (empty($contribution['id'])) {
unset($contributionParams['trxn_id']);
$contributionParams['invoice_id'] = ['LIKE' => "{$invoiceID}%"];
}
$contribution = civicrm_api3('Contribution', 'get', $contributionParams);
// We can't do anything without a matching contribution!
if (empty($contribution['id'])) {
$this->exception("Could not find matching contribution for invoice ID: {$invoiceID}");
}
......
......@@ -196,11 +196,11 @@ abstract class CRM_Core_Payment_AuthorizeNetCommon extends CRM_Core_Payment {
$tresponse = $response->getTransactionResponse();
if ($response->getMessages()->getResultCode() == "Ok") {
$this->setPaymentProcessorOrderID($response->getRefId());
if (!$tresponse) {
$this->handleError(NULL, 'No transaction response returned', $params['error_url']);
}
$this->setPaymentProcessorOrderID($tresponse->getTransId());
$this->setPaymentProcessorTrxnID($tresponse->getTransId());
switch ($tresponse->getResponseCode()) {
case self::RESPONSECODE_APPROVED:
......@@ -478,6 +478,8 @@ abstract class CRM_Core_Payment_AuthorizeNetCommon extends CRM_Core_Payment {
}
if ($response->getMessages()->getResultCode() == "Ok") {
$this->setPaymentProcessorOrderID($response->getRefId());
$recurParams = [
'id' => $params['contributionRecurID'],
'trxn_id' => $response->getSubscriptionId(),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment