mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-11-04 05:07:31 -05:00 
			
		
		
		
	
						commit
						0007bb4538
					
				@ -50,6 +50,7 @@ class CreditCard
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    public function authorizeResponse($request)
 | 
					    public function authorizeResponse($request)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
					        /* Step one - process a $1 payment - but don't complete it*/
 | 
				
			||||||
        $payment = false;
 | 
					        $payment = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $amount_money = new \Square\Models\Money();
 | 
					        $amount_money = new \Square\Models\Money();
 | 
				
			||||||
@ -69,121 +70,19 @@ class CreditCard
 | 
				
			|||||||
        $api_response = $this->square_driver->square->getPaymentsApi()->createPayment($body);
 | 
					        $api_response = $this->square_driver->square->getPaymentsApi()->createPayment($body);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if ($api_response->isSuccess()) {
 | 
					        if ($api_response->isSuccess()) {
 | 
				
			||||||
            // $result = $api_response->getResult();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            $result = $api_response->getBody();
 | 
					            $result = $api_response->getBody();
 | 
				
			||||||
            $payment = json_decode($result);
 | 
					            $payment = json_decode($result);
 | 
				
			||||||
 | 
					 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            $errors = $api_response->getErrors();
 | 
					            $errors = $api_response->getErrors();
 | 
				
			||||||
            nlog($errors);
 | 
					            return $this->processUnsuccessfulPayment($errors);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
 | 
					        /* Step 3 create the card */
 | 
				
			||||||
/*
 | 
					 | 
				
			||||||
Success response looks like this:
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
  "payment": {
 | 
					 | 
				
			||||||
    "id": "Dv9xlBgSgVB8i6eT0imRYFjcrOaZY",
 | 
					 | 
				
			||||||
    "created_at": "2021-03-31T20:56:13.220Z",
 | 
					 | 
				
			||||||
    "updated_at": "2021-03-31T20:56:13.411Z",
 | 
					 | 
				
			||||||
    "amount_money": {
 | 
					 | 
				
			||||||
      "amount": 100,
 | 
					 | 
				
			||||||
      "currency": "USD"
 | 
					 | 
				
			||||||
    },
 | 
					 | 
				
			||||||
    "status": "COMPLETED",
 | 
					 | 
				
			||||||
    "delay_duration": "PT168H",
 | 
					 | 
				
			||||||
    "source_type": "CARD",
 | 
					 | 
				
			||||||
    "card_details": {
 | 
					 | 
				
			||||||
      "status": "CAPTURED",
 | 
					 | 
				
			||||||
      "card": {
 | 
					 | 
				
			||||||
        "card_brand": "AMERICAN_EXPRESS",
 | 
					 | 
				
			||||||
        "last_4": "6550",
 | 
					 | 
				
			||||||
        "exp_month": 3,
 | 
					 | 
				
			||||||
        "exp_year": 2023,
 | 
					 | 
				
			||||||
        "fingerprint": "sq-1-hPdOWUYtEMft3yQ",
 | 
					 | 
				
			||||||
        "card_type": "CREDIT",
 | 
					 | 
				
			||||||
        "prepaid_type": "NOT_PREPAID",
 | 
					 | 
				
			||||||
        "bin": "371263"
 | 
					 | 
				
			||||||
      },
 | 
					 | 
				
			||||||
      "entry_method": "KEYED",
 | 
					 | 
				
			||||||
      "cvv_status": "CVV_ACCEPTED",
 | 
					 | 
				
			||||||
      "avs_status": "AVS_ACCEPTED",
 | 
					 | 
				
			||||||
      "statement_description": "SQ *DEFAULT TEST ACCOUNT",
 | 
					 | 
				
			||||||
      "card_payment_timeline": {
 | 
					 | 
				
			||||||
        "authorized_at": "2021-03-31T20:56:13.334Z",
 | 
					 | 
				
			||||||
        "captured_at": "2021-03-31T20:56:13.411Z"
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    },
 | 
					 | 
				
			||||||
    "location_id": "VJN4XSBFTVPK9",
 | 
					 | 
				
			||||||
    "total_money": {
 | 
					 | 
				
			||||||
      "amount": 100,
 | 
					 | 
				
			||||||
      "currency": "USD"
 | 
					 | 
				
			||||||
    },
 | 
					 | 
				
			||||||
    "approved_money": {
 | 
					 | 
				
			||||||
      "amount": 100,
 | 
					 | 
				
			||||||
      "currency": "USD"
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
   }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
*/
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $billing_address = new \Square\Models\Address();
 | 
					 | 
				
			||||||
        $billing_address->setAddressLine1($this->square_driver->client->address1);
 | 
					 | 
				
			||||||
        $billing_address->setAddressLine2($this->square_driver->client->address2);
 | 
					 | 
				
			||||||
        $billing_address->setLocality($this->square_driver->client->city);
 | 
					 | 
				
			||||||
        $billing_address->setAdministrativeDistrictLevel1($this->square_driver->client->state);
 | 
					 | 
				
			||||||
        $billing_address->setPostalCode($this->square_driver->client->postal_code);
 | 
					 | 
				
			||||||
        $billing_address->setCountry($this->square_driver->client->country->iso_3166_2);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $body = new \Square\Models\CreateCustomerRequest();
 | 
					 | 
				
			||||||
        $body->setGivenName($this->square_driver->client->present()->name());
 | 
					 | 
				
			||||||
        $body->setFamilyName('');
 | 
					 | 
				
			||||||
        $body->setEmailAddress($this->square_driver->client->present()->email());
 | 
					 | 
				
			||||||
        $body->setAddress($billing_address);
 | 
					 | 
				
			||||||
        $body->setPhoneNumber($this->square_driver->client->phone);
 | 
					 | 
				
			||||||
        $body->setReferenceId($this->square_driver->client->number);
 | 
					 | 
				
			||||||
        $body->setNote('Created by Invoice Ninja.');
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $api_response = $this->square_driver
 | 
					 | 
				
			||||||
                             ->square
 | 
					 | 
				
			||||||
                             ->getCustomersApi()
 | 
					 | 
				
			||||||
                             ->createCustomer($body);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if ($api_response->isSuccess()) {
 | 
					 | 
				
			||||||
            $result = $api_response->getResult();
 | 
					 | 
				
			||||||
            nlog($result);
 | 
					 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
            $errors = $api_response->getErrors();
 | 
					 | 
				
			||||||
            nlog($errors);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/*Customer now created response
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
  "customer": {
 | 
					 | 
				
			||||||
    "id": "Q6VKKKGW8GWQNEYMDRMV01QMK8",
 | 
					 | 
				
			||||||
    "created_at": "2021-03-31T18:27:07.803Z",
 | 
					 | 
				
			||||||
    "updated_at": "2021-03-31T18:27:07Z",
 | 
					 | 
				
			||||||
    "given_name": "Amelia",
 | 
					 | 
				
			||||||
    "family_name": "Earhart",
 | 
					 | 
				
			||||||
    "email_address": "Amelia.Earhart@example.com",
 | 
					 | 
				
			||||||
    "preferences": {
 | 
					 | 
				
			||||||
      "email_unsubscribed": false
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
*/
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $card = new \Square\Models\Card();
 | 
					        $card = new \Square\Models\Card();
 | 
				
			||||||
        $card->setCardholderName($this->square_driver->client->present()->name());
 | 
					        $card->setCardholderName($this->square_driver->client->present()->name());
 | 
				
			||||||
        $card->setBillingAddress($billing_address);
 | 
					        // $card->setBillingAddress($billing_address);
 | 
				
			||||||
        $card->setCustomerId($result->getCustomer()->getId());
 | 
					        $card->setCustomerId($this->findOrCreateClient());
 | 
				
			||||||
        $card->setReferenceId(Str::random(8));
 | 
					        $card->setReferenceId(Str::random(8));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $body = new \Square\Models\CreateCardRequest(
 | 
					        $body = new \Square\Models\CreateCardRequest(
 | 
				
			||||||
@ -197,64 +96,34 @@ Success response looks like this:
 | 
				
			|||||||
                             ->getCardsApi()
 | 
					                             ->getCardsApi()
 | 
				
			||||||
                             ->createCard($body);
 | 
					                             ->createCard($body);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $card = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if ($api_response->isSuccess()) {
 | 
					        if ($api_response->isSuccess()) {
 | 
				
			||||||
            $result = $api_response->getResult();
 | 
					            $card = $api_response->getBody();
 | 
				
			||||||
            nlog($result->getBody());
 | 
					            $card = json_decode($card);
 | 
				
			||||||
            nlog("ressy");
 | 
					 | 
				
			||||||
            nlog($result);
 | 
					 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            $errors = $api_response->getErrors();
 | 
					            $errors = $api_response->getErrors();
 | 
				
			||||||
            nlog("i got errors");
 | 
					
 | 
				
			||||||
            nlog($errors);
 | 
					            return $this->processUnsuccessfulPayment($errors);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					        /* Create the token in Invoice Ninja*/
 | 
				
			||||||
 * 
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
  "card": {
 | 
					 | 
				
			||||||
    "id": "ccof:uIbfJXhXETSP197M3GB", //this is the token
 | 
					 | 
				
			||||||
    "billing_address": {
 | 
					 | 
				
			||||||
      "address_line_1": "500 Electric Ave",
 | 
					 | 
				
			||||||
      "address_line_2": "Suite 600",
 | 
					 | 
				
			||||||
      "locality": "New York",
 | 
					 | 
				
			||||||
      "administrative_district_level_1": "NY",
 | 
					 | 
				
			||||||
      "postal_code": "10003",
 | 
					 | 
				
			||||||
      "country": "US"
 | 
					 | 
				
			||||||
    },
 | 
					 | 
				
			||||||
    "bin": "411111",
 | 
					 | 
				
			||||||
    "card_brand": "VISA",
 | 
					 | 
				
			||||||
    "card_type": "CREDIT",
 | 
					 | 
				
			||||||
    "cardholder_name": "Amelia Earhart",
 | 
					 | 
				
			||||||
    "customer_id": "Q6VKKKGW8GWQNEYMDRMV01QMK8",
 | 
					 | 
				
			||||||
    "enabled": true,
 | 
					 | 
				
			||||||
    "exp_month": 11,
 | 
					 | 
				
			||||||
    "exp_year": 2018,
 | 
					 | 
				
			||||||
    "last_4": "1111",
 | 
					 | 
				
			||||||
    "prepaid_type": "NOT_PREPAID",
 | 
					 | 
				
			||||||
    "reference_id": "user-id-1",
 | 
					 | 
				
			||||||
    "version": 1
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
*/
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $cgt = [];
 | 
					        $cgt = [];
 | 
				
			||||||
        $cgt['token'] = $result->getId();
 | 
					        $cgt['token'] = $card->card->id;
 | 
				
			||||||
        $cgt['payment_method_id'] = GatewayType::CREDIT_CARD;
 | 
					        $cgt['payment_method_id'] = GatewayType::CREDIT_CARD;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $payment_meta = new \stdClass;
 | 
					        $payment_meta = new \stdClass;
 | 
				
			||||||
        $payment_meta->exp_month = $result->getExpMonth();
 | 
					        $payment_meta->exp_month = $card->card->exp_month;
 | 
				
			||||||
        $payment_meta->exp_year = $result->getExpYear();
 | 
					        $payment_meta->exp_year = $card->card->exp_year;
 | 
				
			||||||
        $payment_meta->brand = $result->getCardBrand();
 | 
					        $payment_meta->brand = $card->card->card_brand;
 | 
				
			||||||
        $payment_meta->last4 = $result->getLast4();
 | 
					        $payment_meta->last4 = $card->card->last_4;
 | 
				
			||||||
        $payment_meta->type = GatewayType::CREDIT_CARD;
 | 
					        $payment_meta->type = GatewayType::CREDIT_CARD;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $cgt['payment_meta'] = $payment_meta;
 | 
					        $cgt['payment_meta'] = $payment_meta;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $token = $this->square_driver->storeGatewayToken($cgt, []);
 | 
					        $token = $this->square_driver->storeGatewayToken($cgt, []);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return redirect()->route('client.payment_methods.index');
 | 
				
			||||||
        return back();
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function paymentView($data)
 | 
					    public function paymentView($data)
 | 
				
			||||||
@ -291,18 +160,20 @@ Success response looks like this:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    private function processUnsuccessfulPayment($response)
 | 
					    private function processUnsuccessfulPayment($response)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        /*Harvest your own errors here*/
 | 
					        // array (
 | 
				
			||||||
        // $error = $response->status_message;
 | 
					        //   0 => 
 | 
				
			||||||
 | 
					        //   Square\Models\Error::__set_state(array(
 | 
				
			||||||
        // if(property_exists($response, 'approval_message') && $response->approval_message)
 | 
					        //      'category' => 'INVALID_REQUEST_ERROR',
 | 
				
			||||||
        //     $error .= " - {$response->approval_message}";
 | 
					        //      'code' => 'INVALID_CARD_DATA',
 | 
				
			||||||
 | 
					        //      'detail' => 'Invalid card data.',
 | 
				
			||||||
        // $error_code = property_exists($response, 'approval_message') ? $response->approval_message : 'Undefined code';
 | 
					        //      'field' => 'source_id',
 | 
				
			||||||
 | 
					        //   )),
 | 
				
			||||||
 | 
					        // )  
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $data = [
 | 
					        $data = [
 | 
				
			||||||
            'response' => $response,
 | 
					            'response' => $response,
 | 
				
			||||||
            'error' => $error,
 | 
					            'error' => $response[0]['detail'],
 | 
				
			||||||
            'error_code' => $error_code,
 | 
					            'error_code' => '',
 | 
				
			||||||
        ];
 | 
					        ];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return $this->square_driver->processUnsuccessfulTransaction($data);
 | 
					        return $this->square_driver->processUnsuccessfulTransaction($data);
 | 
				
			||||||
@ -310,19 +181,80 @@ Success response looks like this:
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* Helpers */
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /*
 | 
					 | 
				
			||||||
      You will need some helpers to handle successful and unsuccessful responses
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      Some considerations after a succesful transaction include:
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      Logging of events: success +/- failure
 | 
					 | 
				
			||||||
      Recording a payment 
 | 
					 | 
				
			||||||
      Notifications
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private function findOrCreateClient()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $email_address = new \Square\Models\CustomerTextFilter();
 | 
				
			||||||
 | 
					        $email_address->setExact($this->square_driver->client->present()->email());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $filter = new \Square\Models\CustomerFilter();
 | 
				
			||||||
 | 
					        $filter->setEmailAddress($email_address);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $query = new \Square\Models\CustomerQuery();
 | 
				
			||||||
 | 
					        $query->setFilter($filter);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $body = new \Square\Models\SearchCustomersRequest();
 | 
				
			||||||
 | 
					        $body->setQuery($query);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $api_response = $this->square_driver
 | 
				
			||||||
 | 
					                             ->init()
 | 
				
			||||||
 | 
					                             ->square
 | 
				
			||||||
 | 
					                             ->getCustomersApi()
 | 
				
			||||||
 | 
					                             ->searchCustomers($body);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $customers = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if ($api_response->isSuccess()) {
 | 
				
			||||||
 | 
					            $customers = $api_response->getBody();
 | 
				
			||||||
 | 
					            $customers = json_decode($customers);
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            $errors = $api_response->getErrors();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if($customers)
 | 
				
			||||||
 | 
					            return $customers->customers[0]->id;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return $this->createClient();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private function createClient()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /* Step two - create the customer */
 | 
				
			||||||
 | 
					        $billing_address = new \Square\Models\Address();
 | 
				
			||||||
 | 
					        $billing_address->setAddressLine1($this->square_driver->client->address1);
 | 
				
			||||||
 | 
					        $billing_address->setAddressLine2($this->square_driver->client->address2);
 | 
				
			||||||
 | 
					        $billing_address->setLocality($this->square_driver->client->city);
 | 
				
			||||||
 | 
					        $billing_address->setAdministrativeDistrictLevel1($this->square_driver->client->state);
 | 
				
			||||||
 | 
					        $billing_address->setPostalCode($this->square_driver->client->postal_code);
 | 
				
			||||||
 | 
					        $billing_address->setCountry($this->square_driver->client->country->iso_3166_2);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $body = new \Square\Models\CreateCustomerRequest();
 | 
				
			||||||
 | 
					        $body->setGivenName($this->square_driver->client->present()->name());
 | 
				
			||||||
 | 
					        $body->setFamilyName('');
 | 
				
			||||||
 | 
					        $body->setEmailAddress($this->square_driver->client->present()->email());
 | 
				
			||||||
 | 
					        $body->setAddress($billing_address);
 | 
				
			||||||
 | 
					        $body->setPhoneNumber($this->square_driver->client->phone);
 | 
				
			||||||
 | 
					        $body->setReferenceId($this->square_driver->client->number);
 | 
				
			||||||
 | 
					        $body->setNote('Created by Invoice Ninja.');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $api_response = $this->square_driver
 | 
				
			||||||
 | 
					                             ->init()
 | 
				
			||||||
 | 
					                             ->square
 | 
				
			||||||
 | 
					                             ->getCustomersApi()
 | 
				
			||||||
 | 
					                             ->createCustomer($body);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if ($api_response->isSuccess()) {
 | 
				
			||||||
 | 
					            $result = $api_response->getResult();
 | 
				
			||||||
 | 
					            return $result->getCustomer()->getId();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            $errors = $api_response->getErrors();
 | 
				
			||||||
 | 
					            return $this->processUnsuccessfulPayment($errors);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user