File: /var/www/vhost/disk-apps/agile-selling-wpb/app/Http/Controllers/EbizChargeController.php
<?php
namespace App\Http\Controllers;
use App\BoSalesTaxCode;
use Illuminate\Http\Request;
use App\User;
use SoapClient;
use SoapVar;
use stdClass;
class EbizChargeController extends Controller
{
private $client;
private $base_url;
private $username;
private $password;
private $security_id;
public function __construct(){
$this->base_url = config('ebiz.endpoint');
$this->username = config('ebiz.user_id');
$this->password = config('ebiz.password');
$this->security_id = config('ebiz.security_id');
$this->client = new SoapClient($this->base_url, array('trace' => 1));
}
public function getCustomer($customer_id){
$this->addCustomer($customer_id);
$xml = '<SOAP-ENV:Header/>
<ns1:GetCustomer>
<ns1:securityToken>
<ns1:SecurityId>' . $this->security_id . '</ns1:SecurityId>
<ns1:UserId>' . $this->username . '</ns1:UserId>
<ns1:Password>' . $this->password . '</ns1:Password>
</ns1:securityToken>
<ns1:customerId>' . $customer_id . '</ns1:customerId>
</ns1:GetCustomer>';
$query = new SoapVar($xml, XSD_ANYXML);
try{
$response = $this->client->__SoapCall('GetCustomer', array($query));
$o = new stdClass();
$o->customer_internal_id = $response->GetCustomerResult->CustomerInternalId;
$o->customer_id = $response->GetCustomerResult->CustomerId;
$o->payment_methods = $response->GetCustomerResult->PaymentMethodProfiles;
$o->customer_token = $response->GetCustomerResult->CustomerToken;
$o->first_name = $response->GetCustomerResult->FirstName;
$o->last_name = $response->GetCustomerResult->LastName;
$o->email = $response->GetCustomerResult->Email;
return array('status' => true, 'o' => $o);
} catch(\Exception $ex){
return array('status' => false, 'm' => 'Customer ID ' . $customer_id . " not found in Ebiz.", 'headers' => $this->client->__getLastResponseHeaders(), 'e' => $ex->getMessage());
}
}
public function addCustomer($customer_id){
$user = User::where('document', $customer_id)->with("addresses")->first();
$addressBill = $user->addresses->where("tag", "bo_BillTo")->first();
$addressShip = $user->addresses->where("tag", "bo_ShipTo")->first();
$xml = '<SOAP-ENV:Header/>
<ns1:AddCustomer>
<ns1:securityToken>
<ns1:SecurityId>' . $this->security_id . '</ns1:SecurityId>
<ns1:UserId>' . $this->username . '</ns1:UserId>
<ns1:Password>' . $this->password . '</ns1:Password>
</ns1:securityToken>
<ns1:customer>
<ns1:CustomerId>' . $user->document . '</ns1:CustomerId>
<ns1:FirstName><![CDATA[' . $user->first_name . ']]></ns1:FirstName>
<ns1:LastName><![CDATA[' . $user->first_name . ']]></ns1:LastName>
<ns1:CompanyName><![CDATA[' . $user->first_name . ']]></ns1:CompanyName>
<ns1:Phone>' . $user->phone . '</ns1:Phone>
<ns1:Email>' . $user->email . '</ns1:Email>
</ns1:customer>
<ns1:BillingAddress>
<ns1:FirstName><![CDATA[' . $user->first_name . ']]></ns1:FirstName>
<ns1:LastName><![CDATA[' . $user->first_name . ']]></ns1:LastName>
<ns1:CompanyName><![CDATA[' . $user->first_name . ']]></ns1:CompanyName>
<ns1:Address1><![CDATA[' . $addressBill->direction . ']]></ns1:Address1>
<ns1:ZipCode>' . $addressBill->zip_code . '</ns1:ZipCode>
</ns1:BillingAddress>
<ns1:ShippingAddress>
<ns1:FirstName><![CDATA[' . $user->first_name . ']]></ns1:FirstName>
<ns1:LastName><![CDATA[' . $user->first_name . ']]></ns1:LastName>
<ns1:CompanyName><![CDATA[' . $user->first_name . ']]></ns1:CompanyName>
<ns1:Address1><![CDATA[' . $addressShip->direction . ']]></ns1:Address1>
<ns1:ZipCode>' . $addressShip->zip_code . '</ns1:ZipCode>
</ns1:ShippingAddress>
</ns1:AddCustomer>';
$query = new SoapVar($xml, XSD_ANYXML);
try{
$response = $this->client->__SoapCall('AddCustomer', array($query));
$o = new stdClass();
$o->customer_internal_id = $response->AddCustomerResult->CustomerInternalId;
$o->state = $response->AddCustomerResult->Status;
return array('status' => true, 'o' => $o);
} catch(\Exception $ex){
return array('status' => false);
}
}
public function addCreditCardToCustomer(Request $request){
// CardExpiration: YYYY-MM
$xml = '<SOAP-ENV:Header/>
<ns1:AddCustomerPaymentMethodProfile>
<ns1:securityToken>
<ns1:SecurityId>' . $this->security_id . '</ns1:SecurityId>
<ns1:UserId>' . $this->username . '</ns1:UserId>
<ns1:Password>' . $this->password . '</ns1:Password>
</ns1:securityToken>
<ns1:customerInternalId>' . $request->customer_internal_id . '</ns1:customerInternalId>
<ns1:paymentMethodProfile>
<ns1:MethodType>CreditCard</ns1:MethodType>
<ns1:AccountHolderName><![CDATA[' . $request->name_card . ']]></ns1:AccountHolderName>
<ns1:AvsStreet><![CDATA[' . $request->address_card . ']]></ns1:AvsStreet>
<ns1:AvsZip>' . $request->zip_code . '</ns1:AvsZip>
<ns1:CardCode>' . $request->cvv . '</ns1:CardCode>
<ns1:CardExpiration>' . $request->card_expiration . '</ns1:CardExpiration>
<ns1:CardNumber>' . $request->card_number . '</ns1:CardNumber>
</ns1:paymentMethodProfile>
</ns1:AddCustomerPaymentMethodProfile>';
$query = new SoapVar($xml, XSD_ANYXML);
try{
$response = $this->client->__SoapCall('AddCustomerPaymentMethodProfile', array($query));
if(property_exists($response, 'Fault')){
return array('status' => false, 'm' => $response->Fault->faultcode . ' ' . $response->Fault->faultstring);
}
return array('status' => true, 'o' => $response);
} catch(\Exception $ex){
return array('status' => false, 'm' => $ex->getMessage());
}
}
public function runTransaction($data, $force_value = null){
switch ($data->type_transaction) {
case 'authonly':
// stdClass with:
// order: Order::with('orderProducts');
// customer_token: Num de cliente en Ebiz.
// payment_method_id: ID de la tarjeta de crédito a pagar en Ebiz.
return $this->authorizeFromCreditCard($data, $force_value);
break;
case 'capture':
// stdClass with:
// authorization_code: Code de autorización de Ebiz
// reference_number: Code reference transacción de Ebiz
return $this->captureFromAuthcode($data);
break;
case 'void':
// stdClass with:
// authorization_code: Code de autorización de Ebiz
// reference_number: Code reference transacción de Ebiz
return $this->voidFromAuthcode($data);
break;
}
}
public function deleteCreditCard(Request $request){
$xml = '<SOAP-ENV:Header/>
<ns1:DeleteCustomerPaymentMethodProfile>
<ns1:securityToken>
<ns1:SecurityId>' . $this->security_id . '</ns1:SecurityId>
<ns1:UserId>' . $this->username . '</ns1:UserId>
<ns1:Password>' . $this->password . '</ns1:Password>
</ns1:securityToken>
<ns1:customerToken>' . $request->customerToken . '</ns1:customerToken>
<ns1:paymentMethodId>' . $request->paymentMethodId . '</ns1:paymentMethodId>
</ns1:DeleteCustomerPaymentMethodProfile>';
$query = new SoapVar($xml, XSD_ANYXML);
try{
$response = $this->client->__SoapCall('DeleteCustomerPaymentMethodProfile', array($query));
if(property_exists($response, 'Fault')){
return array('status' => false, 'm' => $response->Fault->faultcode . ' ' . $response->Fault->faultstring);
}
return array('status' => true, 'o' => $response);
} catch(\Exception $ex){
return array('status' => false, 'm' => $ex->getMessage());
}
}
public function authorizeFromCreditCard($data, $force_value = null){
// CardExpiration: YYYY-MM
$bo_sales_tax_codes = BoSalesTaxCode::all();
$amount = $data->order->total_price;
if($data->order->bo_balance_used > 0){ // Se uso balance, validar que se deba capturar
$amount = $data->order->total_price - $data->order->bo_balance_used;
}
if($amount == 0){ // No es neceario realizar authorization
return array('status' => true);
}
// Este parametro indica si el pago se altera por el uso de balance en la cuenta del cliente, reescribe el valor a pagar por este nuevo.
if($force_value){
$amount = $force_value;
}
$linesXML = '';
foreach ($data->order->orderProducts as $op) {
$current_tax_code = $bo_sales_tax_codes->where('code', $op->bo_item_tax_code)->first();
//$taxes = round($op->price * ($current_tax_code->rate/100), 2);
$taxes = $data->order->tax;
$line = '<ns1:LineItem>
<ns1:DiscountRate>0</ns1:DiscountRate>
<ns1:ProductRefNum>' . $op->bo_item_code . '</ns1:ProductRefNum>
<ns1:SKU>' . $op->bo_item_code . '</ns1:SKU>
<ns1:ProductName><![CDATA[' . $op->product->name . ']]></ns1:ProductName>
<ns1:Description><![CDATA[' . $op->product->name . ']]></ns1:Description>
<ns1:DiscountAmount>0</ns1:DiscountAmount>
<ns1:TaxRate>0</ns1:TaxRate>
<ns1:UnitOfMeasure>UND</ns1:UnitOfMeasure>
<ns1:UnitPrice>' . $op->price . '</ns1:UnitPrice>
<ns1:Qty>' . $op->quantity . '</ns1:Qty>
<ns1:Taxable>true</ns1:Taxable>
<ns1:TaxAmount>' . $taxes . '</ns1:TaxAmount>
</ns1:LineItem>';
$linesXML = $linesXML . $line;
}
$xml = '<SOAP-ENV:Header/>
<ns1:runCustomerTransaction>
<ns1:securityToken>
<ns1:SecurityId>' . $this->security_id . '</ns1:SecurityId>
<ns1:UserId>' . $this->username . '</ns1:UserId>
<ns1:Password>' . $this->password . '</ns1:Password>
</ns1:securityToken>
<ns1:custNum>' . $data->customer_token . '</ns1:custNum>
<ns1:paymentMethodID>' . $data->payment_method_id . '</ns1:paymentMethodID>
<ns1:tran>
<ns1:Details>
<ns1:NonTax>true</ns1:NonTax>
<ns1:Tax>' . $data->order->tax . '</ns1:Tax>
<ns1:Shipping>' . $data->order->cost_delivery . '</ns1:Shipping>
<ns1:PONum>' . $data->order->code . '</ns1:PONum>
<ns1:OrderID>' . $data->order->bo_doc_num . '</ns1:OrderID>
<ns1:Invoice>' . $data->order->bo_doc_num . '</ns1:Invoice>
<ns1:Duty>0</ns1:Duty>
<ns1:Discount>0</ns1:Discount>
<ns1:Comments><![CDATA[Purchase from AgileSelling:' . $data->order->code .']]></ns1:Comments>
<ns1:Description>Purchase from AgileSelling:' . $data->order->bo_doc_num . '</ns1:Description>
<ns1:Clerk>POS</ns1:Clerk>
<ns1:Amount>' . $amount . '</ns1:Amount>
<ns1:Terminal>AgilePOS</ns1:Terminal>
<ns1:Tip>0</ns1:Tip>
</ns1:Details>
<ns1:Software>AgilePOS</ns1:Software>
<ns1:Command>AuthOnly</ns1:Command>
<ns1:LineItems>
' . $linesXML . '
</ns1:LineItems>
</ns1:tran>
</ns1:runCustomerTransaction>';
// dd($xml);
$query = new SoapVar($xml, XSD_ANYXML);
try{
$response = $this->client->__SoapCall('runCustomerTransaction', array($query));
$o = new stdClass();
$o->result = $response->runCustomerTransactionResult;
return array('status' => true, 'o' => $o);
} catch(\Exception $ex){
return array('status' => false, 'o' => $ex->getMessage());
}
}
public function captureFromAuthcode($data){
$xml = '<SOAP-ENV:Header/>
<ns1:runTransaction>
<ns1:securityToken>
<ns1:SecurityId>' . $this->security_id . '</ns1:SecurityId>
<ns1:UserId>' . $this->username . '</ns1:UserId>
<ns1:Password>' . $this->password . '</ns1:Password>
</ns1:securityToken>
<ns1:tran>
<ns1:Command>Capture</ns1:Command>
<ns1:IgnoreDuplicate>false</ns1:IgnoreDuplicate>
<ns1:AuthCode>' . $data->bo_authorization_code_pay . '</ns1:AuthCode>
<ns1:RefNum>' . $data->bo_reference_number_pay . '</ns1:RefNum>
</ns1:tran>
</ns1:runTransaction>';
// dd($xml);
$query = new SoapVar($xml, XSD_ANYXML);
try{
$response = $this->client->__SoapCall('runTransaction', array($query));
$o = new stdClass();
$o->result = $response->runTransactionResult;
return array('status' => true, 'o' => $o);
} catch(\Exception $ex){
return array('status' => false, 'o' => $ex->getMessage());
}
}
public function voidFromAuthcode($data){
$xml = '<SOAP-ENV:Header/>
<ns1:runTransaction>
<ns1:securityToken>
<ns1:SecurityId>' . $this->security_id . '</ns1:SecurityId>
<ns1:UserId>' . $this->username . '</ns1:UserId>
<ns1:Password>' . $this->password . '</ns1:Password>
</ns1:securityToken>
<ns1:tran>
<ns1:Command>Void</ns1:Command>
<ns1:IgnoreDuplicate>false</ns1:IgnoreDuplicate>
<ns1:AuthCode>' . $data->bo_authorization_code_pay . '</ns1:AuthCode>
<ns1:RefNum>' . $data->bo_reference_number_pay . '</ns1:RefNum>
</ns1:tran>
</ns1:runTransaction>';
// dd($xml);
$query = new SoapVar($xml, XSD_ANYXML);
try{
$response = $this->client->__SoapCall('runTransaction', array($query));
$o = new stdClass();
$o->result = $response->runTransactionResult;
return array('status' => true, 'o' => $o);
} catch(\Exception $ex){
return array('status' => false, 'o' => $ex->getMessage());
}
}
}