1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 13:42:49 +01:00
invoiceninja/app/PaymentDrivers/Authorize/AuthorizeCreateCustomer.php

190 lines
7.7 KiB
PHP
Raw Normal View History

2020-06-10 03:06:37 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
2020-06-10 03:06:37 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2020-06-10 03:06:37 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-06-10 03:06:37 +02:00
*/
namespace App\PaymentDrivers\Authorize;
2023-10-26 04:57:44 +02:00
use App\Exceptions\GenericPaymentDriverFailure;
2020-06-10 07:21:11 +02:00
use App\Models\Client;
2020-06-10 03:06:37 +02:00
use App\PaymentDrivers\AuthorizePaymentDriver;
2023-10-26 04:57:44 +02:00
use net\authorize\api\contract\v1\CreateCustomerProfileRequest;
use net\authorize\api\contract\v1\CustomerAddressType;
2020-06-10 07:21:11 +02:00
use net\authorize\api\contract\v1\CustomerProfileType;
2021-01-25 12:48:12 +01:00
use net\authorize\api\contract\v1\GetCustomerProfileRequest;
use net\authorize\api\controller\CreateCustomerProfileController;
2023-10-26 04:57:44 +02:00
use net\authorize\api\controller\GetCustomerProfileController;
2020-06-10 03:06:37 +02:00
/**
* Class BaseDriver.
2020-06-10 03:06:37 +02:00
*/
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;
$this->client = $client;
2020-06-10 03:06:37 +02:00
}
public function create($data = null)
{
error_reporting(E_ALL & ~E_DEPRECATED);
2020-06-10 07:21:11 +02:00
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
2022-02-01 00:03:51 +01:00
$contact = $this->client->primary_contact()->first() ?: $this->client->contacts()->first();
$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());
2020-06-10 07:21:11 +02:00
$customerProfile->setEmail($this->client->present()->email());
// if($this->client) {
// $primary_contact = $this->client->primary_contact()->first() ?? $this->client->contacts()->first();
// $shipTo = new CustomerAddressType();
// $shipTo->setFirstName(substr($primary_contact->present()->first_name(), 0, 50));
// $shipTo->setLastName(substr($primary_contact->present()->last_name(), 0, 50));
// $shipTo->setCompany(substr($this->client->present()->name(), 0, 50));
// $shipTo->setAddress(substr($this->client->shipping_address1, 0, 60));
// $shipTo->setCity(substr($this->client->shipping_city, 0, 40));
// $shipTo->setState(substr($this->client->shipping_state, 0, 40));
// $shipTo->setZip(substr($this->client->shipping_postal_code, 0, 20));
// if ($this->client->country_id) {
// $shipTo->setCountry($this->client->shipping_country->name);
// }
// $shipTo->setPhoneNumber(substr($this->client->phone, 0, 20));
// $customerProfile->setShipToList([$shipTo]);
// }
2020-06-10 07:21:11 +02:00
// 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());
2020-06-10 03:06:37 +02:00
if (($response != null) && ($response->getMessages()->getResultCode() == 'Ok')) {
return $response->getCustomerProfileId();
2020-06-10 07:21:11 +02:00
} else {
$errorMessages = $response->getMessages()->getMessage();
2020-06-10 07:21:11 +02:00
$message = 'Unable to add customer to Authorize.net gateway';
2020-06-10 07:21:11 +02:00
if (is_array($errorMessages)) {
$message = $errorMessages[0]->getCode().' '.$errorMessages[0]->getText();
}
2020-06-10 07:21:11 +02:00
throw new GenericPaymentDriverFailure($message);
}
2020-06-10 03:06:37 +02:00
}
2021-01-25 12:48:12 +01:00
public function get($profileIdRequested)
{
error_reporting(E_ALL & ~E_DEPRECATED);
$this->authorize->init();
$request = new GetCustomerProfileRequest();
$request->setMerchantAuthentication($this->authorize->merchant_authentication);
$request->setCustomerProfileId($profileIdRequested);
2021-01-25 12:48:12 +01:00
$controller = new GetCustomerProfileController($request);
$response = $controller->executeWithApiResponse($this->authorize->merchant_authentication);
if (($response != null) && ($response->getMessages()->getResultCode() == 'Ok')) {
echo 'GetCustomerProfile SUCCESS : '."\n";
2021-01-25 12:48:12 +01:00
$profileSelected = $response->getProfile();
$paymentProfilesSelected = $profileSelected->getPaymentProfiles();
echo 'Profile Has '.count($paymentProfilesSelected).' Payment Profiles'."\n";
if ($response->getSubscriptionIds() != null) {
if ($response->getSubscriptionIds() != null) {
echo 'List of subscriptions:';
foreach ($response->getSubscriptionIds() as $subscriptionid) {
echo $subscriptionid."\n";
2021-01-25 12:48:12 +01:00
}
}
}
} else {
2021-01-25 12:48:12 +01:00
echo "ERROR : GetCustomerProfile: Invalid response\n";
$errorMessages = $response->getMessages()->getMessage();
echo 'Response : '.$errorMessages[0]->getCode().' '.$errorMessages[0]->getText()."\n";
2021-01-25 12:48:12 +01:00
}
return $response;
}
// This is how we can harvest client profiles and attach them within Invoice Ninja
2023-10-26 04:57:44 +02:00
// $request = new net\authorize\api\contract\v1\GetCustomerProfileRequest();
// $request->setMerchantAuthentication($driver->merchant_authentication);
// $request->setCustomerProfileId($gateway_customer_reference);
// $controller = new net\authorize\api\controller\GetCustomerProfileController($request);
// $response = $controller->executeWithApiResponse($driver->mode());
// if (($response != null) && ($response->getMessages()->getResultCode() == "Ok") )
// {
// echo "GetCustomerProfile SUCCESS : " . "\n";
// $profileSelected = $response->getProfile();
// $paymentProfilesSelected = $profileSelected->getPaymentProfiles();
// echo "Profile Has " . count($paymentProfilesSelected). " Payment Profiles" . "\n";
// foreach ($profileSelected->getPaymentProfiles() as $paymentProfile) {
// echo "\nCustomer Profile ID: " . $paymentProfile->getCustomerProfileId() . "\n";
// echo "Payment profile ID: " . $paymentProfile->getCustomerPaymentProfileId() . "\n";
// echo "Credit Card Number: " . $paymentProfile->getPayment()->getCreditCard()->getCardNumber() . "\n";
// if ($paymentProfile->getBillTo() != null) {
// echo "First Name in Billing Address: " . $paymentProfile->getBillTo()->getFirstName() . "\n";
// }
// }
}
2022-11-17 01:48:34 +01:00
// $request = new net\authorize\api\contract\v1\GetCustomerProfileIdsRequest();
// $request->setMerchantAuthentication($auth->merchant_authentication);
// $controller = new net\authorize\api\controller\GetCustomerProfileIdsController($request);
// $response = $controller->executeWithApiResponse($auth->mode());
// // $customer_profile_id = end($response->getIds());
// foreach($response->getIds() as $customer_profile_id)
// {
// $request = new net\authorize\api\contract\v1\GetCustomerProfileRequest();
// $request->setMerchantAuthentication($auth->merchant_authentication);
// $request->setCustomerProfileId($customer_profile_id);
// $controller = new net\authorize\api\controller\GetCustomerProfileController($request);
// $response = $controller->executeWithApiResponse($auth->mode());
2023-10-26 04:57:44 +02:00
2022-11-17 01:48:34 +01:00
// $profileSelected = $response->getProfile();
// if($profileSelected->getEmail() == 'katnandan@gmail.com')
// {
// $profileSelected;
// break;
// }
// }