| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © Melograno Venture Studio. All rights reserved. |
| 5 |
* @licence See COPYING.md for license details. |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace IvyForms\Controllers\Form; |
| 9 |
|
| 10 |
use IvyForms\Common\Exceptions\ForbiddenException; |
| 11 |
use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 12 |
use IvyForms\Common\Exceptions\NotFoundException; |
| 13 |
use IvyForms\Common\Exceptions\ValidationException; |
| 14 |
use IvyForms\Common\Sanitizer\Sanitizer; |
| 15 |
use IvyForms\Controllers\Controller; |
| 16 |
use IvyForms\Infrastructure\WP\REST\SubmissionMultipartReader; |
| 17 |
use IvyForms\Services\Submission\FormSubmissionService; |
| 18 |
use IvyForms\Services\Submission\SubmissionPayloadParser; |
| 19 |
use IvyForms\Services\Translations\BackendStrings; |
| 20 |
use WP_REST_Request; |
| 21 |
use WP_REST_Response; |
| 22 |
|
| 23 |
/** |
| 24 |
* Class FormSubmissionController |
| 25 |
* |
| 26 |
* @package IvyForms\Controllers\Form |
| 27 |
*/ |
| 28 |
class FormSubmissionController extends Controller |
| 29 |
{ |
| 30 |
private FormSubmissionService $formSubmissionService; |
| 31 |
|
| 32 |
public function __construct(FormSubmissionService $formSubmissionService) |
| 33 |
{ |
| 34 |
$this->formSubmissionService = $formSubmissionService; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* @param WP_REST_Request $data |
| 39 |
* |
| 40 |
* @return WP_REST_Response |
| 41 |
* |
| 42 |
* @throws InvalidArgumentException|NotFoundException|ForbiddenException |
| 43 |
* @throws ValidationException |
| 44 |
*/ |
| 45 |
protected function handle(WP_REST_Request $data): WP_REST_Response |
| 46 |
{ |
| 47 |
Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 48 |
|
| 49 |
$params = SubmissionPayloadParser::parseRequestPayload($data); |
| 50 |
$formId = Sanitizer::sanitizeId((int) ($params['formId'] ?? 0)); |
| 51 |
Sanitizer::verifySubmissionNonce(isset($params['nonce']) ? (string) $params['nonce'] : null, $formId); |
| 52 |
|
| 53 |
$uploadedFiles = SubmissionMultipartReader::resolveUploadedFiles($data); |
| 54 |
$result = $this->formSubmissionService->processSubmission($params, $uploadedFiles); |
| 55 |
|
| 56 |
if (!empty($result['requires_action'])) { |
| 57 |
return new WP_REST_Response([ |
| 58 |
'message' => BackendStrings::getCommonStrings()['ok'], |
| 59 |
'data' => [ |
| 60 |
'success' => false, |
| 61 |
'requires_action' => true, |
| 62 |
'client_secret' => $result['client_secret'] ?? null, |
| 63 |
'payment_intent_id' => $result['payment_intent_id'] ?? null, |
| 64 |
'payment_method' => $result['payment_method'] ?? 'stripe', |
| 65 |
], |
| 66 |
], 200); |
| 67 |
} |
| 68 |
|
| 69 |
if (!empty($result['payment_error'])) { |
| 70 |
return new WP_REST_Response([ |
| 71 |
'message' => (string) ( |
| 72 |
$result['message'] ?? BackendStrings::getPaymentStrings()['payment_failed'] |
| 73 |
), |
| 74 |
'data' => [ |
| 75 |
'success' => false, |
| 76 |
'payment_error' => true, |
| 77 |
], |
| 78 |
], 200); |
| 79 |
} |
| 80 |
|
| 81 |
return new WP_REST_Response([ |
| 82 |
'data' => $result, |
| 83 |
], 200); |
| 84 |
} |
| 85 |
} |
| 86 |
|