Files
shipping-portal/app/Classes/Modules/PerfexCRM/Services/CreatesPerfexCRMInvoicePayment.php
T

50 lines
2.0 KiB
PHP

<?php
namespace App\Classes\Modules\PerfexCRM\Services;
use Illuminate\Support\Facades\Http;
use App\Classes\Exceptions\MalformedRequestException;
use App\Classes\Exceptions\ConnectionErrorException;
use Illuminate\Support\Facades\Log;
use App\Classes\Modules\PerfexCRM\DataTransferObjects\InvoicePaymentPerfexCRMObject;
class CreatesPerfexCRMInvoicePayment
{
/**
* @param InvoicePaymentPerfexCRMObject $invoicePaymentPerfexCRMObject
* @return null|object
* @throws MalformedRequestException
*/
public function execute(InvoicePaymentPerfexCRMObject $invoicePaymentPerfexCRMObject) {
try{
$data = [
'invoiceid' => $invoicePaymentPerfexCRMObject->getInvoiceId(),
'amount' => number_format($invoicePaymentPerfexCRMObject->getAmount(), 2, '.', ''),
'date' => $invoicePaymentPerfexCRMObject->getDate(),
'paymentmode' => $invoicePaymentPerfexCRMObject->getPaymentMode(),
'transactionid' => $invoicePaymentPerfexCRMObject->getTransactionId(),
'note' => $invoicePaymentPerfexCRMObject->getNote(),
];
$response = Http::asForm()->withHeaders([
'authtoken' => config('perfexcrm.api_key')])
->post(config('perfexcrm.base_url').'/api/invoices/recordpayment',$data);
if($response->successful()){
$data = $response->json();
return (object) $data;
}else{
Log::channel('perfex_crm')->info('CreatesPerfexCRMInvoicePayment: '.$response);
return null;
}
}
catch(\Illuminate\Http\Client\ConnectionException $exception){
$error = 'Failed to connect to CRM';
throw new ConnectionErrorException($error, $exception->getMessage(), "", $exception->getTraceAsString());
}
catch(\Exception $exception){
throw new MalformedRequestException('Unable to get correct response from Perfex CRM server: ' . $exception->getMessage());
}
}
}