Api JSON 解码回复

问题描述 投票:0回答:1

我试图按照下面的命令从api收到回复

$client=new \GuzzleHttp\Client();
                $response=$client->request(
                    'post',
                    'https://cowpay.me/api/v0/fawry/charge-request-cc',
                    [
                    'json' =>[
                        'merchant_code' => $merchant_code,
                        'merchant_reference_id' => $merchant_reference_id,
                        'signature' => hash('sha256',$merchant_code.$merchant_reference_id.$user_id.$payment_method.$item_amout_fix.$m_hash_key),
                        'customer_name'=>$user_name,
                        'customer_email'=>$customer_email,
                        'customer_mobile'=>  $customer_mobile,
                        'customer_merchant_profile_id' => $user->id,
                        'currency_code' =>'EGP',
                        'amount' => $item_amout_fix,
                        'payment_method'=>'CARD',
                        'description' => $description,
                        'charge_items'=> [
                            'itemId'=> $item_number,
                            'description'=> $description,
                            'price'=> $item_amout_fix,
                            'quantity'=> '1',
                            ],
                            'card_number' => $request->card,
                            'expiry_year' => $request->year,
                            'expiry_month' => $request->month,
                            'cvv' => $request->cvv,
                            'save_card'     => '0',
                            ]
                            ] );
$response=json_decode($response->getBody()->getContents(), true);
return $response;

如何曾经这工作,并得到响应的形式的 https:/prnt.cs4xcra

但现在我得到的服务器错误和laravel日志显示的消息清楚,一个清晰和正确的状态代码,但不会得到它回到前端。

local.ERROR: Client error: `POST https://cowpay.me/api/v0/fawry/charge-request-cc` resulted in a `422 Unprocessable Entity` response:
{"success":false,"message":"The given data was invalid.","status_code":422,"status_description":"The given data was inva (truncated...)

要注意的是代码是完全正确的,因为我发送了一个有效的数据,它给我另一个状态代码,但在laravel> 存储> 日志>

json laravel
1个回答
0
投票

检查 CowPay API文档. 422状态码是CowPay的响应,告诉你你发送给他们的数据是无效的。

据猜测,这是因为你没有正确设置请求的头部和主体。你的JSON应该在请求的主体中,并以如下方式进行编码 json_encode. 你的标题还应该列出 Content-Type 的参数。

请尝试将您的请求的参数改为以下内容。

 'post',
 'https://cowpay.me/api/v0/fawry/charge-request-cc',
 [
     "headers" => [
         "Accept" => "application/json",
         "Content-Type" => "application/json"
     ],
     "body" => json_encode([
         'merchant_code' => $merchant_code,
         'merchant_reference_id' => $merchant_reference_id,
         'signature' => hash('sha256',$merchant_code.$merchant_reference_id.$user_id.$payment_method.$item_amout_fix.$m_hash_key),
         'customer_name'=>$user_name,
         'customer_email'=>$customer_email,
         'customer_mobile'=>  $customer_mobile,
         'customer_merchant_profile_id' => $user->id,
         'currency_code' =>'EGP',
         'amount' => $item_amout_fix,
         'payment_method'=>'CARD',
         'description' => $description,
         'charge_items'=> [
             'itemId'=> $item_number,
             'description'=> $description,
             'price'=> $item_amout_fix,
             'quantity'=> '1',
         ],
         'card_number' => $request->card,
         'expiry_year' => $request->year,
         'expiry_month' => $request->month,
         'cvv' => $request->cvv,
         'save_card'     => '0',
     ])
 ]
© www.soinside.com 2019 - 2024. All rights reserved.