mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-10-30 20:22:58 -04:00 
			
		
		
		
	Authorize Credit Card
This commit is contained in:
		
							parent
							
								
									19e9aac12b
								
							
						
					
					
						commit
						27cdfd24f1
					
				| @ -36,6 +36,7 @@ class CreditCard | ||||
|     public function __construct(SquarePaymentDriver $square_driver) | ||||
|     { | ||||
|         $this->square_driver = $square_driver; | ||||
|         $this->square_driver->init(); | ||||
|     } | ||||
| 
 | ||||
|     public function authorizeView($data) | ||||
| @ -47,7 +48,7 @@ class CreditCard | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
|     public function authorizeRequest($request) | ||||
|     public function authorizeResponse($request) | ||||
|     { | ||||
|         $amount_money = new \Square\Models\Money(); | ||||
|         $amount_money->setAmount(100); //amount in cents
 | ||||
| @ -63,12 +64,14 @@ class CreditCard | ||||
|         $body->setLocationId($this->square_driver->company_gateway->getConfigField('locationId')); | ||||
|         $body->setReferenceId(Str::random(16)); | ||||
| 
 | ||||
|         $api_response = $client->getPaymentsApi()->createPayment($body); | ||||
|         $api_response = $this->square_driver->square->getPaymentsApi()->createPayment($body); | ||||
| 
 | ||||
|         if ($api_response->isSuccess()) { | ||||
|             $result = $api_response->getResult(); | ||||
|             nlog($result); | ||||
|         } else { | ||||
|             $errors = $api_response->getErrors(); | ||||
|             nlog($errors); | ||||
|         } | ||||
| 
 | ||||
| 
 | ||||
| @ -135,7 +138,7 @@ Success response looks like this: | ||||
|         $body->setGivenName($this->square_driver->client->present()->name()); | ||||
|         $body->setFamilyName(''); | ||||
|         $body->setEmailAddress($this->square_driver->client->present()->email()); | ||||
|         $body->setAddress($address); | ||||
|         $body->setAddress($billing_address); | ||||
|         $body->setPhoneNumber($this->square_driver->client->phone); | ||||
|         $body->setReferenceId($this->square_driver->client->number); | ||||
|         $body->setNote('Created by Invoice Ninja.'); | ||||
| @ -147,8 +150,10 @@ Success response looks like this: | ||||
| 
 | ||||
|         if ($api_response->isSuccess()) { | ||||
|             $result = $api_response->getResult(); | ||||
|             nlog($result); | ||||
|         } else { | ||||
|             $errors = $api_response->getErrors(); | ||||
|             nlog($errors); | ||||
|         } | ||||
| 
 | ||||
| /*Customer now created response | ||||
| @ -172,8 +177,8 @@ Success response looks like this: | ||||
| 
 | ||||
|         $card = new \Square\Models\Card(); | ||||
|         $card->setCardholderName($this->square_driver->client->present()->name()); | ||||
| $card->setBillingAddress($address); | ||||
| $card->setCustomerId($result->customer->id); | ||||
|         $card->setBillingAddress($billing_address); | ||||
|         $card->setCustomerId($result->getCustomer()->getId()); | ||||
|         $card->setReferenceId(Str::random(8)); | ||||
| 
 | ||||
|         $body = new \Square\Models\CreateCardRequest( | ||||
| @ -182,7 +187,10 @@ $body = new \Square\Models\CreateCardRequest( | ||||
|             $card | ||||
|         ); | ||||
| 
 | ||||
| $api_response = $client->getCardsApi()->createCard($body); | ||||
|         $api_response = $this->square_driver | ||||
|                              ->square | ||||
|                              ->getCardsApi() | ||||
|                              ->createCard($body); | ||||
| 
 | ||||
|         if ($api_response->isSuccess()) { | ||||
|             $result = $api_response->getResult(); | ||||
| @ -220,17 +228,15 @@ if ($api_response->isSuccess()) { | ||||
| 
 | ||||
| */ | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|         $cgt = []; | ||||
|         $cgt['token'] = $result->card->id; | ||||
|         $cgt['token'] = $result->getCard()->getId(); | ||||
|         $cgt['payment_method_id'] = GatewayType::CREDIT_CARD; | ||||
| 
 | ||||
|         $payment_meta = new \stdClass; | ||||
|         $payment_meta->exp_month = $result->card->exp_month; | ||||
|         $payment_meta->exp_year = $result->card->exp_year; | ||||
|         $payment_meta->brand = $result->card->card_brand; | ||||
|         $payment_meta->last4 = $result->card->last_4; | ||||
|         $payment_meta->exp_month = $result->getCard()->getExpMonth(); | ||||
|         $payment_meta->exp_year = $result->getCard()->getExpYear(); | ||||
|         $payment_meta->brand = $result->getCard()->getCardBrand(); | ||||
|         $payment_meta->last4 = $result->getCard()->getLast4(); | ||||
|         $payment_meta->type = GatewayType::CREDIT_CARD; | ||||
| 
 | ||||
|         $cgt['payment_meta'] = $payment_meta; | ||||
|  | ||||
| @ -43,9 +43,9 @@ class SquarePaymentDriver extends BaseDriver | ||||
|     public function init() | ||||
|     { | ||||
| 
 | ||||
|         $this->square = new Square\SquareClient([ | ||||
|         $this->square = new \Square\SquareClient([ | ||||
|             'accessToken' => $this->company_gateway->getConfigField('accessToken'), | ||||
|             'environment' => $this->company_gateway->getConfigField('testMode') ? Square\Environment::SANDBOX : Square\Environment::PRODUCTION, | ||||
|             'environment' => $this->company_gateway->getConfigField('testMode') ? \Square\Environment::SANDBOX : \Square\Environment::PRODUCTION, | ||||
|         ]); | ||||
| 
 | ||||
|         return $this; /* This is where you boot the gateway with your auth credentials*/ | ||||
|  | ||||
| @ -8,8 +8,7 @@ | ||||
|     <form action="{{ route('client.payment_methods.store', ['method' => App\Models\GatewayType::CREDIT_CARD]) }}" | ||||
|         method="post" id="server_response"> | ||||
|         @csrf | ||||
|         <input type="text" name="sourceId" hidden> | ||||
|     | ||||
|         <input type="text" name="sourceId" id="sourceId" hidden> | ||||
|     | ||||
|     <div class="alert alert-failure mb-4" hidden id="errors"></div> | ||||
| 
 | ||||
| @ -81,15 +80,6 @@ | ||||
|         return card; | ||||
|       } | ||||
| 
 | ||||
|       async function createPayment(token) { | ||||
| 
 | ||||
|         document.getElementById('sourceId').value = token; | ||||
|         document.getElementById('server_response').submit(); | ||||
| 
 | ||||
|         const errorBody = await paymentResponse.text(); | ||||
|         throw new Error(errorBody); | ||||
|       } | ||||
| 
 | ||||
|       async function tokenize(paymentMethod) { | ||||
|         const tokenResult = await paymentMethod.tokenize(); | ||||
|         if (tokenResult.status === 'OK') { | ||||
| @ -154,10 +144,12 @@ | ||||
|             // disable the submit button as we await tokenization and make a payment request.
 | ||||
|             cardButton.disabled = true; | ||||
|             const token = await tokenize(paymentMethod); | ||||
|             const paymentResults = await createPayment(token); | ||||
| 
 | ||||
|             document.getElementById('sourceId').value = token; | ||||
|             document.getElementById('server_response').submit(); | ||||
|      | ||||
|             displayPaymentResults('SUCCESS'); | ||||
| 
 | ||||
|             console.debug('Payment Success', paymentResults); | ||||
|           } catch (e) { | ||||
|             cardButton.disabled = false; | ||||
|             displayPaymentResults('FAILURE'); | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user