Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
Adyen Magento2
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Shared Libs
Adyen Magento2
Commits
dd1334c7
Commit
dd1334c7
authored
Aug 21, 2017
by
Aleffio
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PW-243: reworked Cron.php to update ExpiryDate on existing billing agreements.
parent
f8b022c4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
87 additions
and
109 deletions
+87
-109
Model/Cron.php
Model/Cron.php
+87
-109
No files found.
Model/Cron.php
View file @
dd1334c7
...
...
@@ -784,133 +784,111 @@ class Cron
}
}
break
;
case
Notification
::
RECURRING_CONTRACT
:
case
Notification
::
RECURRING_CONTRACT
:
// storedReferenceCode
$recurringDetailReference
=
$this
->
_pspReference
;
// check if there is already a BillingAgreement
$billingAgreement
=
$this
->
_billingAgreementFactory
->
create
();
$billingAgreement
->
load
(
$recurringDetailReference
,
'reference_id'
);
if
(
$billingAgreement
&&
$billingAgreement
->
getAgreementId
()
>
0
&&
$billingAgreement
->
isValid
())
{
$storeId
=
$this
->
_order
->
getStoreId
();
$customerReference
=
$this
->
_order
->
getCustomerId
();
$listRecurringContracts
=
null
;
$this
->
_adyenLogger
->
addAdyenNotificationCronjob
(
__
(
'CustomerReference is: %1 and storeId is %2 and RecurringDetailsReference is %3'
,
$customerReference
,
$storeId
,
$recurringDetailReference
)
);
try
{
$listRecurringContracts
=
$this
->
_adyenPaymentRequest
->
getRecurringContractsForShopper
(
$customerReference
,
$storeId
);
$contractDetail
=
null
;
// get current Contract details and get list of all current ones
$recurringReferencesList
=
[];
try
{
$billingAgreement
->
addOrderRelation
(
$this
->
_order
);
$billingAgreement
->
setStatus
(
$billingAgreement
::
STATUS_ACTIVE
);
$billingAgreement
->
setIsObjectChanged
(
true
);
$this
->
_order
->
addRelatedObject
(
$billingAgreement
);
$message
=
__
(
'Used existing billing agreement #%s.'
,
$billingAgreement
->
getReferenceId
());
}
catch
(
Exception
$e
)
{
// could be that it is already linked to this order
$message
=
__
(
'Used existing billing agreement #%s.'
,
$billingAgreement
->
getReferenceId
());
if
(
!
$listRecurringContracts
)
{
throw
new
\Exception
(
"Empty list recurring contracts"
);
}
// Find the reference on the list
foreach
(
$listRecurringContracts
as
$rc
)
{
$recurringReferencesList
[]
=
$rc
[
'recurringDetailReference'
];
if
(
isset
(
$rc
[
'recurringDetailReference'
])
&&
$rc
[
'recurringDetailReference'
]
==
$recurringDetailReference
)
{
$contractDetail
=
$rc
;
}
}
}
else
{
$this
->
_order
->
getPayment
()
->
setBillingAgreementData
(
[
'billing_agreement_id'
=>
$recurringDetailReference
,
'method_code'
=>
$this
->
_order
->
getPayment
()
->
getMethodCode
(),
]
);
// create new object
$billingAgreement
=
$this
->
_billingAgreementFactory
->
create
();
$billingAgreement
->
setStoreId
(
$this
->
_order
->
getStoreId
());
$billingAgreement
->
importOrderPayment
(
$this
->
_order
->
getPayment
());
if
(
$contractDetail
==
null
)
{
$this
->
_adyenLogger
->
addAdyenNotificationCronjob
(
print_r
(
$listRecurringContracts
,
1
));
$message
=
__
(
'Failed to create billing agreement for this order '
.
'(listRecurringCall did not contain contract)'
);
throw
new
\Exception
(
$message
);
}
// get all data for this contract by doing a listRecurringCall
$customerReference
=
$billingAgreement
->
getCustomerReference
();
$storeId
=
$billingAgreement
->
getStoreId
();
$billingAgreements
=
$this
->
_billingAgreementCollectionFactory
->
create
();
$billingAgreements
->
addFieldToFilter
(
'customer_id'
,
$customerReference
);
/*
* for quest checkout users we can't save this in the billing agreement
* because it is linked to customer
*/
if
(
$customerReference
&&
$storeId
)
{
// Get collection and update existing agreements
$listRecurringContracts
=
null
;
try
{
$listRecurringContracts
=
$this
->
_adyenPaymentRequest
->
getRecurringContractsForShopper
(
$customerReference
,
$storeId
foreach
(
$billingAgreements
as
$updateBillingAgreement
)
{
if
(
!
in_array
(
$updateBillingAgreement
->
getReferenceId
(),
$recurringReferencesList
))
{
$updateBillingAgreement
->
setStatus
(
\Adyen\Payment\Model\Billing\Agreement
::
STATUS_CANCELED
);
$updateBillingAgreement
->
save
();
}
else
{
$updateBillingAgreement
->
setStatus
(
\Adyen\Payment\Model\Billing\Agreement
::
STATUS_ACTIVE
);
}
catch
(
\Exception
$exception
)
{
$this
->
_adyenLogger
->
addAdyenNotificationCronjob
(
$exception
->
getMessage
());
$updateBillingAgreement
->
save
();
}
}
$contractDetail
=
null
;
// get current Contract details and get list of all current ones
$recurringReferencesList
=
[];
if
(
$listRecurringContracts
)
{
foreach
(
$listRecurringContracts
as
$rc
)
{
$recurringReferencesList
[]
=
$rc
[
'recurringDetailReference'
];
if
(
isset
(
$rc
[
'recurringDetailReference'
])
&&
$rc
[
'recurringDetailReference'
]
==
$recurringDetailReference
)
{
$contractDetail
=
$rc
;
}
}
}
// Get or create billing agreement
$billingAgreement
=
$this
->
_billingAgreementFactory
->
create
();
$billingAgreement
->
load
(
$recurringDetailReference
,
'reference_id'
);
$message
=
__
(
'Updated billing agreement #%1.'
,
$recurringDetailReference
);
// check if BA exists
if
(
!
(
$billingAgreement
&&
$billingAgreement
->
getAgreementId
()
>
0
&&
$billingAgreement
->
isValid
()))
{
// create new
$this
->
_adyenLogger
->
addAdyenNotificationCronjob
(
"Creating new Billing Agreement"
);
$this
->
_order
->
getPayment
()
->
setBillingAgreementData
(
[
'billing_agreement_id'
=>
$recurringDetailReference
,
'method_code'
=>
$this
->
_order
->
getPayment
()
->
getMethodCode
(),
]
);
if
(
$contractDetail
!=
null
)
{
// update status of all the current saved agreements in magento
$billingAgreements
=
$this
->
_billingAgreementCollectionFactory
->
create
();
$billingAgreements
->
addFieldToFilter
(
'customer_id'
,
$customerReference
);
// get collection
foreach
(
$billingAgreements
as
$updateBillingAgreement
)
{
if
(
!
in_array
(
$updateBillingAgreement
->
getReferenceId
(),
$recurringReferencesList
))
{
$updateBillingAgreement
->
setStatus
(
\Adyen\Payment\Model\Billing\Agreement
::
STATUS_CANCELED
);
$updateBillingAgreement
->
save
();
}
else
{
$updateBillingAgreement
->
setStatus
(
\Adyen\Payment\Model\Billing\Agreement
::
STATUS_ACTIVE
);
$updateBillingAgreement
->
save
();
}
}
// add this billing agreement
$billingAgreement
->
parseRecurringContractData
(
$contractDetail
);
if
(
$billingAgreement
->
isValid
())
{
$message
=
__
(
'Created billing agreement #%1.'
,
$billingAgreement
->
getReferenceId
());
// save into sales_billing_agreement_order
$billingAgreement
->
addOrderRelation
(
$this
->
_order
);
// add to order to save agreement
$this
->
_order
->
addRelatedObject
(
$billingAgreement
);
}
else
{
$message
=
__
(
'Failed to create billing agreement for this order.'
);
}
$billingAgreement
=
$this
->
_billingAgreementFactory
->
create
();
$billingAgreement
->
setStoreId
(
$this
->
_order
->
getStoreId
());
$billingAgreement
->
importOrderPayment
(
$this
->
_order
->
getPayment
());
$message
=
__
(
'Created billing agreement #%1.'
,
$recurringDetailReference
);
}
else
{
$this
->
_adyenLogger
->
addAdyenNotificationCronjob
(
"Using existing Billing Agreement"
);
$billingAgreement
->
setIsObjectChanged
(
true
);
}
// Populate billing agreement data
$billingAgreement
->
parseRecurringContractData
(
$contractDetail
);
if
(
$billingAgreement
->
isValid
())
{
}
else
{
$this
->
_adyenLogger
->
addAdyenNotificationCronjob
(
'Failed to create billing agreement for this order '
.
'(listRecurringCall did not contain contract)'
);
$this
->
_adyenLogger
->
addAdyenNotificationCronjob
(
__
(
'recurringDetailReference in notification is %1'
,
$recurringDetailReference
)
);
$this
->
_adyenLogger
->
addAdyenNotificationCronjob
(
__
(
'CustomerReference is: %1 and storeId is %2'
,
$customerReference
,
$storeId
)
);
$this
->
_adyenLogger
->
addAdyenNotificationCronjob
(
print_r
(
$listRecurringContracts
,
1
));
$message
=
__
(
'Failed to create billing agreement for this order '
.
'(listRecurringCall did not contain contract)'
);
}
// save into sales_billing_agreement_order
$billingAgreement
->
addOrderRelation
(
$this
->
_order
);
$comment
=
$this
->
_order
->
addStatusHistoryComment
(
$message
);
$this
->
_order
->
addRelatedObject
(
$comment
);
// add to order to save agreement
$this
->
_order
->
addRelatedObject
(
$billingAgreement
);
}
else
{
$message
=
__
(
'Failed to create billing agreement for this order.'
);
throw
new
\Exception
(
$message
);
}
}
catch
(
\Exception
$exception
)
{
$message
=
$exception
->
getMessage
();
}
$this
->
_adyenLogger
->
addAdyenNotificationCronjob
(
$message
);
$comment
=
$this
->
_order
->
addStatusHistoryComment
(
$message
);
$this
->
_order
->
addRelatedObject
(
$comment
);
break
;
default
:
$this
->
_adyenLogger
->
addAdyenNotificationCronjob
(
...
...
@@ -976,7 +954,7 @@ class Cron
}
}
else
{
$this
->
_adyenLogger
->
addAdyenNotificationCronjob
(
'Did not create a credit memo for this order beca
su
e refund is done through Magento'
'Did not create a credit memo for this order beca
us
e refund is done through Magento'
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment