Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
getPaymentMethods example
private
readonly MediaService
$mediaService
,
private
readonly AbstractAppLoader
$appLoader
,
)
{
$this
->mimeDetector =
new
FinfoMimeTypeDetector
(
)
;
}
public
function
updatePaymentMethods
(
Manifest
$manifest
, string
$appId
, string
$defaultLocale
, Context
$context
)
: void
{
$existingPaymentMethods
=
$this
->
getExistingPaymentMethods
(
$manifest
->
getMetadata
(
)
->
getName
(
)
,
$appId
,
$context
)
;
$payments
=
$manifest
->
getPayments
(
)
;
$paymentMethods
=
$payments
!== null ?
$payments
->
getPaymentMethods
(
)
:
[
]
;
$upserts
=
[
]
;
foreach
(
$paymentMethods
as
$paymentMethod
)
{
$payload
=
$paymentMethod
->
toArray
(
$defaultLocale
)
;
$payload
[
'handlerIdentifier'
]
=
sprintf
(
'app\\%s_%s',
$manifest
->
getMetadata
(
)
->
getName
(
)
,
$paymentMethod
->
getIdentifier
(
)
)
;
/** @var PaymentMethodEntity|null $existing */
$existing
=
$existingPaymentMethods
->
filterByProperty
(
'handlerIdentifier',
$payload
[
'handlerIdentifier'
]
)
->
first
(
)
;
$existingAppPaymentMethod
=
$existing
?
$existing
->
getAppPaymentMethod
(
)
: null;
$payload
[
'appPaymentMethod'
]
[
'appId'
]
=
$appId
;
$page
->
setSalesChannelShippingMethods
(
$shippingMethods
)
;
$criteria
=
new
Criteria
(
)
;
$criteria
->
setTitle
(
'generic-page::payment-methods'
)
;
$event
=
new
PaymentMethodRouteRequestEvent
(
$request
,
new
Request
(
)
,
$context
,
$criteria
)
;
$this
->eventDispatcher->
dispatch
(
$event
)
;
$paymentMethods
=
$this
->paymentMethodRoute
->
load
(
$event
->
getStoreApiRequest
(
)
,
$context
,
$event
->
getCriteria
(
)
)
->
getPaymentMethods
(
)
;
$page
->
setSalesChannelPaymentMethods
(
$paymentMethods
)
;
$page
->
setMetaInformation
(
(
new
MetaInformation
(
)
)
->
assign
(
[
'revisit' => '15 days',
'robots' => 'index,follow',
'xmlLang' =>
$request
->attributes->
get
(
SalesChannelRequest::ATTRIBUTE_DOMAIN_LOCALE
)
?? '',
'metaTitle' =>
$this
->systemConfigService->
getString
(
'core.basicInformation.shopName',
$context
->
getSalesChannel
(
)
->
getId
(
)
)
,
]
)
)
;
$this
->eventDispatcher->
dispatch
(
$this
->
createMock
(
GenericPageLoader::
class
)
,
$this
->
createMock
(
AddressValidationFactory::
class
)
,
$this
->
createMock
(
DataValidator::
class
)
)
;
$page
=
$checkoutConfirmPageLoader
->
load
(
new
Request
(
)
,
$this
->
getContextWithDummyCustomer
(
)
)
;
static
::
assertInstanceOf
(
CheckoutConfirmPage::
class
,
$page
)
;
static
::
assertSame
(
$paymentMethods
,
$page
->
getPaymentMethods
(
)
)
;
static
::
assertSame
(
$shippingMethods
,
$page
->
getShippingMethods
(
)
)
;
}
public
function
testCustomerNotLoggedInException
(
)
: void
{
$checkoutConfirmPageLoader
=
new
CheckoutConfirmPageLoader
(
$this
->
createMock
(
EventDispatcher::
class
)
,
$this
->
createMock
(
StorefrontCartFacade::
class
)
,
$this
->
createMock
(
ShippingMethodRoute::
class
)
,
$this
->
createMock
(
PaymentMethodRoute::
class
)
,
$this
->
createMock
(
GenericPageLoader::
class
)
,
/** @var CheckoutConfirmPageLoadedEvent $event */
$event
= null;
$this
->
catchEvent
(
CheckoutConfirmPageLoadedEvent::
class
,
$event
)
;
$page
=
$this
->
getPageLoader
(
)
->
load
(
$request
,
$context
)
;
static
::
assertInstanceOf
(
CheckoutConfirmPage::
class
,
$page
)
;
static
::
assertSame
(
0.0,
$page
->
getCart
(
)
->
getPrice
(
)
->
getNetPrice
(
)
)
;
static
::
assertSame
(
$context
->
getToken
(
)
,
$page
->
getCart
(
)
->
getToken
(
)
)
;
static
::
assertCount
(
StorefrontPageTestConstants::AVAILABLE_SHIPPING_METHOD_COUNT,
$page
->
getShippingMethods
(
)
)
;
static
::
assertCount
(
StorefrontPageTestConstants::AVAILABLE_PAYMENT_METHOD_COUNT,
$page
->
getPaymentMethods
(
)
)
;
static
::
assertNotEmpty
(
$page
->
getPaymentMethods
(
)
)
;
self::
assertPageEvent
(
CheckoutConfirmPageLoadedEvent::
class
,
$event
,
$context
,
$request
,
$page
)
;
}
public
function
testItIgnoresUnavailableShippingMethods
(
)
: void
{
$request
=
new
Request
(
)
;
$context
=
$this
->
createSalesChannelContextWithLoggedInCustomerAndWithNavigation
(
)
;
/** @var EntityRepository $shippingMethodRepository */
$shippingMethodRepository
=
$this
->
getContainer
(
)
->
get
(
'shipping_method.repository'
)
;
$page
=
$this
->genericLoader->
load
(
$request
,
$salesChannelContext
)
;
$page
= CheckoutCartPage::
createFrom
(
$page
)
;
if
(
$page
->
getMetaInformation
(
)
)
{
$page
->
getMetaInformation
(
)
->
setRobots
(
'noindex,follow'
)
;
}
$page
->
setCountries
(
$this
->
getCountries
(
$salesChannelContext
)
)
;
$page
->
setPaymentMethods
(
$this
->
getPaymentMethods
(
$salesChannelContext
)
)
;
$page
->
setShippingMethods
(
$this
->
getShippingMethods
(
$salesChannelContext
)
)
;
$page
->
setCart
(
$this
->cartService->
get
(
$salesChannelContext
->
getToken
(
)
,
$salesChannelContext
)
)
;
$this
->eventDispatcher->
dispatch
(
new
CheckoutCartPageLoadedEvent
(
$page
,
$salesChannelContext
,
$request
)
)
;
return
$page
;
}
$this
->sortedRoute =
new
SortedPaymentMethodRoute
(
$this
->decorated,
$this
->executor
)
;
}
public
function
testTriggersScriptHookExecution
(
)
: void
{
$this
->decorated
->
expects
(
static
::
once
(
)
)
->
method
(
'load'
)
->
willReturn
(
$this
->response
)
;
$this
->executor->
method
(
'execute'
)
->
with
(
static
::
callback
(
fn
(
PaymentMethodRouteHook
$hook
)
=>
$hook
->
getCollection
(
)
===
$this
->response->
getPaymentMethods
(
)
&&
$hook
->
getSalesChannelContext
(
)
===
$this
->context
&&
$hook
->
isOnlyAvailable
(
)
)
)
;
$response
=
$this
->sortedRoute->
load
(
new
Request
(
[
'onlyAvailable' => true
]
)
,
$this
->context,
new
Criteria
(
)
)
;
static
::
assertCount
(
1,
$response
->
getPaymentMethods
(
)
)
;
}
}
private
function
getPaymentMethodToChangeTo
(
ErrorCollection
$errors
, SalesChannelContext
$salesChannelContext
)
: ?PaymentMethodEntity
{
$blockedPaymentMethodNames
=
$errors
->
fmap
(
static
fn
(
Error
$error
)
=>
$error
instanceof PaymentMethodBlockedError ?
$error
->
getName
(
)
: null
)
;
$request
=
new
Request
(
[
'onlyAvailable' => true
]
)
;
$defaultPaymentMethod
=
$this
->paymentMethodRoute->
load
(
$request
,
$salesChannelContext
,
new
Criteria
(
[
$salesChannelContext
->
getSalesChannel
(
)
->
getPaymentMethodId
(
)
]
)
)
->
getPaymentMethods
(
)
->
first
(
)
;
if
(
$defaultPaymentMethod
!== null && !\
in_array
(
$defaultPaymentMethod
->
getName
(
)
,
$blockedPaymentMethodNames
, true
)
)
{
return
$defaultPaymentMethod
;
}
$criteria
=
new
Criteria
(
)
;
$criteria
->
addFilter
(
new
NandFilter
(
[
new
EqualsAnyFilter
(
'name',
$blockedPaymentMethodNames
)
,
]
)
)
;
class
PaymentMethodTest
extends
TestCase
{
public
function
testFromXml
(
)
: void
{
$manifest
= Manifest::
createFromXmlFile
(
__DIR__ . '/../_fixtures/test-manifest.xml'
)
;
static
::
assertNotNull
(
$manifest
->
getPayments
(
)
)
;
static
::
assertCount
(
2,
$manifest
->
getPayments
(
)
->
getPaymentMethods
(
)
)
;
$firstWebhook
=
$manifest
->
getPayments
(
)
->
getPaymentMethods
(
)
[
0
]
;
static
::
assertNotNull
(
$firstWebhook
)
;
static
::
assertSame
(
'myMethod',
$firstWebhook
->
getIdentifier
(
)
)
;
static
::
assertSame
(
'https://payment.app/payment/process',
$firstWebhook
->
getPayUrl
(
)
)
;
static
::
assertSame
(
'https://payment.app/payment/finalize',
$firstWebhook
->
getFinalizeUrl
(
)
)
;
static
::
assertSame
(
'https://payment.app/payment/refund',
$firstWebhook
->
getRefundUrl
(
)
)
;
static
::
assertSame
(
'https://payment.app/payment/recurring',
$firstWebhook
->
getRecurringUrl
(
)
)
;
static
::
assertSame
(
'Resources/payment.png',
$firstWebhook
->
getIcon
(
)
)
;
static
::
assertSame
(
[
'en-GB' => 'The app payment method',
if
(
$this
->env !== 'dev'
)
{
return
;
}
$usedFeatures
=
[
]
;
if
(
\
count
(
$manifest
->
getAdmin
(
)
?->
getModules
(
)
??
[
]
)
> 0
)
{
// if there is no app secret but the manifest specifies modules, throw an exception in dev mode
$usedFeatures
[
]
= 'Admin Modules';
}
if
(
\
count
(
$manifest
->
getPayments
(
)
?->
getPaymentMethods
(
)
??
[
]
)
> 0
)
{
$usedFeatures
[
]
= 'Payment Methods';
}
if
(
\
count
(
$manifest
->
getTax
(
)
?->
getTaxProviders
(
)
??
[
]
)
> 0
)
{
$usedFeatures
[
]
= 'Tax providers';
}
if
(
\
count
(
$manifest
->
getWebhooks
(
)
?->
getWebhooks
(
)
??
[
]
)
> 0
)
{
$usedFeatures
[
]
= 'Webhooks';
}
public
function
getDecorated
(
)
: AbstractPaymentMethodRoute
{
return
$this
->decorated;
}
#[Route(path: '/store-api/payment-method', name: 'store-api.payment.method', methods: ['GET', 'POST'], defaults: ['_entity' => 'payment_method'])]
public
function
load
(
Request
$request
, SalesChannelContext
$context
, Criteria
$criteria
)
: PaymentMethodRouteResponse
{
$response
=
$this
->
getDecorated
(
)
->
load
(
$request
,
$context
,
$criteria
)
;
$response
->
getPaymentMethods
(
)
->
sortPaymentMethodsByPreference
(
$context
)
;
$this
->scriptExecutor->
execute
(
new
PaymentMethodRouteHook
(
$response
->
getPaymentMethods
(
)
,
$request
->query->
getBoolean
(
'onlyAvailable'
)
||
$request
->request->
getBoolean
(
'onlyAvailable'
)
,
$context
)
)
;
return
$response
;
}
}
$orderRouteResponse
=
$this
->
getOrder
(
$request
,
$salesChannelContext
)
;
/** @var OrderEntity $order */
$order
=
$orderRouteResponse
->
getOrders
(
)
->
first
(
)
;
if
(
$this
->
isOrderPaid
(
$order
)
)
{
throw
OrderException::
orderAlreadyPaid
(
$order
->
getId
(
)
)
;
}
$page
->
setOrder
(
$order
)
;
$page
->
setPaymentChangeable
(
$this
->
isPaymentChangeable
(
$orderRouteResponse
,
$page
)
)
;
$page
->
setPaymentMethods
(
$this
->
getPaymentMethods
(
$salesChannelContext
,
$request
,
$order
)
)
;
$page
->
setDeepLinkCode
(
$request
->
get
(
'deepLinkCode'
)
)
;
$this
->eventDispatcher->
dispatch
(
new
AccountEditOrderPageLoadedEvent
(
$page
,
$salesChannelContext
,
$request
)
)
;
return
$page
;
}
private
function
getOrder
(
Request
$request
, SalesChannelContext
$context
)
: OrderRouteResponse
{
$this
->eventDispatcher->
dispatch
(
$event
)
;
}
private
function
validateRequest
(
SalesChannelContext
$salesChannelContext
, string
$paymentMethodId
)
: void
{
$paymentRequest
=
new
Request
(
)
;
$paymentRequest
->query->
set
(
'onlyAvailable', '1'
)
;
$availablePayments
=
$this
->paymentRoute->
load
(
$paymentRequest
,
$salesChannelContext
,
new
Criteria
(
)
)
;
if
(
$availablePayments
->
getPaymentMethods
(
)
->
get
(
$paymentMethodId
)
=== null
)
{
if
(
Feature::
isActive
(
'v6.6.0.0'
)
)
{
throw
OrderException::
paymentMethodNotAvailable
(
$paymentMethodId
)
;
}
throw
new
UnknownPaymentMethodException
(
$paymentMethodId
)
;
}
}
private
function
tryTransition
(
OrderEntity
$order
, string
$paymentMethodId
, Context
$context
)
: bool
{
$transactions
=
$order
->
getTransactions
(
)
;
$this
->ruleRepository->
create
(
$rule
,
$defaultContext
)
;
$criteria
=
new
Criteria
(
[
$rule
[
0
]
[
'id'
]
]
)
;
$criteria
->
addAssociation
(
'paymentMethods'
)
;
$searchedRule
=
$this
->ruleRepository->
search
(
$criteria
,
$defaultContext
)
->
getEntities
(
)
->
first
(
)
;
static
::
assertNotNull
(
$searchedRule
)
;
static
::
assertSame
(
$rule
[
0
]
[
'id'
]
?? null,
$searchedRule
->
getId
(
)
)
;
static
::
assertSame
(
$rule
[
0
]
[
'paymentMethods'
]
[
0
]
[
'id'
]
?? null,
$searchedRule
->
getPaymentMethods
(
)
?->
first
(
)
?->
getId
(
)
)
;
}
public
function
testIfPaymentMethodCanBeAddedToRule
(
)
: void
{
$defaultContext
= Context::
createDefaultContext
(
)
;
$rule
=
$this
->
createSimpleRule
(
)
;
$this
->ruleRepository->
create
(
$rule
,
$defaultContext
)
;
$additionalPaymentMethod
=
[
$traces
=
$this
->
getContainer
(
)
->
get
(
ScriptTraces::
class
)
->
getTraces
(
)
;
static
::
assertArrayHasKey
(
PaymentMethodRouteHook::HOOK_NAME,
$traces
)
;
}
public
function
testSorting
(
)
: void
{
$paymentMethodRoute
=
$this
->
getContainer
(
)
->
get
(
PaymentMethodRoute::
class
)
;
$request
=
new
Request
(
)
;
$unselectedPaymentResult
=
$paymentMethodRoute
->
load
(
$request
,
$this
->salesChannelContext,
new
Criteria
(
)
)
;
static
::
assertNotNull
(
$unselectedPaymentResult
->
getPaymentMethods
(
)
->
last
(
)
)
;
$lastPaymentMethodId
=
$unselectedPaymentResult
->
getPaymentMethods
(
)
->
last
(
)
->
getId
(
)
;
$this
->salesChannelContext->
getPaymentMethod
(
)
->
setId
(
$lastPaymentMethodId
)
;
$selectedPaymentMethodResult
=
$paymentMethodRoute
->
load
(
$request
,
$this
->salesChannelContext,
new
Criteria
(
)
)
;
static
::
assertNotNull
(
$selectedPaymentMethodResult
->
getPaymentMethods
(
)
->
first
(
)
)
;
static
::
assertSame
(
$lastPaymentMethodId
,
$selectedPaymentMethodResult
->
getPaymentMethods
(
)
->
first
(
)
->
getId
(
)
)
;
$traces
=
$this
->
getContainer
(
)
->
get
(
ScriptTraces::
class
)
->
getTraces
(
)
;
static
::
assertArrayHasKey
(
PaymentMethodRouteHook::HOOK_NAME,
$traces
)
;
}
$request
=
new
Request
(
)
;
$context
=
$this
->
createSalesChannelContextWithLoggedInCustomerAndWithNavigation
(
)
;
/** @var AccountPaymentMethodPageLoadedEvent $event */
$event
= null;
$this
->
catchEvent
(
AccountPaymentMethodPageLoadedEvent::
class
,
$event
)
;
$page
=
$this
->
getPageLoader
(
)
->
load
(
$request
,
$context
)
;
static
::
assertInstanceOf
(
AccountPaymentMethodPage::
class
,
$page
)
;
static
::
assertSame
(
StorefrontPageTestConstants::PAYMENT_METHOD_COUNT,
$page
->
getPaymentMethods
(
)
->
count
(
)
)
;
self::
assertPageEvent
(
AccountPaymentMethodPageLoadedEvent::
class
,
$event
,
$context
,
$request
,
$page
)
;
}
/** * @return AccountPaymentMethodPageLoader */
protected
function
getPageLoader
(
)
{
return
$this
->
getContainer
(
)
->
get
(
AccountPaymentMethodPageLoader::
class
)
;
}
}