mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-11-04 05:37:33 -05:00 
			
		
		
		
	Token Payments
This commit is contained in:
		
							parent
							
								
									8040b0a8f9
								
							
						
					
					
						commit
						b061506091
					
				
							
								
								
									
										111
									
								
								app/PaymentDrivers/Eway/Token.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										111
									
								
								app/PaymentDrivers/Eway/Token.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,111 @@
 | 
				
			|||||||
 | 
					<?php
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Invoice Ninja (https://invoiceninja.com).
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @link https://github.com/invoiceninja/invoiceninja source repository
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @license https://www.elastic.co/licensing/elastic-license
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace App\PaymentDrivers\Eway;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use App\Exceptions\PaymentFailed;
 | 
				
			||||||
 | 
					use App\Jobs\Mail\PaymentFailureMailer;
 | 
				
			||||||
 | 
					use App\Jobs\Util\SystemLogger;
 | 
				
			||||||
 | 
					use App\Models\ClientGatewayToken;
 | 
				
			||||||
 | 
					use App\Models\GatewayType;
 | 
				
			||||||
 | 
					use App\Models\Payment;
 | 
				
			||||||
 | 
					use App\Models\PaymentHash;
 | 
				
			||||||
 | 
					use App\Models\PaymentType;
 | 
				
			||||||
 | 
					use App\Models\SystemLog;
 | 
				
			||||||
 | 
					use App\PaymentDrivers\EwayPaymentDriver;
 | 
				
			||||||
 | 
					use App\PaymentDrivers\Eway\ErrorCode;
 | 
				
			||||||
 | 
					use App\Utils\Traits\MakesHash;
 | 
				
			||||||
 | 
					use Illuminate\Http\Request;
 | 
				
			||||||
 | 
					use Illuminate\Support\Facades\Cache;
 | 
				
			||||||
 | 
					use Illuminate\Support\Str;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Token
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						use MakesHash;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public $eway_driver;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function __construct(EwayPaymentDriver $eway_driver)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $this->eway_driver = $eway_driver;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    	$transaction = [
 | 
				
			||||||
 | 
							    'Customer' => [
 | 
				
			||||||
 | 
							        'TokenCustomerID' => $cgt->token,
 | 
				
			||||||
 | 
							    ],
 | 
				
			||||||
 | 
							    'Payment' => [
 | 
				
			||||||
 | 
							        'TotalAmount' => $this->eway_driver->convertAmount($amount),
 | 
				
			||||||
 | 
							    ],
 | 
				
			||||||
 | 
							    'TransactionType' => \Eway\Rapid\Enum\TransactionType::RECURRING,
 | 
				
			||||||
 | 
							];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $response = $this->eway_driver->init()->eway->createTransaction(\Eway\Rapid\Enum\ApiMethod::DIRECT, $transaction);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $response_status = ErrorCode::getStatus($response->ResponseMessage);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if(!$response_status['success'])
 | 
				
			||||||
 | 
					          return $this->processUnsuccessfulPayment($response);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      	$payment = $this->processSuccessfulPayment($response);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      	return $payment;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private function processSuccessfulPayment($response)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $amount = array_sum(array_column($this->eway_driver->payment_hash->invoices(), 'amount')) + $this->eway_driver->payment_hash->fee_total;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $data = [
 | 
				
			||||||
 | 
					            'gateway_type_id' => $cgt->gateway_type_id,
 | 
				
			||||||
 | 
					            'payment_type' => GatewayType::CREDIT_CARD_OTHER,
 | 
				
			||||||
 | 
					            'transaction_reference' => $response->Customer->Reference,
 | 
				
			||||||
 | 
					            'amount' => $amount,
 | 
				
			||||||
 | 
					        ];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $payment = $this->eway_driver->createPayment($data);
 | 
				
			||||||
 | 
					        $payment->meta = $cgt->meta;
 | 
				
			||||||
 | 
					        $payment->save();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $payment_hash->payment_id = $payment->id;
 | 
				
			||||||
 | 
					        $payment_hash->save();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return $payment;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private function processUnsuccessfulPayment($response)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $response_status = ErrorCode::getStatus($response->ResponseMessage);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    	$error = $response_status['message']
 | 
				
			||||||
 | 
					    	$error_code = $response->ResponseMessage;
 | 
				
			||||||
 | 
					    	
 | 
				
			||||||
 | 
					        $data = [
 | 
				
			||||||
 | 
					            'response' => $response,
 | 
				
			||||||
 | 
					            'error' => $error,
 | 
				
			||||||
 | 
					            'error_code' => $error_code,
 | 
				
			||||||
 | 
					        ];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return $this->driver_class->processUnsuccessfulTransaction($data);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -18,6 +18,7 @@ use App\Models\Payment;
 | 
				
			|||||||
use App\Models\PaymentHash;
 | 
					use App\Models\PaymentHash;
 | 
				
			||||||
use App\Models\SystemLog;
 | 
					use App\Models\SystemLog;
 | 
				
			||||||
use App\PaymentDrivers\Eway\CreditCard;
 | 
					use App\PaymentDrivers\Eway\CreditCard;
 | 
				
			||||||
 | 
					use App\PaymentDrivers\Eway\Token;
 | 
				
			||||||
use App\Utils\Traits\MakesHash;
 | 
					use App\Utils\Traits\MakesHash;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class EwayPaymentDriver extends BaseDriver
 | 
					class EwayPaymentDriver extends BaseDriver
 | 
				
			||||||
@ -95,13 +96,30 @@ class EwayPaymentDriver extends BaseDriver
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
 | 
					    public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        return $this->payment_method->yourTokenBillingImplmentation(); //this is your custom implementation from here
 | 
					        return (new Token($this))->tokenBilling($cgt, $payment_hash);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function processWebhookRequest(PaymentWebhookRequest $request, Payment $payment = null)
 | 
					    public function processWebhookRequest(PaymentWebhookRequest $request, Payment $payment = null)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function convertAmount($amount)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $precision = $this->client->currency()->precision;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if($precision == 0)
 | 
				
			||||||
 | 
					            return $amount;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if($precision == 1)
 | 
				
			||||||
 | 
					            return $amount*10;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if$precision == 2)
 | 
				
			||||||
 | 
					            return $amount*100;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return $amount;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function getClientRequiredFields(): array
 | 
					    public function getClientRequiredFields(): array
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $fields = [];
 | 
					        $fields = [];
 | 
				
			||||||
 | 
				
			|||||||
@ -21,12 +21,14 @@ use App\Models\Payment;
 | 
				
			|||||||
use App\Models\PaymentHash;
 | 
					use App\Models\PaymentHash;
 | 
				
			||||||
use App\Models\PaymentType;
 | 
					use App\Models\PaymentType;
 | 
				
			||||||
use App\Models\SystemLog;
 | 
					use App\Models\SystemLog;
 | 
				
			||||||
 | 
					use App\Utils\Traits\MakesHash;
 | 
				
			||||||
use Illuminate\Http\Request;
 | 
					use Illuminate\Http\Request;
 | 
				
			||||||
use Illuminate\Support\Facades\Cache;
 | 
					use Illuminate\Support\Facades\Cache;
 | 
				
			||||||
use Illuminate\Support\Str;
 | 
					use Illuminate\Support\Str;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class CreditCard
 | 
					class CreditCard
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					    use MakesHash;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public $driver_class;
 | 
					    public $driver_class;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -55,6 +57,43 @@ class CreditCard
 | 
				
			|||||||
        
 | 
					        
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* This method is stubbed ready to go - you just need to harvest the equivalent 'transaction_reference' */
 | 
				
			||||||
 | 
					    private function processSuccessfulPayment($response)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $amount = array_sum(array_column($this->driver_class->payment_hash->invoices(), 'amount')) + $this->driver_class->payment_hash->fee_total;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $payment_record = [];
 | 
				
			||||||
 | 
					        $payment_record['amount'] = $amount;
 | 
				
			||||||
 | 
					        $payment_record['payment_type'] = PaymentType::CREDIT_CARD_OTHER;
 | 
				
			||||||
 | 
					        $payment_record['gateway_type_id'] = GatewayType::CREDIT_CARD;
 | 
				
			||||||
 | 
					        // $payment_record['transaction_reference'] = $response->transaction_id;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $payment = $this->driver_class->createPayment($payment_record, Payment::STATUS_COMPLETED);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private function processUnsuccessfulPayment($response)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        /*Harvest your own errors here*/
 | 
				
			||||||
 | 
					        // $error = $response->status_message;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // if(property_exists($response, 'approval_message') && $response->approval_message)
 | 
				
			||||||
 | 
					        //     $error .= " - {$response->approval_message}";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // $error_code = property_exists($response, 'approval_message') ? $response->approval_message : 'Undefined code';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $data = [
 | 
				
			||||||
 | 
					            'response' => $response,
 | 
				
			||||||
 | 
					            'error' => $error,
 | 
				
			||||||
 | 
					            'error_code' => $error_code,
 | 
				
			||||||
 | 
					        ];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return $this->driver_class->processUnsuccessfulTransaction($data);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* Helpers */
 | 
					    /* Helpers */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -69,4 +108,6 @@ class CreditCard
 | 
				
			|||||||
     */
 | 
					     */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user