Google 购物内容 returnrefundlineitem API content.ContentErrorDomain 格式错误的请求

Google Shopping Content returnrefundlineitem API content.ContentErrorDomain Malformed request

在看到 google 内容中的文档后,我试图将请求发送至 api 一切看起来都是正确的

https://www.googleapis.com/content/v2.1sandbox/myid/orders/orderid/returnRefundLineItem

但 google 响应 400 错误请求“格式错误的请求”

导致 400 Bad Request

Google内容Api响应:

{
 "error": {
  "errors": [
   {
    "domain": "content.ContentErrorDomain",
    "reason": "malformed_request",
    "message": "Malformed request"
   }
  ],
  "code": 400,
  "message": "Malformed request"
 }
}

这是我的请求正文

{
  "operationId": "operation-6f8fa23bce94444b87e",
  "lineItemId": "QGKPTRMCKNCZESI",
  "quantity": 10,
  "reason": "other",
  "reasonText": "other",
  "priceAmount": {
    "value": "4500.00",
    "currency": "EUR"
  },
  "taxAmount": {
    "value": "0.00",
    "currency": "EUR"
  }
}

将执行 guzzle 请求的函数

    public function execute($api, $requestType = 'GET', $body = null) {


        $client = new \GuzzleHttp\Client();
        $token = $this->container->get('config')->findOneByCode($this->configCode);
        $token = $token->getOptions();
        $requestOptions = ['headers' => ['Authorization' => 'Bearer ' . $token, 'Content-Type' => 'application/json', 'Accept' => 'application/json']];
        if (null !== $body) {

            $requestOptions = array_merge($requestOptions, ['body' => $body]);
        }
        $response = $client->request($requestType, $this->url . '/' . $this->merchant_id . '/' . $api, $requestOptions);

        if ($response->getStatusCode() === 200) {

            return (string)$response->getBody();
        }

        return false;
    }

用于退款的函数


    public function refundLineItem($saleReturnId){

        $saleReturn  = $this->container->get('sale.return')->find($saleReturnId);
        $operationId = substr('operation-' . md5($saleReturn->getShipmentId() . uniqid($this->source) . time()), 0, 36);
        if($saleReturn->getIsRefunded()){
            throw new \Exception('Cette commande est déja rembourser', 953);
        }
        $saleInfo = json_decode($saleReturn->getSale()->getOrderinfo());

        $saleInfo = $saleInfo->google_order_info;
        $currentLineItem = null;

        foreach ($saleInfo->lineItems as $lineItem){
            if($lineItem->id === $saleReturn->getItemRef()){
                $currentLineItem = $lineItem;
                break;
            }
        }
        if (null === $currentLineItem) {
            throw new \Exception('Line item not found in sale info', 951);
        }

        $requestBody = [
          'operationId' => $operationId,
          'lineItemId' => $saleReturn->getItemRef(),
//          'productId' => $saleReturn->getSaleProduct()->getFinalProduct()->getId(),
          'quantity' => $saleReturn->getSaleProduct()->getQty(),
          'reason' => 'other',
          'reasonText' => 'other',
          'priceAmount' => [
              'value' => $currentLineItem->price->value,
              'currency' => $currentLineItem->price->currency,
          ],
          'taxAmount' => [
              'value' => $currentLineItem->tax->value,
              'currency' => $currentLineItem->tax->currency,
          ]
        ];



        $response = $this->execute("orders/{$saleReturn->getSaleIncrementId()}/returnRefundLineItem", 'POST', json_encode($requestBody));

        if(empty($response)){
            throw new \Exception('Empty Google Response', 952);
        }
        $saleReturn->setIsRefunded(true);
        $saleReturn = $this->container->get('sale.return')->save($saleReturn);
        $this->cancelSaleOrSaleProduct($saleReturn->getSale()->getId(), 'product', $saleReturn->getSaleProduct()->getId());

    }

我通过使用另一笔退款解决了我的问题function createrefundinvoice

因为我用 "enableOrderinvoices" 创建了订单:true

使用returnrefundlineitem

设置"enableOrderinvoices":false即可解决问题