在看到谷歌内容api中的文档后,我试图发送请求,每件事看起来都是正确的
https://www.googleapis.com/content/v2.1sandbox/myid/orders/orderid/returnRefundLineItem
但谷歌回应了 400 错误请求“格式错误的请求”
导致了一个400 Bad Request
.
谷歌内容 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());
}