问题描述
在沙箱中使用以下代码使用新的SDK:
$payment = new \XeroAPI\XeroPHP\Models\Accounting\Payment([
'code' => '001','invoice_number' => $inv_num,'amount' => $amount
]);
$this->accounting_api->createPayment($this->getSingleTenant(),$payment);
我收到以下异常
"ErrorNumber": 10,"Type": "ValidationException","Message": "A validation exception occurred","Elements": [
{
"Date": "\/Date(1604880000000+0000)\/","Amount": 1.74,"Status": "AUTHORISED","HasAccount": false,"HasValidationErrors": true,"ValidationErrors": [
{
"Message": "You must specify either an Invoice ID or Invoice Number or CreditNote ID or CreditNote Number or Prepayment ID or Overpayment ID for the payment"
},{
"Message": "You must specify and Account code or Account ID for the payment"
},{
"Message": "You must specify an Overpayment ID for the payment"
}
]
}
]
我的输入是否真的有问题,我是否缺少触发此请求的必需内容,或者API是否为此产生了负担?
解决方法
最后,答案是忽略account_number和code属性以及它们的getter和setter方法,而是使用只设置了code和invoice_number属性的空Invoice和Account对象:
public function RegisterPayment($amount,$inv_num,$account_code){
$payment = new \XeroAPI\XeroPHP\Models\Accounting\Payment([
'account' => new \XeroAPI\XeroPHP\Models\Accounting\Account(['code' => $account_code]),'invoice' => new \XeroAPI\XeroPHP\Models\Accounting\Invoice(['invoice_number' => $inv_num]),'amount' => $amount,'date' => date('Y-m-d')
]);
try{
$this->accounting_api->createPayment($this->getSingleTenant(),$payment);
} catch (\XeroAPI\XeroPHP\ApiException $ex) {
//var_dump($ex->getResponseBody());
}
}