Paypal 客户纠纷 API - 提供证据总是返回 MISSING_OR_INVALID_REQUEST_BODY

时间:2021-01-27 11:16:01

标签: php laravel curl paypal guzzle

在确认争议案件的 Paypal Disputes 上有 /provide-evidence 后,我一直在尝试在索赔阶段向 HATEOAS links 提供证据时上传 pdf 文件,curl下面的代码在 CLI 中工作:

 $ curl -v -X POST https://api.sandbox.paypal.com/v1/customer/disputes/PP-D-12345/provide-evidence \
   -H "Content-Type: multipart/related" \
   -H "Authorization: Bearer {AccessToken}" \
   -F 'input={"evidences":[{"evidence_type": "PROOF_OF_FULFILLMENT", "evidence_info": {"tracking_info": [{"carrier_name": "FEDEX", "tracking_number": "123456789"}]}, "notes": "Test"}]};type=application/json' \
   -F 'file1=@D:\Sample\storage\app\paypal\sample.pdf'

但是,当转换为 PHP 时,无论是使用 curl 还是 guzzle,API 都会返回 VALIDATION_ERROR - MISSING_OR_INVALID_REQUEST_BODY,我已经尝试了几乎所有可能的方法,但是错误一致。

使用 Guzzle:(尝试尽可能多地复制上面的工作卷曲,因为 Guzzle 无法发送 multipart/related 请求,我不得不 modify the content-type manually)。< /p>

$pdf = 'D:\Sample\storage\app\paypal\sample.pdf';

$input = [
  'evidences' => [
    [
      'evidence_type' => 'PROOF_OF_FULFILLMENT',
      'evidence_info' => [
        'tracking_info' => [
          'carrier_name' => "FEDEX",
          'tracking_number' => '122533485'
        ]
      ],
      'notes' => 'Test',
    ],
  ]
];

$client = new \GuzzleHttp\Client([
    'base_uri' => 'https://api.sandbox.paypal.com',
    'timeout'  => 2.0,
    'version' => 1.1
]);

$options = [
  'headers' => [
    'Authorization' => "Bearer $token",
  ],
  'multipart' => [
      [
          'name'     => 'input',
          'contents' => json_encode($input),
          'headers'  => ['Content-Type' => 'application/json']
      ],
      [
          'name'     => 'file1',
          'contents' => fopen($pdf, 'r'),
          'filename' => 'sample.pdf',
          'headers'  => ['Content-Type' => 'application/pdf']
      ],
  ]
];

$url = '/v1/customer/disputes/'.$disputeId.'/provide-evidence';

$headers = isset($options['headers']) ? $options['headers'] : [];
$body = new \GuzzleHttp\Psr7\MultipartStream($options['multipart']);
$request = new \GuzzleHttp\Psr7\Request('POST', $url, $headers, $body, '1.1');

$modify['set_headers']['Content-Type'] = 'multipart/related; boundary=' . $request->getBody()->getBoundary();

$request = \GuzzleHttp\Psr7\modify_request($request, $modify);

$response = $client->send($request);

当我只是执行普通的 multipart/form-data 请求时,上面的 guzzle 代码仍然返回 {VALIDATION_ERROR - MISSING_OR_INVALID_REQUEST_BODY} 和相同的结果。

可能是什么问题?任何想法或建议都会非常有帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

$input = [
  'evidences' => [
    [
      'evidence_type' => 'PROOF_OF_FULFILLMENT',
      'evidence_info' => [
        'tracking_info' => [
          'carrier_name' => "FEDEX",
          'tracking_number' => '122533485'
        ]
      ],
      'notes' => 'Test',
    ],
  ]
];

这是你的$input,如果你json_encode()它,你得到

{
    "evidences":
    [
        {
            "evidence_type":"PROOF_OF_FULFILLMENT",
            "evidence_info":{
                "tracking_info":{
                    "carrier_name":"FEDEX",
                    "tracking_number":"122533485"
                }
            },
            "notes":"Test"
            
        }
    ]
    
}

哪个与您的请求正文匹配,


我通过插入另一个方括号更改了您的 $input,

$input = [
  'evidences' => [
    [
      'evidence_type' => 'PROOF_OF_FULFILLMENT',
      'evidence_info' => [
        'tracking_info' => [[
          'carrier_name' => "FEDEX",
          'tracking_number' => '122533485'
        ]]
      ],
      'notes' => 'Test',
    ],
  ]
];

现在它匹配您的 curl 请求,

{
    "evidences":
        [
            {
                "evidence_type":"PROOF_OF_FULFILLMENT",
                "evidence_info":
                    {
                        "tracking_info":
                            [
                                {
                                    "carrier_name":"FEDEX",
                                    "tracking_number":"122533485"
                                    
                                }
                            ]    
                    },
                    "notes":"Test"             
            }
        ]
}

希望能帮到你。如果它不起作用,那么问题可能出在其他地方,但请求正文将匹配,您可以先尝试 guzzle 调用。