Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
commerce_datatrans-3507218
Manage
Activity
Members
Labels
Plan
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Issue forks
commerce_datatrans-3507218
Commits
6d6d8425
Commit
6d6d8425
authored
8 years ago
by
Primoz Hmeljak
Browse files
Options
Downloads
Patches
Plain Diff
by Primsi: create the payment in onNotify.
parent
98793447
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/DatatransHelper.php
+3
-3
3 additions, 3 deletions
src/DatatransHelper.php
src/Plugin/Commerce/PaymentGateway/Datatrans.php
+31
-59
31 additions, 59 deletions
src/Plugin/Commerce/PaymentGateway/Datatrans.php
src/PluginForm/DatatransForm.php
+4
-9
4 additions, 9 deletions
src/PluginForm/DatatransForm.php
with
38 additions
and
71 deletions
src/DatatransHelper.php
+
3
−
3
View file @
6d6d8425
...
...
@@ -42,14 +42,14 @@ class DatatransHelper {
* Transaction amount in cents or smallest available unit of the currency.
* @param string $currency
* Transaction currency – ISO Character Code (CHF, USD, EUR etc.).
* @param string $
payment
_id
* @param string $
order
_id
* Our reference number.
*
* @return string
* The computed hash.
*/
public
static
function
generateSign
(
$hmac_key
,
$merchant_id
,
$amount
,
$currency
,
$
payment
_id
)
{
$hmac_data
=
$merchant_id
.
$amount
.
$currency
.
$
payment
_id
;
public
static
function
generateSign
(
$hmac_key
,
$merchant_id
,
$amount
,
$currency
,
$
order
_id
)
{
$hmac_data
=
$merchant_id
.
$amount
.
$currency
.
$
order
_id
;
return
hash_hmac
(
'md5'
,
$hmac_data
,
pack
(
'H*'
,
$hmac_key
));
}
...
...
This diff is collapsed.
Click to expand it.
src/Plugin/Commerce/PaymentGateway/Datatrans.php
+
31
−
59
View file @
6d6d8425
...
...
@@ -3,6 +3,7 @@
namespace
Drupal\commerce_datatrans\Plugin\Commerce\PaymentGateway
;
use
Drupal\commerce_order
\Entity\OrderInterface
;
use
Drupal\commerce_payment
\Exception\PaymentGatewayException
;
use
Drupal\commerce_payment
\PaymentMethodTypeManager
;
use
Drupal\commerce_payment
\PaymentTypeManager
;
use
Drupal\commerce_payment
\Plugin\Commerce\PaymentGateway\OffsitePaymentGatewayBase
;
...
...
@@ -207,45 +208,19 @@ class Datatrans extends OffsitePaymentGatewayBase {
// @todo Add examples of request validation.
$post_data
=
$request
->
request
->
all
();
if
(
$payment
=
\Drupal
::
entityTypeManager
()
->
getStorage
(
'commerce_payment'
)
->
load
(
$post_data
[
'refno'
]))
{
$currency_code
=
$payment
->
getAmount
()
->
getCurrencyCode
();
/** @var \Drupal\commerce_price\Entity\CurrencyInterface $currency */
$currency
=
Currency
::
load
(
$currency_code
);
$a
=
$post_data
[
'amount'
]
/
pow
(
10
,
$currency
->
getFractionDigits
());
$price
=
new
Price
((
string
)
$a
,
$currency_code
);
// @todo Missing 'state' here.
$payment
->
setAmount
(
$price
);
$payment
->
setRemoteId
(
$post_data
[
'acqAuthorizationCode'
]);
$payment
->
setRemoteState
(
$post_data
[
'status'
]);
$payment
->
setAuthorizedTime
(
REQUEST_TIME
);
// $payment->save();
if
(
isset
(
$post_data
[
'status'
]))
{
switch
(
$post_data
[
'status'
])
{
case
'success'
:
drupal_set_message
(
$this
->
t
(
'Payment was processed successfully'
));
break
;
default
:
// @todo Should we give more info.
drupal_set_message
(
$this
->
t
(
'There was a problem while processing your payment.'
),
'warning'
);
throw
new
PaymentGatewayException
();
}
}
// @todo Missing 'state' here.
drupal_set_message
(
'Payment was processed'
);
}
/**
* {@inheritdoc}
*/
public
function
onCancel
(
OrderInterface
$order
,
Request
$request
)
{
drupal_set_message
(
$this
->
t
(
'You have canceled checkout at @gateway but may resume the checkout process here when you are ready.'
,
[
'@gateway'
=>
$this
->
getDisplayLabel
(),
]));
}
/**
* {@inheritdoc}
*/
public
function
getNotifyUrl
()
{
return
Url
::
fromRoute
(
'commerce_payment.notify'
,
[
'commerce_payment_gateway'
=>
$this
->
entityId
,
],
[
'absolute'
=>
TRUE
]);
}
/**
...
...
@@ -258,43 +233,40 @@ class Datatrans extends OffsitePaymentGatewayBase {
return
new
Response
(
''
,
400
);
}
/** @var \Drupal\commerce_
payment
\entity\
Payment
Interface $
payment
*/
$
payment
=
$this
->
entityTypeManager
->
getStorage
(
'commerce_
payment
'
)
->
load
(
$post_data
[
'refno'
]);
if
(
!
$
payment
)
{
/** @var \Drupal\commerce_
order
\entity\
Order
Interface $
order
*/
$
order
=
$this
->
entityTypeManager
->
getStorage
(
'commerce_
order
'
)
->
load
(
$post_data
[
'refno'
]);
if
(
!
$
order
)
{
return
new
Response
(
''
,
400
);
}
switch
(
$post_data
[
'status'
])
{
case
'success'
:
$currency_code
=
$payment
->
getAmount
()
->
getCurrencyCode
();
/** @var \Drupal\commerce_price\Entity\CurrencyInterface $currency */
$currency
=
Currency
::
load
(
$currency_code
);
$datatrans_amount
=
$post_data
[
'amount'
]
/
pow
(
10
,
$currency
->
getFractionDigits
());
$price
=
new
Price
((
string
)
$datatrans_amount
,
$currency_code
);
// @todo Missing 'state' here.
$payment
->
setAmount
(
$price
);
// Documentation states we should use acqAuthorizationCode but that
// number is not visible in their ui.
$payment
->
setRemoteId
(
$post_data
[
'authorizationCode'
]);
$payment
->
setRemoteState
(
$post_data
[
'status'
]);
$payment
->
setAuthorizedTime
(
REQUEST_TIME
);
$payment_storage
=
$this
->
entityTypeManager
->
getStorage
(
'commerce_payment'
);
$payment
=
$payment_storage
->
create
([
'state'
=>
'authorization'
,
// @todo Should we rather set price we get from datatrans?
'amount'
=>
$order
->
getTotalPrice
(),
'payment_gateway'
=>
$this
->
entityId
,
'order_id'
=>
$order
->
id
(),
'test'
=>
$this
->
getMode
()
==
'test'
,
'remote_id'
=>
$post_data
[
'authorizationCode'
],
'remote_state'
=>
$post_data
[
'responseMessage'
],
'authorized'
=>
REQUEST_TIME
,
]);
$payment
->
save
();
break
;
case
'error'
:
$this
->
logger
->
error
(
'The payment gateway returned the error code %code (%details) for payment %
payment
_id'
,
[
$this
->
logger
->
error
(
'The payment gateway returned the error code %code (%details) for payment %
order
_id'
,
[
'%code'
=>
$post_data
[
'errorCode'
],
'%details'
=>
$post_data
[
'errorDetail'
],
'%
payment
_id'
=>
$
payment
->
id
(),
'%
order
_id'
=>
$
order
->
id
(),
]);
break
;
case
'cancel'
:
$this
->
logger
->
info
(
'The user canceled the authorisation process for payment %
payment
_id'
,
[
'%
payment
_id'
=>
$
payment
->
id
(),
$this
->
logger
->
info
(
'The user canceled the authorisation process for payment %
order
_id'
,
[
'%
order
_id'
=>
$
order
->
id
(),
]);
break
;
}
...
...
This diff is collapsed.
Click to expand it.
src/PluginForm/DatatransForm.php
+
4
−
9
View file @
6d6d8425
...
...
@@ -20,14 +20,9 @@ class DatatransForm extends PaymentOffsiteForm {
/** @var \Drupal\commerce_payment\Entity\PaymentInterface $payment */
$payment
=
$this
->
entity
;
if
(
$payment
->
isNew
())
{
$payment
->
save
();
$this
->
setEntity
(
$payment
);
}
$gateway
=
$payment
->
getPaymentGateway
()
->
getPlugin
();
$gateway_config
=
$gateway
->
getConfiguration
();
$order
=
$payment
->
getOrder
();
$currency_code
=
$payment
->
getAmount
()
->
getCurrencyCode
();
/** @var \Drupal\commerce_price\Entity\CurrencyInterface $currency */
...
...
@@ -35,8 +30,8 @@ class DatatransForm extends PaymentOffsiteForm {
$data
=
[
'merchantId'
=>
$gateway_config
[
'merchant_id'
],
'amount'
=>
intval
(
$
payment
->
getAmount
()
->
getNumber
()
*
pow
(
10
,
$currency
->
getFractionDigits
())),
'refno'
=>
$
payment
->
id
(),
'amount'
=>
intval
(
$
order
->
getTotalPrice
()
->
getNumber
()
*
pow
(
10
,
$currency
->
getFractionDigits
())),
'refno'
=>
$
order
->
id
(),
'sign'
=>
NULL
,
'currency'
=>
$currency_code
,
'successUrl'
=>
$form
[
'#return_url'
],
...
...
@@ -47,7 +42,7 @@ class DatatransForm extends PaymentOffsiteForm {
if
(
$gateway_config
[
'security_level'
]
==
2
)
{
// Generates the sign.
$payment
[
'sign'
]
=
DatatransHelper
::
generateSign
(
$gateway_config
[
'hmac_key'
],
$gateway_config
[
'merchant_id'
],
$
payment
->
getAmount
()
->
getNumber
(),
$currency_code
,
$
payment
->
id
());
$payment
[
'sign'
]
=
DatatransHelper
::
generateSign
(
$gateway_config
[
'hmac_key'
],
$gateway_config
[
'merchant_id'
],
$
order
->
getTotalPrice
()
->
getNumber
(),
$currency_code
,
$
order
->
id
());
}
return
$this
->
buildRedirectForm
(
$form
,
$form_state
,
$gateway_config
[
'service_url'
],
$data
,
static
::
REDIRECT_POST
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment