Open Journal Systems  3.3.0
PKPBackendPaymentsSettingsHandler.inc.php
1 <?php
16 import('lib.pkp.classes.handler.APIHandler');
17 import('classes.core.Services');
18 
20 
24  public function __construct() {
25  $rootPattern = '/{contextPath}/api/{version}/_payments';
26  $this->_endpoints = array_merge_recursive($this->_endpoints, array(
27  'PUT' => array(
28  array(
29  'pattern' => $rootPattern,
30  'handler' => array($this, 'edit'),
31  'roles' => array(
32  ROLE_ID_SITE_ADMIN,
33  ROLE_ID_MANAGER,
34  ),
35  ),
36  ),
37  ));
38  parent::__construct();
39  }
40 
44  public function authorize($request, &$args, $roleAssignments) {
45  import('lib.pkp.classes.security.authorization.PolicySet');
46  $rolePolicy = new PolicySet(COMBINING_PERMIT_OVERRIDES);
47 
48  import('lib.pkp.classes.security.authorization.RoleBasedHandlerOperationPolicy');
49  foreach ($roleAssignments as $role => $operations) {
50  $rolePolicy->addPolicy(new RoleBasedHandlerOperationPolicy($request, $role, $operations));
51  }
52  $this->addPolicy($rolePolicy);
53 
54  return parent::authorize($request, $args, $roleAssignments);
55  }
56 
65  public function edit($slimRequest, $response, $args) {
66  $request = $this->getRequest();
67  $context = $request->getContext();
68  $params = $slimRequest->getParsedBody();
69  $contextService = Services::get('context');
70 
71  // Process query params to format incoming data as needed
72  foreach ($slimRequest->getParsedBody() as $param => $val) {
73  switch ($param) {
74  case 'paymentsEnabled':
75  $params[$param] = $val === 'true';
76  break;
77  case 'currency':
78  $params[$param] = (string) $val;
79  break;
80  }
81  }
82 
83  if (isset($params['currency'])) {
84  $errors = $contextService->validate(
85  VALIDATE_ACTION_EDIT,
86  ['currency' => $params['currency']],
87  $context->getSupportedFormLocales(),
88  $context->getPrimaryLocale()
89  );
90  if (!empty($errors)) {
91  return $response->withStatus(400)->withJson($errors);
92  }
93  }
94 
95  $paymentPlugins = PluginRegistry::loadCategory('paymethod', true);
96  $errors = [];
97  foreach ($paymentPlugins as $paymentPlugin) {
98  $errors = array_merge(
99  $errors,
100  $paymentPlugin->saveSettings($params, $slimRequest, $request)
101  );
102  }
103  if (!empty($errors)) {
104  return $response->withStatus(400)->withJson($errors);
105  }
106 
107  $context = $contextService->get($context->getId());
108  $context = $contextService->edit($context, $params, $request);
109 
110  return $response->withJson($params);
111  }
112 }
PKPBackendPaymentsSettingsHandler\__construct
__construct()
Definition: PKPBackendPaymentsSettingsHandler.inc.php:24
PluginRegistry\loadCategory
static loadCategory($category, $enabledOnly=false, $mainContextId=null)
Definition: PluginRegistry.inc.php:103
PKPBackendPaymentsSettingsHandler
A private API endpoint handler for payment settings. It may be possible to deprecate this when we hav...
Definition: PKPBackendPaymentsSettingsHandler.inc.php:19
PKPBackendPaymentsSettingsHandler\edit
edit($slimRequest, $response, $args)
Definition: PKPBackendPaymentsSettingsHandler.inc.php:65
APIHandler
Base request API handler.
Definition: APIHandler.inc.php:22
PKPBackendPaymentsSettingsHandler\authorize
authorize($request, &$args, $roleAssignments)
Definition: PKPBackendPaymentsSettingsHandler.inc.php:44
RoleBasedHandlerOperationPolicy
Class to control access to handler operations via role based access control.
Definition: RoleBasedHandlerOperationPolicy.inc.php:18
APIHandler\getRequest
getRequest()
Definition: APIHandler.inc.php:149
PKPHandler\addPolicy
addPolicy($authorizationPolicy, $addToTop=false)
Definition: PKPHandler.inc.php:157
PolicySet
An ordered list of policies. Policy sets can be added to decision managers like policies....
Definition: PolicySet.inc.php:26
PKPServices\get
static get($service)
Definition: PKPServices.inc.php:49