|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace EasyPost\Service; |
| 4 | + |
| 5 | +use EasyPost\FedExAccountValidationResponse; |
| 6 | +use EasyPost\FedExRequestPinResponse; |
| 7 | +use EasyPost\Http\Requestor; |
| 8 | +use EasyPost\Util\InternalUtil; |
| 9 | + |
| 10 | +/** |
| 11 | + * FedExRegistration service containing all the logic to make API calls. |
| 12 | + */ |
| 13 | +class FedExRegistrationService extends BaseService |
| 14 | +{ |
| 15 | + /** |
| 16 | + * Register the billing address for a FedEx account. |
| 17 | + * Advanced method for custom parameter structures. |
| 18 | + * |
| 19 | + * @param string $fedexAccountNumber |
| 20 | + * @param mixed $params |
| 21 | + * @return mixed |
| 22 | + */ |
| 23 | + public function registerAddress(string $fedexAccountNumber, mixed $params = null): mixed |
| 24 | + { |
| 25 | + $wrappedParams = $this->wrapAddressValidation($params); |
| 26 | + $url = "/fedex_registrations/{$fedexAccountNumber}/address"; |
| 27 | + |
| 28 | + $response = Requestor::request($this->client, 'post', $url, $wrappedParams); |
| 29 | + |
| 30 | + return InternalUtil::convertToEasyPostObject($this->client, $response); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Request a PIN for FedEx account verification. |
| 35 | + * |
| 36 | + * @param string $fedexAccountNumber |
| 37 | + * @param string $pinMethodOption |
| 38 | + * @return mixed |
| 39 | + */ |
| 40 | + public function requestPin(string $fedexAccountNumber, string $pinMethodOption): mixed |
| 41 | + { |
| 42 | + $wrappedParams = [ |
| 43 | + 'pin_method' => [ |
| 44 | + 'option' => $pinMethodOption, |
| 45 | + ], |
| 46 | + ]; |
| 47 | + $url = "/fedex_registrations/{$fedexAccountNumber}/pin"; |
| 48 | + |
| 49 | + $response = Requestor::request($this->client, 'post', $url, $wrappedParams); |
| 50 | + |
| 51 | + return InternalUtil::convertToEasyPostObject($this->client, $response); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Validate the PIN entered by the user for FedEx account verification. |
| 56 | + * |
| 57 | + * @param string $fedexAccountNumber |
| 58 | + * @param mixed $params |
| 59 | + * @return mixed |
| 60 | + */ |
| 61 | + public function validatePin(string $fedexAccountNumber, mixed $params = null): mixed |
| 62 | + { |
| 63 | + $wrappedParams = $this->wrapPinValidation($params); |
| 64 | + $url = "/fedex_registrations/{$fedexAccountNumber}/pin/validate"; |
| 65 | + |
| 66 | + $response = Requestor::request($this->client, 'post', $url, $wrappedParams); |
| 67 | + |
| 68 | + return InternalUtil::convertToEasyPostObject($this->client, $response); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Submit invoice information to complete FedEx account registration. |
| 73 | + * |
| 74 | + * @param string $fedexAccountNumber |
| 75 | + * @param mixed $params |
| 76 | + * @return mixed |
| 77 | + */ |
| 78 | + public function submitInvoice(string $fedexAccountNumber, mixed $params = null): mixed |
| 79 | + { |
| 80 | + $wrappedParams = $this->wrapInvoiceValidation($params); |
| 81 | + $url = "/fedex_registrations/{$fedexAccountNumber}/invoice"; |
| 82 | + |
| 83 | + $response = Requestor::request($this->client, 'post', $url, $wrappedParams); |
| 84 | + |
| 85 | + return InternalUtil::convertToEasyPostObject($this->client, $response); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Wraps address validation parameters and ensures the "name" field exists. |
| 90 | + * If not present, generates a UUID (with hyphens removed) as the name. |
| 91 | + * |
| 92 | + * @param mixed $params |
| 93 | + * @return array<string, mixed> |
| 94 | + */ |
| 95 | + private function wrapAddressValidation(mixed $params): array |
| 96 | + { |
| 97 | + $wrappedParams = []; |
| 98 | + |
| 99 | + if (isset($params['address_validation'])) { |
| 100 | + $addressValidation = $params['address_validation']; |
| 101 | + $this->ensureNameField($addressValidation); |
| 102 | + $wrappedParams['address_validation'] = $addressValidation; |
| 103 | + } |
| 104 | + |
| 105 | + if (isset($params['easypost_details'])) { |
| 106 | + $wrappedParams['easypost_details'] = $params['easypost_details']; |
| 107 | + } |
| 108 | + |
| 109 | + return $wrappedParams; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Wraps PIN validation parameters and ensures the "name" field exists. |
| 114 | + * If not present, generates a UUID (with hyphens removed) as the name. |
| 115 | + * |
| 116 | + * @param mixed $params |
| 117 | + * @return array<string, mixed> |
| 118 | + */ |
| 119 | + private function wrapPinValidation(mixed $params): array |
| 120 | + { |
| 121 | + $wrappedParams = []; |
| 122 | + |
| 123 | + if (isset($params['pin_validation'])) { |
| 124 | + $pinValidation = $params['pin_validation']; |
| 125 | + $this->ensureNameField($pinValidation); |
| 126 | + $wrappedParams['pin_validation'] = $pinValidation; |
| 127 | + } |
| 128 | + |
| 129 | + if (isset($params['easypost_details'])) { |
| 130 | + $wrappedParams['easypost_details'] = $params['easypost_details']; |
| 131 | + } |
| 132 | + |
| 133 | + return $wrappedParams; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Wraps invoice validation parameters and ensures the "name" field exists. |
| 138 | + * If not present, generates a UUID (with hyphens removed) as the name. |
| 139 | + * |
| 140 | + * @param mixed $params |
| 141 | + * @return array<string, mixed> |
| 142 | + */ |
| 143 | + private function wrapInvoiceValidation(mixed $params): array |
| 144 | + { |
| 145 | + $wrappedParams = []; |
| 146 | + |
| 147 | + if (isset($params['invoice_validation'])) { |
| 148 | + $invoiceValidation = $params['invoice_validation']; |
| 149 | + $this->ensureNameField($invoiceValidation); |
| 150 | + $wrappedParams['invoice_validation'] = $invoiceValidation; |
| 151 | + } |
| 152 | + |
| 153 | + if (isset($params['easypost_details'])) { |
| 154 | + $wrappedParams['easypost_details'] = $params['easypost_details']; |
| 155 | + } |
| 156 | + |
| 157 | + return $wrappedParams; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Ensures the "name" field exists in the provided array. |
| 162 | + * If not present, generates a UUID (with hyphens removed) as the name. |
| 163 | + * This follows the pattern used in the web UI implementation. |
| 164 | + * |
| 165 | + * @param array<string, mixed> &$array |
| 166 | + * @return void |
| 167 | + */ |
| 168 | + private function ensureNameField(array &$array): void |
| 169 | + { |
| 170 | + if (!isset($array['name'])) { |
| 171 | + $uuid = sprintf( |
| 172 | + '%04x%04x%04x%04x%04x%04x%04x%04x', |
| 173 | + mt_rand(0, 0xffff), |
| 174 | + mt_rand(0, 0xffff), |
| 175 | + mt_rand(0, 0xffff), |
| 176 | + mt_rand(0, 0x0fff) | 0x4000, |
| 177 | + mt_rand(0, 0x3fff) | 0x8000, |
| 178 | + mt_rand(0, 0xffff), |
| 179 | + mt_rand(0, 0xffff), |
| 180 | + mt_rand(0, 0xffff) |
| 181 | + ); |
| 182 | + $array['name'] = $uuid; |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments