Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
C
commerce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Custom Issue Tracker
Custom Issue Tracker
Labels
Merge Requests
24
Merge Requests
24
Requirements
Requirements
List
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Analytics
Analytics
Code Review
Insights
Issue
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
project
commerce
Commits
f9875d38
Commit
f9875d38
authored
Dec 03, 2016
by
mglaman
Committed by
bojanz
Dec 21, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue
#2785591
: Initial implementation of an off-site API.
parent
e4d6636f
Changes
21
Hide whitespace changes
Inline
Side-by-side
Showing
21 changed files
with
858 additions
and
57 deletions
+858
-57
modules/checkout/src/Plugin/Commerce/CheckoutFlow/CheckoutFlowBase.php
...out/src/Plugin/Commerce/CheckoutFlow/CheckoutFlowBase.php
+18
-5
modules/checkout/src/Plugin/Commerce/CheckoutFlow/CheckoutFlowInterface.php
...rc/Plugin/Commerce/CheckoutFlow/CheckoutFlowInterface.php
+10
-0
modules/checkout/src/Plugin/Commerce/CheckoutFlow/MultistepDefault.php
...out/src/Plugin/Commerce/CheckoutFlow/MultistepDefault.php
+1
-0
modules/payment/commerce_payment.libraries.yml
modules/payment/commerce_payment.libraries.yml
+9
-0
modules/payment/commerce_payment.routing.yml
modules/payment/commerce_payment.routing.yml
+23
-0
modules/payment/js/offiste-redirect.js
modules/payment/js/offiste-redirect.js
+23
-0
modules/payment/src/Controller/PaymentCheckoutController.php
modules/payment/src/Controller/PaymentCheckoutController.php
+53
-0
modules/payment/src/Plugin/Commerce/CheckoutPane/PaymentInformation.php
...t/src/Plugin/Commerce/CheckoutPane/PaymentInformation.php
+105
-21
modules/payment/src/Plugin/Commerce/CheckoutPane/PaymentProcess.php
...yment/src/Plugin/Commerce/CheckoutPane/PaymentProcess.php
+29
-31
modules/payment/src/Plugin/Commerce/PaymentGateway/OffsitePaymentGatewayBase.php
...gin/Commerce/PaymentGateway/OffsitePaymentGatewayBase.php
+33
-0
modules/payment/src/Plugin/Commerce/PaymentGateway/OffsitePaymentGatewayInterface.php
...ommerce/PaymentGateway/OffsitePaymentGatewayInterface.php
+50
-0
modules/payment/src/Plugin/Commerce/PaymentGateway/OffsiteRedirectPaymentGatewayInterface.php
...PaymentGateway/OffsiteRedirectPaymentGatewayInterface.php
+22
-0
modules/payment/src/Plugin/Commerce/PaymentGateway/PaymentGatewayBase.php
...src/Plugin/Commerce/PaymentGateway/PaymentGatewayBase.php
+3
-0
modules/payment/src/PluginForm/OffsitePaymentForm.php
modules/payment/src/PluginForm/OffsitePaymentForm.php
+43
-0
modules/payment/tests/src/Functional/PaymentCheckoutOffsiteRedirectTest.php
...sts/src/Functional/PaymentCheckoutOffsiteRedirectTest.php
+172
-0
modules/payment_example/commerce_payment_example.routing.yml
modules/payment_example/commerce_payment_example.routing.yml
+16
-0
modules/payment_example/config/schema/commerce_payment_example.schema.yml
...example/config/schema/commerce_payment_example.schema.yml
+6
-0
modules/payment_example/src/Controller/DummyRedirectController.php
...ayment_example/src/Controller/DummyRedirectController.php
+75
-0
modules/payment_example/src/Plugin/Commerce/PaymentGateway/OffsiteRedirectExample.php
...Plugin/Commerce/PaymentGateway/OffsiteRedirectExample.php
+107
-0
modules/payment_example/src/Plugin/Commerce/PaymentGateway/OffsiteRedirectExampleInterface.php
...mmerce/PaymentGateway/OffsiteRedirectExampleInterface.php
+9
-0
modules/payment_example/src/PluginForm/Offsite/OffsiteRedirectPaymentForm.php
...ple/src/PluginForm/Offsite/OffsiteRedirectPaymentForm.php
+51
-0
No files found.
modules/checkout/src/Plugin/Commerce/CheckoutFlow/CheckoutFlowBase.php
View file @
f9875d38
...
...
@@ -2,12 +2,14 @@
namespace
Drupal\commerce_checkout\Plugin\Commerce\CheckoutFlow
;
use
Drupal\commerce\Response\NeedsRedirectException
;
use
Drupal\Component\Utility\NestedArray
;
use
Drupal\Core\Entity\EntityTypeManagerInterface
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\Core\Plugin\ContainerFactoryPluginInterface
;
use
Drupal\Core\Plugin\PluginBase
;
use
Drupal\Core\Routing\RouteMatchInterface
;
use
Drupal\Core\Url
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
use
Symfony\Component\EventDispatcher\EventDispatcherInterface
;
...
...
@@ -147,6 +149,22 @@ abstract class CheckoutFlowBase extends PluginBase implements CheckoutFlowInterf
return
isset
(
$step_ids
[
$current_index
+
1
])
?
$step_ids
[
$current_index
+
1
]
:
NULL
;
}
/**
* {@inheritdoc}
*/
public
function
redirectToStep
(
$step_id
)
{
$this
->
order
->
checkout_step
=
$step_id
;
if
(
$step_id
==
'complete'
)
{
$transition
=
$this
->
order
->
getState
()
->
getWorkflow
()
->
getTransition
(
'place'
);
$this
->
order
->
getState
()
->
applyTransition
(
$transition
);
}
$this
->
order
->
save
();
throw
new
NeedsRedirectException
(
Url
::
fromRoute
(
'commerce_checkout.form'
,
[
'commerce_order'
=>
$this
->
order
->
id
(),
'step'
=>
$step_id
,
])
->
toString
());
}
/**
* {@inheritdoc}
*/
...
...
@@ -373,11 +391,6 @@ abstract class CheckoutFlowBase extends PluginBase implements CheckoutFlowInterf
}
// Hide the actions element if it has no buttons.
$actions
[
'#access'
]
=
isset
(
$actions
[
'previous'
])
||
isset
(
$actions
[
'next'
]);
// Once these two steps are reached, the user can't go back.
if
(
in_array
(
$this
->
stepId
,
[
'offsite_payment'
,
'complete'
]))
{
$actions
[
'#access'
]
=
FALSE
;
}
return
$actions
;
}
...
...
modules/checkout/src/Plugin/Commerce/CheckoutFlow/CheckoutFlowInterface.php
View file @
f9875d38
...
...
@@ -56,6 +56,16 @@ interface CheckoutFlowInterface extends FormInterface, ConfigurablePluginInterfa
*/
public
function
getNextStepId
();
/**
* Redirects an order to a specific step in the checkout.
*
* @param string $step_id
* The step ID to redirect to.
*
* @throws \Drupal\commerce\Response\NeedsRedirectException
*/
public
function
redirectToStep
(
$step_id
);
/**
* Gets the defined steps.
*
...
...
modules/checkout/src/Plugin/Commerce/CheckoutFlow/MultistepDefault.php
View file @
f9875d38
...
...
@@ -29,6 +29,7 @@ class MultistepDefault extends CheckoutFlowWithPanesBase {
'review'
=>
[
'label'
=>
$this
->
t
(
'Review'
),
'next_label'
=>
$this
->
t
(
'Continue to review'
),
'previous_label'
=>
$this
->
t
(
'Cancel payment'
),
'has_order_summary'
=>
TRUE
,
],
]
+
parent
::
getSteps
();
...
...
modules/payment/commerce_payment.libraries.yml
View file @
f9875d38
...
...
@@ -9,3 +9,12 @@ payment_method_icons:
css
:
theme
:
css/commerce_payment.payment_method_icons.css
:
{}
offsite_redirect
:
version
:
VERSION
js
:
js/offiste-redirect.js
:
{}
dependencies
:
-
core/jquery
-
core/drupal
-
core/drupalSettings
modules/payment/commerce_payment.routing.yml
View file @
f9875d38
...
...
@@ -62,3 +62,26 @@ entity.commerce_payment_method.collection:
parameters
:
user
:
type
:
entity:user
commerce_payment.checkout.cancel
:
path
:
'
/checkout/{commerce_order}/{step}/cancel'
defaults
:
_controller
:
'
\Drupal\commerce_payment\Controller\PaymentCheckoutController::cancelPaymentPage'
requirements
:
_custom_access
:
'
\Drupal\commerce_checkout\Controller\CheckoutController::checkAccess'
_module_dependencies
:
commerce_checkout
options
:
parameters
:
commerce_order
:
type
:
entity:commerce_order
commerce_payment.checkout.return
:
path
:
'
/checkout/{commerce_order}/{step}/return'
defaults
:
_controller
:
'
\Drupal\commerce_payment\Controller\PaymentCheckoutController::returnPaymentPage'
requirements
:
_custom_access
:
'
\Drupal\commerce_checkout\Controller\CheckoutController::checkAccess'
_module_dependencies
:
commerce_checkout
options
:
parameters
:
commerce_order
:
type
:
entity:commerce_order
modules/payment/js/offiste-redirect.js
0 → 100644
View file @
f9875d38
/**
* @file
* Defines behaviors for the payment redirect form.
*/
(
function
(
$
,
Drupal
,
drupalSettings
)
{
'
use strict
'
;
/**
* Attaches the commercePaymentRedirect behavior.
*
* @type {Drupal~behavior}
*
* @prop {Drupal~behaviorAttach} attach
* Attaches the commercePaymentRedirect behavior.
*/
Drupal
.
behaviors
.
commercePaymentRedirect
=
{
attach
:
function
(
context
)
{
$
(
'
.payment-redirect-form
'
,
context
).
find
(
'
input[type="submit"]
'
).
trigger
(
'
click
'
);
}
};
})(
jQuery
,
Drupal
,
drupalSettings
);
modules/payment/src/Controller/PaymentCheckoutController.php
0 → 100644
View file @
f9875d38
<?php
namespace
Drupal\commerce_payment\Controller
;
use
Drupal\commerce_order
\
Entity\OrderInterface
;
use
Drupal\commerce_payment
\
Plugin\Commerce\PaymentGateway\OffsitePaymentGatewayInterface
;
use
Drupal\Core\Access\AccessException
;
class
PaymentCheckoutController
{
/**
* Controller callback for offsite payments cancelled.
*
* @param \Drupal\commerce_order\Entity\OrderInterface $commerce_order
* The order.
*/
public
function
cancelPaymentPage
(
OrderInterface
$commerce_order
)
{
/** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway */
$payment_gateway
=
$commerce_order
->
payment_gateway
->
entity
;
$payment_gateway_plugin
=
$payment_gateway
->
getPlugin
();
if
(
!
$payment_gateway_plugin
instanceof
OffsitePaymentGatewayInterface
)
{
throw
new
AccessException
(
'The payment gateway for the order does not implement '
.
OffsitePaymentGatewayInterface
::
class
);
}
$payment_gateway_plugin
->
onRedirectCancel
(
$commerce_order
);
/** @var \Drupal\commerce_checkout\Entity\CheckoutFlowInterface $checkout_flow */
$checkout_flow
=
$commerce_order
->
checkout_flow
->
entity
;
$checkout_flow_plugin
=
$checkout_flow
->
getPlugin
();
$checkout_flow_plugin
->
redirectToStep
(
$checkout_flow_plugin
->
getPreviousStepId
());
}
/**
* Controller callback for offsite payments returned.
*
* @param \Drupal\commerce_order\Entity\OrderInterface $commerce_order
* The order.
*/
public
function
returnPaymentPage
(
OrderInterface
$commerce_order
)
{
/** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway */
$payment_gateway
=
$commerce_order
->
payment_gateway
->
entity
;
$payment_gateway_plugin
=
$payment_gateway
->
getPlugin
();
if
(
!
$payment_gateway_plugin
instanceof
OffsitePaymentGatewayInterface
)
{
throw
new
AccessException
(
'The payment gateway for the order does not implement '
.
OffsitePaymentGatewayInterface
::
class
);
}
$payment_gateway_plugin
->
onRedirectReturn
(
$commerce_order
);
/** @var \Drupal\commerce_checkout\Entity\CheckoutFlowInterface $checkout_flow */
$checkout_flow
=
$commerce_order
->
checkout_flow
->
entity
;
$checkout_flow_plugin
=
$checkout_flow
->
getPlugin
();
$checkout_flow_plugin
->
redirectToStep
(
$checkout_flow_plugin
->
getNextStepId
());
}
}
modules/payment/src/Plugin/Commerce/CheckoutPane/PaymentInformation.php
View file @
f9875d38
...
...
@@ -4,13 +4,16 @@ namespace Drupal\commerce_payment\Plugin\Commerce\CheckoutPane;
use
Drupal\commerce_checkout
\
Plugin\Commerce\CheckoutFlow\CheckoutFlowInterface
;
use
Drupal\commerce_checkout
\
Plugin\Commerce\CheckoutPane\CheckoutPaneBase
;
use
Drupal\commerce_payment
\
Entity\PaymentGatewayInterface
as
EntityPaymentGatewayInterface
;
use
Drupal\commerce_payment
\
Plugin\Commerce\PaymentGateway\SupportsStoredPaymentMethodsInterface
;
use
Drupal\Component\Utility\Html
;
use
Drupal\Component\Utility\NestedArray
;
use
Drupal\Core\Entity\Entity\EntityFormDisplay
;
use
Drupal\Core\Entity\EntityTypeManagerInterface
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\Core\Plugin\ContainerFactoryPluginInterface
;
use
Drupal\Core\Render\RendererInterface
;
use
Drupal\profile\Entity\Profile
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
/**
...
...
@@ -80,10 +83,11 @@ class PaymentInformation extends CheckoutPaneBase implements ContainerFactoryPlu
* {@inheritdoc}
*/
public
function
buildPaneSummary
()
{
$summary
=
''
;
/** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway */
$payment_gateway
=
$this
->
order
->
payment_gateway
->
entity
;
if
(
!
$payment_gateway
)
{
return
''
;
return
$summary
;
}
$payment_gateway_plugin
=
$payment_gateway
->
getPlugin
();
...
...
@@ -95,10 +99,12 @@ class PaymentInformation extends CheckoutPaneBase implements ContainerFactoryPlu
}
else
{
$billing_profile
=
$this
->
order
->
getBillingProfile
();
$profile_view_builder
=
$this
->
entityTypeManager
->
getViewBuilder
(
'profile'
);
$profile_view
=
$profile_view_builder
->
view
(
$billing_profile
,
'default'
);
$summary
=
$payment_gateway
->
getPlugin
()
->
getDisplayLabel
();
$summary
.
=
$this
->
renderer
->
render
(
$profile_view
);
if
(
$billing_profile
)
{
$profile_view_builder
=
$this
->
entityTypeManager
->
getViewBuilder
(
'profile'
);
$profile_view
=
$profile_view_builder
->
view
(
$billing_profile
,
'default'
);
$summary
=
$payment_gateway
->
getPlugin
()
->
getDisplayLabel
();
$summary
.
=
$this
->
renderer
->
render
(
$profile_view
);
}
}
return
$summary
;
...
...
@@ -110,8 +116,6 @@ class PaymentInformation extends CheckoutPaneBase implements ContainerFactoryPlu
public
function
buildPaneForm
(
array
$pane_form
,
FormStateInterface
$form_state
,
array
&
$complete_form
)
{
/** @var \Drupal\commerce_payment\PaymentGatewayStorageInterface $payment_gateway_storage */
$payment_gateway_storage
=
$this
->
entityTypeManager
->
getStorage
(
'commerce_payment_gateway'
);
/** @var \Drupal\commerce_payment\PaymentMethodStorageInterface $payment_method_storage */
$payment_method_storage
=
$this
->
entityTypeManager
->
getStorage
(
'commerce_payment_method'
);
/** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface[] $payment_gateways */
$payment_gateways
=
$payment_gateway_storage
->
loadMultipleForOrder
(
$this
->
order
);
// When no payment gateways are defined, throw an error and fail reliably.
...
...
@@ -122,8 +126,57 @@ class PaymentInformation extends CheckoutPaneBase implements ContainerFactoryPlu
// @todo Support multiple gateways.
/** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway */
$payment_gateway
=
reset
(
$payment_gateways
);
$pane_form
[
'payment_gateway'
]
=
[
'#type'
=>
'value'
,
'#value'
=>
$payment_gateway
->
id
(),
];
$payment_gateway_plugin
=
$payment_gateway
->
getPlugin
();
if
(
$payment_gateway_plugin
instanceof
SupportsStoredPaymentMethodsInterface
)
{
$this
->
attachPaymentMethodForm
(
$payment_gateway
,
$pane_form
,
$form_state
);
}
else
{
/** @var \Drupal\profile\Entity\ProfileInterface $billing_profile */
$billing_profile
=
$this
->
order
->
getBillingProfile
();
if
(
!
$billing_profile
)
{
$billing_profile
=
Profile
::
create
([
'uid'
=>
$this
->
order
->
getCustomerId
(),
'type'
=>
'customer'
,
]);
}
$form_display
=
EntityFormDisplay
::
collectRenderDisplay
(
$billing_profile
,
'default'
);
$form_display
->
buildForm
(
$billing_profile
,
$pane_form
,
$form_state
);
// Remove the details wrapper from the address field.
if
(
!
empty
(
$pane_form
[
'address'
][
'widget'
][
0
]))
{
$pane_form
[
'address'
][
'widget'
][
0
][
'#type'
]
=
'container'
;
}
// Store the billing profile for the validate/submit methods.
$pane_form
[
'#entity'
]
=
$billing_profile
;
}
return
$pane_form
;
}
/**
* Creates the payment method selection form for supported gateways.
*
* @param \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway
* The payment gateway.
* @param array $pane_form
* The pane form, containing the following basic properties:
* - #parents: Identifies the position of the pane form in the overall
* parent form, and identifies the location where the field values are
* placed within $form_state->getValues().
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state of the parent form.
*/
protected
function
attachPaymentMethodForm
(
EntityPaymentGatewayInterface
$payment_gateway
,
array
&
$pane_form
,
FormStateInterface
$form_state
)
{
/** @var \Drupal\commerce_payment\PaymentMethodStorageInterface $payment_method_storage */
$payment_method_storage
=
$this
->
entityTypeManager
->
getStorage
(
'commerce_payment_method'
);
$payment_gateway_plugin
=
$payment_gateway
->
getPlugin
();
$options
=
[];
$default_option
=
NULL
;
$customer
=
$this
->
order
->
getCustomer
();
...
...
@@ -174,8 +227,6 @@ class PaymentInformation extends CheckoutPaneBase implements ContainerFactoryPlu
'#default_value'
=>
$payment_method
,
];
}
return
$pane_form
;
}
/**
...
...
@@ -192,8 +243,23 @@ class PaymentInformation extends CheckoutPaneBase implements ContainerFactoryPlu
*/
public
function
validatePaneForm
(
array
&
$pane_form
,
FormStateInterface
$form_state
,
array
&
$complete_form
)
{
$values
=
$form_state
->
getValue
(
$pane_form
[
'#parents'
]);
if
(
!
isset
(
$values
[
'payment_method'
]))
{
$form_state
->
setError
(
$complete_form
,
$this
->
noPaymentGatewayErrorMessage
());
/** @var \Drupal\commerce_payment\PaymentGatewayStorageInterface $payment_gateway_storage */
$payment_gateway_storage
=
$this
->
entityTypeManager
->
getStorage
(
'commerce_payment_gateway'
);
/** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway */
$payment_gateway
=
$payment_gateway_storage
->
load
(
$values
[
'payment_gateway'
]);
if
(
$payment_gateway
->
getPlugin
()
instanceof
SupportsStoredPaymentMethodsInterface
)
{
if
(
!
isset
(
$values
[
'payment_method'
]))
{
$form_state
->
setError
(
$complete_form
,
$this
->
noPaymentGatewayErrorMessage
());
}
}
else
{
/** @var \Drupal\profile\Entity\ProfileInterface $billing_profile */
$billing_profile
=
$pane_form
[
'#entity'
];
$form_display
=
EntityFormDisplay
::
collectRenderDisplay
(
$billing_profile
,
'default'
);
$form_display
->
extractFormValues
(
$billing_profile
,
$pane_form
,
$form_state
);
$form_display
->
validateFormValues
(
$billing_profile
,
$pane_form
,
$form_state
);
}
}
...
...
@@ -202,19 +268,37 @@ class PaymentInformation extends CheckoutPaneBase implements ContainerFactoryPlu
*/
public
function
submitPaneForm
(
array
&
$pane_form
,
FormStateInterface
$form_state
,
array
&
$complete_form
)
{
$values
=
$form_state
->
getValue
(
$pane_form
[
'#parents'
]);
if
(
is_numeric
(
$values
[
'payment_method'
]))
{
/** @var \Drupal\commerce_payment\PaymentMethodStorageInterface $payment_method_storage */
$payment_method_storage
=
$this
->
entityTypeManager
->
getStorage
(
'commerce_payment_method'
);
$payment_method
=
$payment_method_storage
->
load
(
$values
[
'payment_method'
]);
/** @var \Drupal\commerce_payment\PaymentGatewayStorageInterface $payment_gateway_storage */
$payment_gateway_storage
=
$this
->
entityTypeManager
->
getStorage
(
'commerce_payment_gateway'
);
/** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway */
$payment_gateway
=
$payment_gateway_storage
->
load
(
$values
[
'payment_gateway'
]);
if
(
$payment_gateway
->
getPlugin
()
instanceof
SupportsStoredPaymentMethodsInterface
)
{
if
(
is_numeric
(
$values
[
'payment_method'
]))
{
/** @var \Drupal\commerce_payment\PaymentMethodStorageInterface $payment_method_storage */
$payment_method_storage
=
$this
->
entityTypeManager
->
getStorage
(
'commerce_payment_method'
);
$payment_method
=
$payment_method_storage
->
load
(
$values
[
'payment_method'
]);
}
else
{
$payment_method
=
$values
[
'add_payment_method'
];
}
/** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method */
$this
->
order
->
payment_gateway
=
$payment_method
->
getPaymentGateway
();
$this
->
order
->
payment_method
=
$payment_method
;
$this
->
order
->
setBillingProfile
(
$payment_method
->
getBillingProfile
());
}
else
{
$payment_method
=
$values
[
'add_payment_method'
];
/** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method */
$this
->
order
->
payment_gateway
=
$payment_gateway
;
/** @var \Drupal\profile\Entity\ProfileInterface $billing_profile */
$billing_profile
=
$pane_form
[
'#entity'
];
$form_display
=
EntityFormDisplay
::
collectRenderDisplay
(
$billing_profile
,
'default'
);
$form_display
->
extractFormValues
(
$billing_profile
,
$pane_form
,
$form_state
);
$billing_profile
->
save
();
$this
->
order
->
setBillingProfile
(
$billing_profile
);
}
/** @var \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method */
$this
->
order
->
payment_gateway
=
$payment_method
->
getPaymentGateway
();
$this
->
order
->
payment_method
=
$payment_method
;
$this
->
order
->
setBillingProfile
(
$payment_method
->
getBillingProfile
());
}
/**
...
...
modules/payment/src/Plugin/Commerce/CheckoutPane/PaymentProcess.php
View file @
f9875d38
...
...
@@ -2,15 +2,15 @@
namespace
Drupal\commerce_payment\Plugin\Commerce\CheckoutPane
;
use
Drupal\commerce\Response\NeedsRedirectException
;
use
Drupal\commerce_checkout
\
Plugin\Commerce\CheckoutFlow\CheckoutFlowInterface
;
use
Drupal\commerce_checkout
\
Plugin\Commerce\CheckoutPane\CheckoutPaneBase
;
use
Drupal\commerce_payment
\
Exception\PaymentGatewayException
;
use
Drupal\commerce_payment
\
Plugin\Commerce\PaymentGateway\OffsitePaymentGatewayInterface
;
use
Drupal\commerce_payment
\
Plugin\Commerce\PaymentGateway\OffsiteRedirectPaymentGatewayInterface
;
use
Drupal\commerce_payment
\
Plugin\Commerce\PaymentGateway\OnsitePaymentGatewayInterface
;
use
Drupal\Core\Entity\EntityTypeManagerInterface
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\Core\Plugin\ContainerFactoryPluginInterface
;
use
Drupal\Core\Url
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
/**
...
...
@@ -133,31 +133,19 @@ class PaymentProcess extends CheckoutPaneBase implements ContainerFactoryPluginI
$payment_gateway
=
$this
->
order
->
payment_gateway
->
entity
;
$payment_gateway_plugin
=
$payment_gateway
->
getPlugin
();
$payment_storage
=
$this
->
entityTypeManager
->
getStorage
(
'commerce_payment'
);
$payment
=
$payment_storage
->
create
([
'state'
=>
'new'
,
'amount'
=>
$this
->
order
->
getTotalPrice
(),
'payment_gateway'
=>
$payment_gateway
->
id
(),
'order_id'
=>
$this
->
order
->
id
(),
]);
if
(
$payment_gateway_plugin
instanceof
OnsitePaymentGatewayInterface
)
{
try
{
$payment_storage
=
$this
->
entityTypeManager
->
getStorage
(
'commerce_payment'
);
$payment
=
$payment_storage
->
create
([
'state'
=>
'new'
,
'amount'
=>
$this
->
order
->
getTotalPrice
(),
'payment_gateway'
=>
$payment_gateway
->
id
(),
'payment_method'
=>
$this
->
order
->
payment_method
->
entity
,
'order_id'
=>
$this
->
order
->
id
(),
]);
$payment
->
payment_method
=
$this
->
order
->
payment_method
->
entity
;
$payment_gateway_plugin
->
createPayment
(
$payment
,
$this
->
configuration
[
'capture'
]);
$next_step_id
=
$this
->
checkoutFlow
->
getNextStepId
();
// @todo Add a checkout flow method for completing checkout.
if
(
$next_step_id
==
'complete'
)
{
$transition
=
$this
->
order
->
getState
()
->
getWorkflow
()
->
getTransition
(
'place'
);
$this
->
order
->
getState
()
->
applyTransition
(
$transition
);
}
$this
->
order
->
checkout_step
=
$next_step_id
;
$this
->
order
->
save
();
throw
new
NeedsRedirectException
(
Url
::
fromRoute
(
'commerce_checkout.form'
,
[
'commerce_order'
=>
$this
->
order
->
id
(),
'step'
=>
$next_step_id
,
])
->
toString
());
$this
->
checkoutFlow
->
redirectToStep
(
$this
->
checkoutFlow
->
getNextStepId
());
}
catch
(
PaymentGatewayException
$e
)
{
...
...
@@ -165,6 +153,22 @@ class PaymentProcess extends CheckoutPaneBase implements ContainerFactoryPluginI
$this
->
redirectToPreviousStep
();
}
}
elseif
(
$payment_gateway_plugin
instanceof
OffsitePaymentGatewayInterface
)
{
$pane_form
[
'offsite_payment'
]
=
[
'#type'
=>
'commerce_payment_gateway_form'
,
'#operation'
=>
'offsite-payment'
,
'#default_value'
=>
$payment
,
];
if
(
$payment_gateway_plugin
instanceof
OffsiteRedirectPaymentGatewayInterface
)
{
$redirect_url
=
$payment_gateway_plugin
->
getRedirectUrl
();
// Make sure the redirect URL is the root form's action.
$complete_form
[
'#action'
]
=
$redirect_url
;
$complete_form
[
'#attributes'
][
'class'
][]
=
'payment-redirect-form'
;
}
return
$pane_form
;
}
else
{
drupal_set_message
(
$this
->
t
(
'Sorry, we can currently only support on site payment gateways.'
),
'error'
);
$this
->
redirectToPreviousStep
();
...
...
@@ -183,13 +187,7 @@ class PaymentProcess extends CheckoutPaneBase implements ContainerFactoryPluginI
$previous_step_id
=
$pane
->
getStepId
();
}
}
$this
->
order
->
checkout_step
=
$previous_step_id
;
$this
->
order
->
save
();
throw
new
NeedsRedirectException
(
Url
::
fromRoute
(
'commerce_checkout.form'
,
[
'commerce_order'
=>
$this
->
order
->
id
(),
'step'
=>
$previous_step_id
,
])
->
toString
());
$this
->
checkoutFlow
->
redirectToStep
(
$previous_step_id
);
}
}
modules/payment/src/Plugin/Commerce/PaymentGateway/OffsitePaymentGatewayBase.php
0 → 100644
View file @
f9875d38
<?php
namespace
Drupal\commerce_payment\Plugin\Commerce\PaymentGateway
;
use
Drupal\commerce_order
\
Entity\OrderInterface
;
use
Drupal\Core\Url
;
/**
* Provides the base class for off-site payment gateways.
*/
abstract
class
OffsitePaymentGatewayBase
extends
PaymentGatewayBase
implements
OffsitePaymentGatewayInterface
{
/**
* {@inheritdoc}
*/
public
function
getRedirectCancelUrl
(
OrderInterface
$order
)
{
return
Url
::
fromRoute
(
'commerce_payment.checkout.cancel'
,
[
'commerce_order'
=>
$order
->
id
(),
'step'
=>
'payment'
,
],
[
'absolute'
=>
TRUE
]);
}
/**
* {@inheritdoc}
*/
public
function
getRedirectReturnUrl
(
OrderInterface
$order
)
{
return
Url
::
fromRoute
(
'commerce_payment.checkout.return'
,
[
'commerce_order'
=>
$order
->
id
(),
'step'
=>
'payment'
,
],
[
'absolute'
=>
TRUE
]);
}
}
modules/payment/src/Plugin/Commerce/PaymentGateway/OffsitePaymentGatewayInterface.php
0 → 100644
View file @
f9875d38
<?php
namespace
Drupal\commerce_payment\Plugin\Commerce\PaymentGateway
;
use
Drupal\commerce_order
\
Entity\OrderInterface
;
/**
* Defines the base interface for off-site payment gateways.
*/
interface
OffsitePaymentGatewayInterface
extends
PaymentGatewayInterface
{
/**
* Gets the URL for offiste redirect cancel.
*
* @param \Drupal\commerce_order\Entity\OrderInterface $order
* The order.
*
* @return \Drupal\Core\Url
* The Url object
*/
public
function
getRedirectCancelUrl
(
OrderInterface
$order
);
/**
* Gets the URL for offiste redirect return.
*
* @param \Drupal\commerce_order\Entity\OrderInterface $order
* The order.
*
* @return \Drupal\Core\Url
* The Url object
*/
public
function
getRedirectReturnUrl
(
OrderInterface
$order
);
/**
* Invoked when the off-site payment return.
*
* @param \Drupal\commerce_order\Entity\OrderInterface $order
* The order.
*/
public
function
onRedirectReturn
(
OrderInterface
$order
);
/**
* Invoked when the off-site payment way cancelled or failed.
*
* @param \Drupal\commerce_order\Entity\OrderInterface $order
* The order.
*/
public
function
onRedirectCancel
(
OrderInterface
$order
);
}
modules/payment/src/Plugin/Commerce/PaymentGateway/OffsiteRedirectPaymentGatewayInterface.php
0 → 100644
View file @
f9875d38
<?php
namespace
Drupal\commerce_payment\Plugin\Commerce\PaymentGateway
;
/**
* Defines the base interface for off-site payment gateways.
*/
interface
OffsiteRedirectPaymentGatewayInterface
extends
OffsitePaymentGatewayInterface
{
/**
* Gets the off-site redirect URL.