Hello,
I'm integrating Cybersource into my project however when testing the request to create a plan I'm getting a Bad Response error like below:
CybersourceHelper.php on line 146: """ Erro: Client error: `POST https://apitest.cybersource.com/rbs/v1/plans` resulted in a `400 Bad Request` response:\n {"response":{"rmsg":"Bad Request"}}\n """
I'm using Guzzle client to http requests. Below is code snippet:
public function __construct(SettingsHelper $settingsHelper, string $apiKeySetting, string $secretKeySetting, string $merchantIdSetting, Router $router)
{
parent::__construct($settingsHelper, $apiKeySetting, $router);
$this->apiKey = $this->getApiKey();
$this->secretKey = $this->getSettingValue($secretKeySetting, true);
$this->merchantId = $this->getSettingValue($merchantIdSetting, true);
$this->host = 'apitest.cybersource.com';
}
// Function used to generate the digest for the given payload
private function generateDigest($requestPayload)
{
$utf8EncodedString = utf8_encode($requestPayload);
$digestEncode = hash("sha256", $utf8EncodedString, true);
return base64_encode($digestEncode);
}
private function getHttpSignature($resourcePath, $httpMethod, $currentDate, $payload)
{
$digest = "";
if($httpMethod == "get")
{
$signatureString = "host: " . $this->host . "\ndate: " . $currentDate . "\nrequest-target: " . $httpMethod . " " . $resourcePath . "\nv-c-merchant-id: " . $this->merchantId;
$headerString = "host date request-target v-c-merchant-id";
}
else if($httpMethod == "post")
{
//Get digest data
$digest = $this->generateDigest($payload);
$signatureString = "host: " . $this->host . "\ndate: " . $currentDate . "\nrequest-target: " . $httpMethod . " " . $resourcePath . "\ndigest: SHA-256=" . $digest . "\nv-c-merchant-id: " . $this->merchantId;
$headerString = "host date request-target digest v-c-merchant-id";
}
$signatureByteString = utf8_encode($signatureString);
$decodeKey = base64_decode($this->secretKey);
$signature = base64_encode(hash_hmac("sha256", $signatureByteString, $decodeKey, true));
$signatureHeader = array(
'keyid="' . $this->apiKey . '"',
'algorithm="HmacSHA256"',
'headers="' . $headerString . '"',
'signature="' . $signature . '"'
);
$signatureToken = "Signature:" . implode(", ", $signatureHeader);
$host = "Host:" . $this->host;
$vcMerchant = "v-c-merchant-id:" . $this->merchantId;
$headers = array(
$vcMerchant,
$signatureToken,
$host,
'Date:' . $currentDate
);
if($httpMethod == "post"){
$digestArray = array("Digest: SHA-256=" . $digest);
$headers = array_merge($headers, $digestArray);
}
return $headers;
}
public function createPlan($planParams) {
try {
$resource = "/rbs/v1/plans/";
$method = "post";
$url = "https://" . $this->host . "/rbs/v1/plans";
$resource = utf8_encode($resource);
$date = date("D, d M Y G:i:s ") . "GMT";
$headerParams = [];
$headers = [];
$headerParams['Accept'] = 'application/hal+json;charset=utf-8';
$headerParams['Content-Type'] = 'application/json;charset=utf-8';
foreach ($headerParams as $key => $val) {
$headers[] = "$key: $val";
}
$authHeaders = $this->getHttpSignature($resource, $method, $date, json_encode($planParams));
$headerParams = array_merge($headers, $authHeaders);
$params = [
'headers' => $headerParams,
'body' => json_encode($planParams),
];
$client = new Client();
$response = $client->request($method, $url, $params);
$contents = $response->getBody()->getContents();
$planData = json_decode($contents, false);
return $planData;
} catch (\GuzzleHttp\Exception\ClientException $e) {
dump("Error: " . $e->getMessage());
exit;
}
}
The code snippet which calls the create plan function:
$planParams = [
"planInformation" => [
"name" => "Gold Plan",
"description" => "New Gold Plan",
"billingPeriod" => [
"length" => "1",
"unit" => "M"
],
"billingCycles" => [
"total" => "12"
]
],
"orderInformation" => [
"amountDetails" => [
"currency" => "USD",
"billingAmount" => "10",
]
]
];
$result = $this->checkoutHelper->createPlan($planParams);
Could you please help me with this issue?
Thank you in advance!
12-04-2024 02:19 PM