| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Services\Payment; |
| 4 |
|
| 5 |
use IvyForms\Entity\Form\Form; |
| 6 |
use IvyForms\Entity\Payment\Order as OrderEntity; |
| 7 |
use IvyForms\Entity\Payment\Payment as PaymentEntity; |
| 8 |
use IvyForms\Services\Translations\BackendStrings; |
| 9 |
use IvyForms\ValueObjects\Payment\Order as OrderValueObject; |
| 10 |
use IvyForms\ValueObjects\Payment\Payment as PaymentValueObject; |
| 11 |
use WP_REST_Response; |
| 12 |
|
| 13 |
/** |
| 14 |
* Payment processing during public form submission (gateway dispatch + persistence). |
| 15 |
*/ |
| 16 |
class FormSubmissionPaymentService |
| 17 |
{ |
| 18 |
public const STATUS_SUCCEEDED = 'succeeded'; |
| 19 |
|
| 20 |
public const STATUS_DECLINED = 'declined'; |
| 21 |
|
| 22 |
public const STATUS_FAILED = 'failed'; |
| 23 |
|
| 24 |
public const STATUS_ORPHANED = 'orphaned'; |
| 25 |
|
| 26 |
public const STATUS_PENDING = 'pending'; |
| 27 |
|
| 28 |
public const STATUS_REFUNDED = 'refunded'; |
| 29 |
|
| 30 |
public const STATUS_DISPUTED = 'disputed'; |
| 31 |
|
| 32 |
private PaymentAmountCalculator $paymentAmountCalculator; |
| 33 |
|
| 34 |
private PaymentManager $paymentManager; |
| 35 |
|
| 36 |
private OrderManager $orderManager; |
| 37 |
|
| 38 |
private PaymentGatewayRegistry $paymentGatewayRegistry; |
| 39 |
|
| 40 |
public function __construct( |
| 41 |
PaymentAmountCalculator $paymentAmountCalculator, |
| 42 |
PaymentManager $paymentManager, |
| 43 |
OrderManager $orderManager, |
| 44 |
PaymentGatewayRegistry $paymentGatewayRegistry |
| 45 |
) { |
| 46 |
$this->paymentAmountCalculator = $paymentAmountCalculator; |
| 47 |
$this->paymentManager = $paymentManager; |
| 48 |
$this->orderManager = $orderManager; |
| 49 |
$this->paymentGatewayRegistry = $paymentGatewayRegistry; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* @param array<int, object> $formFields |
| 54 |
*/ |
| 55 |
public function findPaymentMethodField(array $formFields): ?object |
| 56 |
{ |
| 57 |
foreach ($formFields as $field) { |
| 58 |
if (method_exists($field, 'getType') && $field->getType() === 'payment-method') { |
| 59 |
return $field; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
return null; |
| 64 |
} |
| 65 |
|
| 66 |
public function persistChargeFromResult( |
| 67 |
Form $form, |
| 68 |
PaymentChargeResult $chargeResult, |
| 69 |
string $status, |
| 70 |
?string $customerName = null |
| 71 |
): void { |
| 72 |
if ($status === self::STATUS_PENDING) { |
| 73 |
$this->persistDurableStripePayment($chargeResult, $form, $status, $customerName); |
| 74 |
|
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
$this->persistPayment( |
| 79 |
0, |
| 80 |
$form->getId(), |
| 81 |
[ |
| 82 |
'transactionId' => (string) ($chargeResult->getTransactionId() ?? ''), |
| 83 |
'receiptUrl' => $chargeResult->getReceiptUrl(), |
| 84 |
'providerMeta' => $chargeResult->getProviderMeta(), |
| 85 |
], |
| 86 |
$chargeResult->getPaymentTotal(), |
| 87 |
$chargeResult->getPaymentCurrency(), |
| 88 |
$chargeResult->getPaymentMethod(), |
| 89 |
$chargeResult->getStatus() |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
public function paymentAlreadyLinkedToEntry(string $transactionId): bool |
| 94 |
{ |
| 95 |
return $this->paymentManager->paymentAlreadyLinkedToEntry($transactionId); |
| 96 |
} |
| 97 |
|
| 98 |
public function duplicatePaymentErrorResponse(): WP_REST_Response |
| 99 |
{ |
| 100 |
return $this->paymentErrorResponse( |
| 101 |
BackendStrings::getPaymentStrings()['duplicate_payment_transaction'] |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Persist a successful submission payment. An entryId of 0 means the form does not store |
| 107 |
* entries, so the payment is kept without an entry link instead of being dropped. |
| 108 |
* |
| 109 |
* @param array<string, mixed> $paymentOutcome |
| 110 |
* @return bool False when a payment was expected but could not be recorded (e.g. duplicate transactionId). |
| 111 |
*/ |
| 112 |
public function recordSubmissionPayment(int $entryId, int $formId, array $paymentOutcome): bool |
| 113 |
{ |
| 114 |
return $this->recordPaymentFromOutcome($formId, $paymentOutcome, self::STATUS_SUCCEEDED, $entryId); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Persist a charge whose submission could not be stored, so the transaction is not lost. |
| 119 |
* |
| 120 |
* @param array<string, mixed> $paymentOutcome |
| 121 |
* @return bool False when a payment was expected but could not be recorded (e.g. duplicate transactionId). |
| 122 |
*/ |
| 123 |
public function recordOrphanedPayment(int $formId, array $paymentOutcome): bool |
| 124 |
{ |
| 125 |
return $this->recordPaymentFromOutcome($formId, $paymentOutcome, self::STATUS_ORPHANED); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* @param array<string, mixed> $paymentOutcome |
| 130 |
*/ |
| 131 |
private function recordPaymentFromOutcome( |
| 132 |
int $formId, |
| 133 |
array $paymentOutcome, |
| 134 |
string $status, |
| 135 |
int $entryId = 0 |
| 136 |
): bool { |
| 137 |
$paymentResult = $paymentOutcome['paymentResult'] ?? null; |
| 138 |
if (!is_array($paymentResult)) { |
| 139 |
return true; |
| 140 |
} |
| 141 |
|
| 142 |
return $this->persistPayment( |
| 143 |
$entryId, |
| 144 |
$formId, |
| 145 |
$paymentResult, |
| 146 |
(float) ($paymentOutcome['paymentTotal'] ?? 0), |
| 147 |
(string) ($paymentOutcome['paymentCurrency'] ?? 'USD'), |
| 148 |
(string) ($paymentOutcome['paymentMethod'] ?? ''), |
| 149 |
$status, |
| 150 |
isset($paymentOutcome['customerName']) ? (string) $paymentOutcome['customerName'] : null |
| 151 |
); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* @param array<string, mixed> $paymentResult |
| 156 |
*/ |
| 157 |
private function persistPayment( |
| 158 |
int $entryId, |
| 159 |
int $formId, |
| 160 |
array $paymentResult, |
| 161 |
float $paymentTotal, |
| 162 |
string $paymentCurrency, |
| 163 |
string $paymentMethod, |
| 164 |
string $status, |
| 165 |
?string $customerName = null |
| 166 |
): bool { |
| 167 |
$transactionId = (string) ($paymentResult['transactionId'] ?? ''); |
| 168 |
if ($transactionId === '') { |
| 169 |
return false; |
| 170 |
} |
| 171 |
|
| 172 |
$paymentEntity = new PaymentEntity( |
| 173 |
new PaymentValueObject( |
| 174 |
0, |
| 175 |
$entryId > 0 ? $entryId : null, |
| 176 |
$formId, |
| 177 |
$transactionId, |
| 178 |
$paymentMethod, |
| 179 |
$customerName !== null && $customerName !== '' ? $customerName : null, |
| 180 |
MoneyMinorUnits::toMinor($paymentTotal, $paymentCurrency), |
| 181 |
strtoupper($paymentCurrency), |
| 182 |
!empty($paymentResult['receiptUrl']) ? (string) $paymentResult['receiptUrl'] : null, |
| 183 |
null, |
| 184 |
!empty($paymentResult['providerMeta']) ? (string) $paymentResult['providerMeta'] : null |
| 185 |
) |
| 186 |
); |
| 187 |
$paymentEntity->setStatus($status); |
| 188 |
|
| 189 |
return $this->paymentManager->upsertByTransactionId($paymentEntity); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Customers column value: mapped name field, then mapped email. |
| 194 |
* |
| 195 |
* @param array<string, mixed> $buyerInfo |
| 196 |
*/ |
| 197 |
public static function resolveCustomerNameFromBuyerInfo(array $buyerInfo): ?string |
| 198 |
{ |
| 199 |
return SquareBuyerInfoBuilder::customerName($buyerInfo); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Persist the Square order record linked to an entry. |
| 204 |
*/ |
| 205 |
public function recordOrderForEntry( |
| 206 |
int $entryId, |
| 207 |
string $squareOrderId, |
| 208 |
?string $squareCustomerId, |
| 209 |
int $amountCents, |
| 210 |
string $currency |
| 211 |
): void { |
| 212 |
if ($squareOrderId === '') { |
| 213 |
return; |
| 214 |
} |
| 215 |
|
| 216 |
$orderEntity = new OrderEntity( |
| 217 |
new OrderValueObject( |
| 218 |
0, |
| 219 |
$entryId, |
| 220 |
$squareOrderId, |
| 221 |
'COMPLETED', |
| 222 |
$squareCustomerId, |
| 223 |
$amountCents, |
| 224 |
strtoupper($currency) |
| 225 |
) |
| 226 |
); |
| 227 |
|
| 228 |
$this->orderManager->createOrder($orderEntity); |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* @param array<string, mixed> $rawParams |
| 233 |
* @param array<int, object> $formFields |
| 234 |
* @param array<string, mixed> $submissionData |
| 235 |
* @return array{ |
| 236 |
* errorResponse: ?WP_REST_Response, |
| 237 |
* paymentResult: ?array<string, mixed>, |
| 238 |
* paymentTotal: float, |
| 239 |
* paymentCurrency: string, |
| 240 |
* paymentMethod: string, |
| 241 |
* squareOrderId: string, |
| 242 |
* squareCustomerId: ?string, |
| 243 |
* customerName: ?string, |
| 244 |
* requiresAction: bool, |
| 245 |
* clientSecret: ?string, |
| 246 |
* paymentIntentId: ?string |
| 247 |
* } |
| 248 |
* |
| 249 |
* @SuppressWarnings(PHPMD.NPathComplexity) |
| 250 |
* @SuppressWarnings(PHPMD.CyclomaticComplexity) |
| 251 |
*/ |
| 252 |
public function processWhenRequired( |
| 253 |
array $rawParams, |
| 254 |
Form $form, |
| 255 |
array $formFields, |
| 256 |
array $submissionData, |
| 257 |
?object $paymentField |
| 258 |
): array { |
| 259 |
if ($this->shouldSkipPayment($paymentField, $rawParams)) { |
| 260 |
return $this->skippedPaymentOutcome(); |
| 261 |
} |
| 262 |
|
| 263 |
return (new FormSubmissionChargeOrchestrator( |
| 264 |
$this->paymentAmountCalculator, |
| 265 |
$this->paymentGatewayRegistry, |
| 266 |
$this |
| 267 |
))->process($rawParams, $form, $formFields, $submissionData, $paymentField); |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* @return array<string, mixed> |
| 272 |
*/ |
| 273 |
private function skippedPaymentOutcome(): array |
| 274 |
{ |
| 275 |
return [ |
| 276 |
'errorResponse' => null, |
| 277 |
'paymentResult' => null, |
| 278 |
'paymentTotal' => 0.0, |
| 279 |
'paymentCurrency' => 'USD', |
| 280 |
'paymentMethod' => 'square', |
| 281 |
'squareOrderId' => '', |
| 282 |
'squareCustomerId' => null, |
| 283 |
'customerName' => null, |
| 284 |
'requiresAction' => false, |
| 285 |
'clientSecret' => null, |
| 286 |
'paymentIntentId' => null, |
| 287 |
]; |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* @param array<string, mixed> $rawParams |
| 292 |
*/ |
| 293 |
private function shouldSkipPayment(?object $paymentField, array $rawParams): bool |
| 294 |
{ |
| 295 |
if ($paymentField === null) { |
| 296 |
return true; |
| 297 |
} |
| 298 |
|
| 299 |
return !PaymentMethodFieldConfig::isRequired($paymentField) |
| 300 |
&& PaymentMethodFieldConfig::resolveGatewayIdFromToken($rawParams, $paymentField) === null; |
| 301 |
} |
| 302 |
|
| 303 |
private function paymentErrorResponse(string $message): WP_REST_Response |
| 304 |
{ |
| 305 |
return new WP_REST_Response([ |
| 306 |
'message' => $message, |
| 307 |
'data' => [ |
| 308 |
'success' => false, |
| 309 |
'payment_error' => true, |
| 310 |
], |
| 311 |
], 400); |
| 312 |
} |
| 313 |
|
| 314 |
private function persistDurableStripePayment( |
| 315 |
PaymentChargeResult $chargeResult, |
| 316 |
Form $form, |
| 317 |
string $status, |
| 318 |
?string $customerName = null |
| 319 |
): void { |
| 320 |
$transactionId = (string) ($chargeResult->getTransactionId() ?? ''); |
| 321 |
if ( |
| 322 |
!in_array($chargeResult->getPaymentMethod(), ['stripe', 'paypal'], true) |
| 323 |
|| $transactionId === '' |
| 324 |
) { |
| 325 |
return; |
| 326 |
} |
| 327 |
|
| 328 |
$this->persistPayment( |
| 329 |
0, |
| 330 |
$form->getId(), |
| 331 |
[ |
| 332 |
'transactionId' => $transactionId, |
| 333 |
'receiptUrl' => $chargeResult->getReceiptUrl(), |
| 334 |
'providerMeta' => $chargeResult->getProviderMeta(), |
| 335 |
], |
| 336 |
$chargeResult->getPaymentTotal(), |
| 337 |
$chargeResult->getPaymentCurrency(), |
| 338 |
$chargeResult->getPaymentMethod(), |
| 339 |
$status, |
| 340 |
$customerName ?? $chargeResult->getCustomerName() |
| 341 |
); |
| 342 |
} |
| 343 |
} |
| 344 |
|