1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 17:01:33 +02:00
invoiceninja/app/PaymentDrivers/Authorize/AuthorizeCreateCustomer.php

89 lines
2.7 KiB
PHP
Raw Normal View History

2020-06-10 03:06:37 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\PaymentDrivers\Authorize;
2020-06-10 07:21:11 +02:00
use App\Exceptions\GenericPaymentDriverFailure;
use App\Models\Client;
2020-06-10 03:06:37 +02:00
use App\Models\GatewayType;
use App\PaymentDrivers\AuthorizePaymentDriver;
2020-06-10 07:21:11 +02:00
use net\authorize\api\constants\ANetEnvironment;
use net\authorize\api\contract\v1\CreateCustomerProfileRequest;
use net\authorize\api\contract\v1\CustomerAddressType;
use net\authorize\api\contract\v1\CustomerPaymentProfileType;
use net\authorize\api\contract\v1\CustomerProfileType;
use net\authorize\api\controller\CreateCustomerProfileController;
2020-06-10 03:06:37 +02:00
/**
* Class BaseDriver
* @package App\PaymentDrivers
*
*/
class AuthorizeCreateCustomer
{
public $authorize;
2020-06-10 07:21:11 +02:00
public $client;
public function __construct(AuthorizePaymentDriver $authorize, Client $client)
2020-06-10 03:06:37 +02:00
{
$this->authorize = $authorize;
2020-06-10 07:21:11 +02:00
$this->client = $client;
2020-06-10 03:06:37 +02:00
}
public function create($data = null)
{
2020-06-10 07:21:11 +02:00
error_reporting (E_ALL & ~E_DEPRECATED);
2020-06-10 10:11:53 +02:00
$this->authorize->init();
2020-06-10 07:21:11 +02:00
// Create the Bill To info for new payment type
$contact = $this->client->primary_contact()->first();
2020-06-10 10:11:53 +02:00
$refId = 'ref' . time();
2020-06-10 07:21:11 +02:00
// Create a new CustomerProfileType and add the payment profile object
$customerProfile = new CustomerProfileType();
$customerProfile->setDescription($this->client->present()->name());
$customerProfile->setMerchantCustomerId("M_" . time());
$customerProfile->setEmail($this->client->present()->email());
// Assemble the complete transaction request
$request = new CreateCustomerProfileRequest();
2020-06-10 10:11:53 +02:00
$request->setMerchantAuthentication($this->authorize->merchant_authentication);
2020-06-10 07:21:11 +02:00
$request->setRefId($refId);
$request->setProfile($customerProfile);
// Create the controller and get the response
$controller = new CreateCustomerProfileController($request);
$response = $controller->executeWithApiResponse($this->authorize->mode());
if (($response != null) && ($response->getMessages()->getResultCode() == "Ok")) {
return $response->getCustomerProfileId();
2020-06-10 03:06:37 +02:00
2020-06-10 07:21:11 +02:00
} else {
$errorMessages = $response->getMessages()->getMessage();
$message = "Unable to add customer to Authorize.net gateway";
if(is_array($errorMessages))
$message = $errorMessages[0]->getCode() . " " .$errorMessages[0]->getText();
throw new GenericPaymentDriverFailure($message);
}
2020-06-10 03:06:37 +02:00
}
2020-06-10 07:21:11 +02:00
2020-06-10 03:06:37 +02:00
}