|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CyberSource\Api; |
| 4 | + |
| 5 | +use CyberSource\ApiException; |
| 6 | +use CyberSource\Utilities\PGP\BatchUpload\PgpEncryptionUtility; |
| 7 | +use CyberSource\Utilities\PGP\BatchUpload\MutualAuthUploadUtility; |
| 8 | +use CyberSource\Utilities\PGP\BatchUpload\BatchuploadUtility; |
| 9 | +use CyberSource\Logging\LogFactory; |
| 10 | +use CyberSource\Logging\LogConfiguration; |
| 11 | + |
| 12 | +class BatchUploadApi |
| 13 | +{ |
| 14 | + private static $logger = null; |
| 15 | + |
| 16 | + /** |
| 17 | + * BatchUploadApi constructor. |
| 18 | + * |
| 19 | + * Example usage with custom log configuration: |
| 20 | + * ```php |
| 21 | + * $logConfig = new \CyberSource\Logging\LogConfiguration(); |
| 22 | + * $logConfig->enableLogging(true); |
| 23 | + * $logConfig->setDebugLogFile(__DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "Log" . DIRECTORY_SEPARATOR . "debugTest.log"); |
| 24 | + * $logConfig->setErrorLogFile(__DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "Log" . DIRECTORY_SEPARATOR . "errorTest.log"); |
| 25 | + * $logConfig->setLogDateFormat("Y-m-d\TH:i:s"); |
| 26 | + * $logConfig->setLogFormat("[%datetime%] [%level_name%] [%channel%] : %message%\n"); |
| 27 | + * $logConfig->setLogMaxFiles(3); |
| 28 | + * $logConfig->setLogLevel("debug"); |
| 29 | + * $logConfig->enableMasking(true); |
| 30 | + * $api = new \CyberSource\Api\BatchUploadApi($logConfig); |
| 31 | + * ``` |
| 32 | + * |
| 33 | + * @param LogConfiguration|null $logConfig |
| 34 | + */ |
| 35 | + public function __construct($logConfig = null) |
| 36 | + { |
| 37 | + // If no log config provided, create one with logging disabled |
| 38 | + if ($logConfig === null) { |
| 39 | + $logConfig = new LogConfiguration(); |
| 40 | + $logConfig->enableLogging(false); |
| 41 | + } |
| 42 | + // Use LogFactory to get a logger for this class |
| 43 | + self::$logger = (new LogFactory())->getLogger( |
| 44 | + \CyberSource\Utilities\Helpers\ClassHelper::getClassName(get_class($this)), |
| 45 | + $logConfig |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Uploads a batch file using mutual TLS authentication with a PKCS#12 (.p12/.pfx) client certificate file. |
| 51 | + * |
| 52 | + * @param string $inputFilePath Path to the file to be uploaded. |
| 53 | + * @param string $environmentHostname The environment hostname. |
| 54 | + * @param string $pgpEncryptionCertPath Path to the PGP encryption certificate. |
| 55 | + * @param string $clientCertP12FilePath Path to the PKCS#12 client certificate file (.p12 or .pfx). |
| 56 | + * @param string $clientCertP12Password Password for the PKCS#12 client certificate. |
| 57 | + * @param string|null $serverTrustCertPath Path to the server trust certificate(s) in PEM format. Optional. |
| 58 | + * @param bool $verify_ssl Whether to verify the server's SSL certificate. Optional. Set to false to disable verification (not recommended). Default is true. |
| 59 | + * @return array [responseBody, statusCode, headers] |
| 60 | + * @throws ApiException |
| 61 | + */ |
| 62 | + public function uploadBatchWithP12( |
| 63 | + $inputFilePath, |
| 64 | + $environmentHostname, |
| 65 | + $pgpEncryptionCertPath, |
| 66 | + $clientCertP12FilePath, |
| 67 | + $clientCertP12Password, |
| 68 | + $serverTrustCertPath = null, |
| 69 | + $verify_ssl = true |
| 70 | + ) { |
| 71 | + if (self::$logger) { |
| 72 | + self::$logger->info("Starting batch upload with P12/PFX for file: $inputFilePath"); |
| 73 | + if ($verify_ssl === false) { |
| 74 | + self::$logger->warning("SSL verification is DISABLED for this batch upload. This is insecure and should not be used in production."); |
| 75 | + } |
| 76 | + } |
| 77 | + BatchuploadUtility::validateBatchApiP12Inputs( |
| 78 | + $inputFilePath, $environmentHostname, $pgpEncryptionCertPath, $clientCertP12FilePath, $serverTrustCertPath |
| 79 | + ); |
| 80 | + |
| 81 | + $endpointUrl = $this->getEndpointUrl($environmentHostname, "/pts/v1/transaction-batch-upload"); |
| 82 | + |
| 83 | + $encryptedPgpBytes = PgpEncryptionUtility::encryptFileToBytes($inputFilePath, $pgpEncryptionCertPath); |
| 84 | + |
| 85 | + $pgpFileName = basename($inputFilePath); |
| 86 | + if (empty($pgpFileName) || $pgpFileName === '.' || $pgpFileName === '..') { |
| 87 | + $pgpFileName = 'file.pgp'; |
| 88 | + } else { |
| 89 | + $pgpFileName = pathinfo($pgpFileName, PATHINFO_FILENAME) . '.pgp'; |
| 90 | + } |
| 91 | + |
| 92 | + return MutualAuthUploadUtility::uploadWithP12( |
| 93 | + $encryptedPgpBytes, |
| 94 | + $endpointUrl, |
| 95 | + $pgpFileName, |
| 96 | + $clientCertP12FilePath, |
| 97 | + $clientCertP12Password, |
| 98 | + $serverTrustCertPath, |
| 99 | + $verify_ssl, |
| 100 | + self::$logger |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Uploads a batch file using mutual TLS authentication with client private key and certificate. |
| 106 | + * |
| 107 | + * @param string $inputFilePath Path to the file to be uploaded. |
| 108 | + * @param string $environmentHostname The environment hostname (e.g., api.cybersource.com). |
| 109 | + * @param string $pgpEncryptionCertPath Path to the PGP encryption certificate. |
| 110 | + * @param string $clientCertPath Path to the client certificate (PEM). |
| 111 | + * @param string $clientKeyPath Path to the client private key (PEM). |
| 112 | + * @param string|null $serverTrustCertPath Path to the server trust certificate(s) in PEM format. Optional. |
| 113 | + * @param string|null $clientKeyPassword Password for the client private key. Optional. |
| 114 | + * @param bool $verify_ssl Whether to verify the server's SSL certificate. Optional. Set to false to disable verification (not recommended). Default is true. |
| 115 | + * @return array [responseBody, statusCode, headers] |
| 116 | + * @throws ApiException |
| 117 | + */ |
| 118 | + public function uploadBatchWithKeyAndCert( |
| 119 | + $inputFilePath, |
| 120 | + $environmentHostname, |
| 121 | + $pgpEncryptionCertPath, |
| 122 | + $clientCertPath, |
| 123 | + $clientKeyPath, |
| 124 | + $serverTrustCertPath = null, |
| 125 | + $clientKeyPassword = null, |
| 126 | + $verify_ssl = true |
| 127 | + ) { |
| 128 | + if (self::$logger) { |
| 129 | + self::$logger->info("Starting batch upload with client key/cert for file: $inputFilePath"); |
| 130 | + if ($verify_ssl === false) { |
| 131 | + self::$logger->warning("SSL verification is DISABLED for this batch upload. This is insecure and should not be used in production."); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + BatchuploadUtility::validateBatchApiKeysInputs($inputFilePath, $environmentHostname, $pgpEncryptionCertPath, $clientKeyPath, $clientCertPath, $serverTrustCertPath); |
| 136 | + |
| 137 | + $endpointUrl = $this->getEndpointUrl($environmentHostname, "/pts/v1/transaction-batch-upload"); |
| 138 | + |
| 139 | + $encryptedPgpBytes = PgpEncryptionUtility::encryptFileToBytes($inputFilePath, $pgpEncryptionCertPath); |
| 140 | + |
| 141 | + $pgpFileName = basename($inputFilePath); |
| 142 | + if (empty($pgpFileName) || $pgpFileName === '.' || $pgpFileName === '..') { |
| 143 | + $pgpFileName = 'file.pgp'; |
| 144 | + } else { |
| 145 | + $pgpFileName = pathinfo($pgpFileName, PATHINFO_FILENAME) . '.pgp'; |
| 146 | + } |
| 147 | + |
| 148 | + return MutualAuthUploadUtility::uploadWithKeyAndCert( |
| 149 | + $encryptedPgpBytes, |
| 150 | + $endpointUrl, |
| 151 | + $pgpFileName, |
| 152 | + $clientCertPath, |
| 153 | + $clientKeyPath, |
| 154 | + $serverTrustCertPath, |
| 155 | + $clientKeyPassword, |
| 156 | + $verify_ssl, |
| 157 | + self::$logger |
| 158 | + ); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Constructs the full endpoint URL for the given environment hostname and endpoint path. |
| 163 | + * |
| 164 | + * @param string $environmentHostname The environment hostname (with or without protocol prefix). |
| 165 | + * @param string $endpoint The endpoint path to append. |
| 166 | + * @return string The full endpoint URL. |
| 167 | + */ |
| 168 | + private function getEndpointUrl($environmentHostname, $endpoint) |
| 169 | + { |
| 170 | + $URL_PREFIX = 'https://'; |
| 171 | + $baseUrl = (stripos(trim($environmentHostname), $URL_PREFIX) === 0) |
| 172 | + ? trim($environmentHostname) |
| 173 | + : $URL_PREFIX . trim($environmentHostname); |
| 174 | + return $baseUrl . $endpoint; |
| 175 | + } |
| 176 | + |
| 177 | +} |
0 commit comments