Skip to content

Commit 84c9bc0

Browse files
committed
multipart helper used
1 parent 61672e5 commit 84c9bc0

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

lib/Utilities/MultipartHelpers/MultipartHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ public static function build_data_files($boundary, $formParams){
1010

1111
$delimiter = '-------------' . $boundary;
1212

13-
foreach ($formParams as $name => $content) {
13+
foreach ($formParams as $filename => $content) {
1414
$data .= "--" . $delimiter . $eol
15-
. 'Content-Disposition: form-data; name="' . $name . '"; filename="' . $name . '"' . $eol
15+
. 'Content-Disposition: form-data; name="' . $filename . '"; filename="' . $filename . '"' . $eol
1616
. 'Content-Transfer-Encoding: binary'.$eol
1717
;
1818

lib/Utilities/PGP/BatchUpload/MutualAuthUploadUtility.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace CyberSource\Utilities\PGP\BatchUpload;
44

55
use CyberSource\ApiException;
6+
use CyberSource\Utilities\MultipartHelpers\MultipartHelper;
67

78
class MutualAuthUploadUtility
89
{
@@ -95,29 +96,24 @@ private static function doMultipartUpload(
9596
$verify_ssl,
9697
$logger
9798
){
98-
$boundary = '--------------------------' . microtime(true);
99-
$eol = "\r\n";
100-
// works without this header too: Content-Transfer-Encoding
101-
$multipartBody =
102-
"--$boundary$eol" .
103-
"Content-Disposition: form-data; name=\"file\"; filename=\"$fileName\"$eol" .
104-
"Content-Type: application/octet-stream$eol" .
105-
"Content-Transfer-Encoding: binary$eol$eol" .
106-
$encryptedPgpBytes . $eol .
107-
"--$boundary--$eol";
108-
99+
100+
$boundary = uniqid();
101+
$formParams = [
102+
$fileName => $encryptedPgpBytes
103+
];
104+
$multipartBody = MultipartHelper::build_data_files($boundary, $formParams);
109105
$correlationId = self::generateCorrelationId();
110106

111107
$ch = curl_init();
112108
curl_setopt($ch, CURLOPT_URL, $endpointUrl);
113109
curl_setopt($ch, CURLOPT_POST, 1);
114110
curl_setopt($ch, CURLOPT_POSTFIELDS, $multipartBody);
115111
curl_setopt($ch, CURLOPT_HTTPHEADER, [
116-
"Content-Type: multipart/form-data; boundary=$boundary",
112+
"Content-Type: multipart/form-data; boundary=-------------$boundary",
117113
"v-c-correlation-id: $correlationId"
118114
]);
119115

120-
// mTLS: handle both PKCS#12 and cert and key
116+
//mTLS: handle both PKCS#12 and cert and key
121117
if ($tlsConfig['type'] === 'p12') {
122118
curl_setopt($ch, CURLOPT_SSLCERT, $tlsConfig['cert']);
123119
curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'P12');
@@ -132,7 +128,16 @@ private static function doMultipartUpload(
132128

133129
// CA cert for server validation
134130
if ($caCertPath) {
135-
curl_setopt($ch, CURLOPT_CAINFO, $caCertPath);
131+
print_r("Using custom CA cert path: $caCertPath\n");
132+
$defaultCaCertPath = __DIR__ . '/../../../ssl/cacert.pem';
133+
$defaultCa = file_exists($defaultCaCertPath) ? file_get_contents($defaultCaCertPath) : '';
134+
$userCa = file_get_contents($caCertPath);
135+
$combinedCa = rtrim($defaultCa, "\r\n") . "\n" . ltrim($userCa, "\r\n");
136+
curl_setopt($ch, CURLOPT_CAINFO_BLOB, $combinedCa);
137+
}else{
138+
$defaultCaCertPath = __DIR__ . '/../../../ssl/cacert.pem';
139+
$defaultCa = file_exists($defaultCaCertPath) ? file_get_contents($defaultCaCertPath) : '';
140+
curl_setopt($ch, CURLOPT_CAINFO_BLOB, $defaultCa);
136141
}
137142

138143
// SSL verification

0 commit comments

Comments
 (0)