2019-09-05 09:00:12 +02:00
< ? php
2020-06-24 16:07:12 +02:00
2019-09-05 09:00:12 +02:00
/**
2020-09-06 11:38:10 +02:00
* Invoice Ninja ( https :// invoiceninja . com ) .
2019-09-05 09:00:12 +02:00
*
* @ link https :// github . com / invoiceninja / invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @ copyright Copyright ( c ) 2024. Invoice Ninja LLC ( https :// invoiceninja . com )
2019-09-05 09:00:12 +02:00
*
2021-06-16 08:58:16 +02:00
* @ license https :// www . elastic . co / licensing / elastic - license
2019-09-05 09:00:12 +02:00
*/
namespace App\PaymentDrivers ;
2024-07-24 04:27:09 +02:00
use Exception ;
use Stripe\Stripe ;
use Stripe\Account ;
use Stripe\Customer ;
2023-01-16 21:21:04 +01:00
use App\Models\Client ;
2023-03-18 08:24:56 +01:00
use App\Models\Payment ;
2024-07-24 04:27:09 +02:00
use Stripe\SetupIntent ;
use Stripe\StripeClient ;
2023-03-18 08:24:56 +01:00
use App\Models\SystemLog ;
2024-07-24 04:27:09 +02:00
use Stripe\PaymentIntent ;
use Stripe\PaymentMethod ;
use App\Models\GatewayType ;
use App\Models\PaymentHash ;
use App\Http\Requests\Request ;
use App\Jobs\Util\SystemLogger ;
use App\Utils\Traits\MakesHash ;
use App\Exceptions\PaymentFailed ;
use App\Models\ClientGatewayToken ;
2021-01-19 16:07:58 +01:00
use App\PaymentDrivers\Stripe\ACH ;
2024-07-24 04:27:09 +02:00
use App\PaymentDrivers\Stripe\EPS ;
use App\PaymentDrivers\Stripe\FPX ;
2022-01-24 04:24:47 +01:00
use App\PaymentDrivers\Stripe\ACSS ;
2022-12-16 11:25:23 +01:00
use App\PaymentDrivers\Stripe\BACS ;
2023-03-18 08:24:56 +01:00
use App\PaymentDrivers\Stripe\BECS ;
2024-07-24 04:27:09 +02:00
use App\PaymentDrivers\Stripe\SEPA ;
2023-03-18 08:24:56 +01:00
use App\PaymentDrivers\Stripe\iDeal ;
2024-07-24 04:27:09 +02:00
use App\PaymentDrivers\Stripe\Alipay ;
use App\PaymentDrivers\Stripe\Charge ;
2023-03-18 08:24:56 +01:00
use App\PaymentDrivers\Stripe\Klarna ;
use App\PaymentDrivers\Stripe\SOFORT ;
use Illuminate\Http\RedirectResponse ;
2024-07-24 04:27:09 +02:00
use App\PaymentDrivers\Stripe\GIROPAY ;
2023-03-18 08:24:56 +01:00
use Stripe\Exception\ApiErrorException ;
2024-07-24 04:27:09 +02:00
use App\Exceptions\StripeConnectFailure ;
use App\PaymentDrivers\Stripe\Utilities ;
use App\PaymentDrivers\Stripe\Bancontact ;
use App\PaymentDrivers\Stripe\BrowserPay ;
use App\PaymentDrivers\Stripe\CreditCard ;
use App\PaymentDrivers\Stripe\PRZELEWY24 ;
use App\PaymentDrivers\Stripe\BankTransfer ;
use App\PaymentDrivers\Stripe\Connect\Verify ;
use App\PaymentDrivers\Stripe\ImportCustomers ;
use App\PaymentDrivers\Stripe\Jobs\ChargeRefunded ;
use App\Http\Requests\Payments\PaymentWebhookRequest ;
use Laracasts\Presenter\Exceptions\PresenterException ;
use App\PaymentDrivers\Stripe\Jobs\PaymentIntentWebhook ;
use App\PaymentDrivers\Stripe\Jobs\PaymentIntentFailureWebhook ;
use App\PaymentDrivers\Stripe\Jobs\PaymentIntentProcessingWebhook ;
use App\PaymentDrivers\Stripe\Jobs\PaymentIntentPartiallyFundedWebhook ;
2019-09-05 14:42:26 +02:00
2020-10-10 05:32:10 +02:00
class StripePaymentDriver extends BaseDriver
2019-09-05 09:00:12 +02:00
{
2024-01-14 05:05:00 +01:00
use MakesHash ;
use Utilities ;
2019-09-25 07:55:52 +02:00
2020-07-15 07:05:02 +02:00
public $refundable = true ;
2019-09-05 09:00:12 +02:00
2020-07-15 07:05:02 +02:00
public $token_billing = true ;
2019-09-06 07:22:05 +02:00
2020-07-15 07:05:02 +02:00
public $can_authorise_credit_card = true ;
2019-09-15 13:40:46 +02:00
2021-06-29 12:32:44 +02:00
/** @var StripeClient */
2020-11-01 18:47:48 +01:00
public $stripe ;
2020-09-18 14:35:53 +02:00
2019-12-30 22:59:12 +01:00
protected $customer_reference = 'customerReferenceParam' ;
2019-09-06 07:22:05 +02:00
2020-10-10 05:32:10 +02:00
public $payment_method ;
2020-06-01 14:03:18 +02:00
2021-04-20 13:30:52 +02:00
public $stripe_connect = false ;
public $stripe_connect_auth = [];
2020-06-23 16:47:29 +02:00
public static $methods = [
GatewayType :: CREDIT_CARD => CreditCard :: class ,
2021-01-19 16:07:58 +01:00
GatewayType :: BANK_TRANSFER => ACH :: class ,
2020-06-23 16:47:29 +02:00
GatewayType :: ALIPAY => Alipay :: class ,
GatewayType :: SOFORT => SOFORT :: class ,
2021-10-31 14:50:54 +01:00
GatewayType :: APPLE_PAY => BrowserPay :: class ,
2021-10-06 07:54:58 +02:00
GatewayType :: SEPA => SEPA :: class ,
2021-10-09 16:45:38 +02:00
GatewayType :: PRZELEWY24 => PRZELEWY24 :: class ,
2021-10-09 10:09:04 +02:00
GatewayType :: GIROPAY => GIROPAY :: class ,
2021-10-09 16:13:04 +02:00
GatewayType :: IDEAL => iDeal :: class ,
2021-10-10 06:51:51 +02:00
GatewayType :: EPS => EPS :: class ,
2021-10-10 11:43:55 +02:00
GatewayType :: BANCONTACT => Bancontact :: class ,
2021-10-11 18:15:34 +02:00
GatewayType :: BECS => BECS :: class ,
2021-10-12 16:46:42 +02:00
GatewayType :: ACSS => ACSS :: class ,
2022-01-21 04:35:16 +01:00
GatewayType :: FPX => FPX :: class ,
2022-12-09 00:10:42 +01:00
GatewayType :: KLARNA => Klarna :: class ,
2022-12-16 11:25:23 +01:00
GatewayType :: BACS => BACS :: class ,
2023-02-23 06:52:45 +01:00
GatewayType :: DIRECT_DEBIT => BankTransfer :: class ,
2020-06-23 16:47:29 +02:00
];
2024-01-14 05:05:00 +01:00
public const SYSTEM_LOG_TYPE = SystemLog :: TYPE_STRIPE ;
2020-11-16 13:36:29 +01:00
2019-09-17 12:27:48 +02:00
/**
2020-09-06 11:38:10 +02:00
* Initializes the Stripe API .
2023-05-08 12:33:21 +02:00
* @ return self
2019-09-17 12:27:48 +02:00
*/
2021-08-18 02:15:11 +02:00
public function init ()
2019-12-30 22:59:12 +01:00
{
2022-06-21 11:57:17 +02:00
if ( $this -> stripe_connect ) {
2021-04-20 13:30:52 +02:00
Stripe :: setApiKey ( config ( 'ninja.ninja_stripe_key' ));
2021-05-11 15:15:51 +02:00
2022-06-21 11:57:17 +02:00
if ( strlen ( $this -> company_gateway -> getConfigField ( 'account_id' )) > 1 ) {
$this -> stripe_connect_auth = [ 'stripe_account' => $this -> company_gateway -> getConfigField ( 'account_id' )];
} else {
2021-08-15 08:21:18 +02:00
throw new StripeConnectFailure ( 'Stripe Connect has not been configured' );
2022-06-21 11:57:17 +02:00
}
} else {
2021-04-20 13:30:52 +02:00
$this -> stripe = new StripeClient (
$this -> company_gateway -> getConfigField ( 'apiKey' )
);
2020-09-18 14:35:53 +02:00
2021-04-20 13:30:52 +02:00
Stripe :: setApiKey ( $this -> company_gateway -> getConfigField ( 'apiKey' ));
2022-12-05 12:06:31 +01:00
Stripe :: setApiVersion ( '2022-11-15' );
2023-11-16 04:47:23 +01:00
// Stripe::setAPiVersion('2023-08-16');
2021-04-20 13:30:52 +02:00
}
2021-08-18 02:15:11 +02:00
return $this ;
2019-12-30 22:59:12 +01:00
}
2019-09-17 12:27:48 +02:00
2020-06-23 16:47:29 +02:00
public function setPaymentMethod ( $payment_method_id )
2020-06-01 14:03:18 +02:00
{
2020-06-23 16:47:29 +02:00
$class = self :: $methods [ $payment_method_id ];
2020-06-24 16:07:12 +02:00
2020-06-23 16:47:29 +02:00
$this -> payment_method = new $class ( $this );
2020-06-01 14:03:18 +02:00
return $this ;
}
2024-01-14 05:05:00 +01:00
2019-12-30 22:59:12 +01:00
/**
2020-09-06 11:38:10 +02:00
* Returns the gateway types .
2019-12-30 22:59:12 +01:00
*/
2020-06-24 16:07:12 +02:00
public function gatewayTypes () : array
2019-09-08 14:13:55 +02:00
{
$types = [
2021-10-31 14:50:54 +01:00
GatewayType :: CREDIT_CARD ,
2024-04-06 01:57:58 +02:00
GatewayType :: APPLE_PAY ,
2021-10-04 18:09:10 +02:00
];
2021-06-29 12:32:44 +02:00
2021-03-22 12:18:57 +01:00
if ( $this -> client
2021-01-27 13:36:10 +01:00
&& isset ( $this -> client -> country )
&& in_array ( $this -> client -> country -> iso_3166_3 , [ 'AUT' , 'BEL' , 'DEU' , 'ITA' , 'NLD' , 'ESP' ])) {
2019-09-08 14:13:55 +02:00
$types [] = GatewayType :: SOFORT ;
2019-12-30 22:59:12 +01:00
}
2019-09-08 14:13:55 +02:00
2021-03-22 12:18:57 +01:00
if ( $this -> client
2021-01-27 13:36:10 +01:00
&& isset ( $this -> client -> country )
2023-02-24 02:16:40 +01:00
&& ( in_array ( $this -> client -> country -> iso_3166_3 , [ 'USA' ]) || ( $this -> client -> gateway_tokens () -> where ( 'gateway_type_id' , GatewayType :: BANK_TRANSFER ) -> exists ()))
2023-02-16 02:36:09 +01:00
) {
2021-01-27 13:36:10 +01:00
$types [] = GatewayType :: BANK_TRANSFER ;
}
2021-03-22 12:18:57 +01:00
if ( $this -> client
2024-03-30 20:45:34 +01:00
&& $this -> client -> currency ()
&& in_array ( $this -> client -> currency () -> code , [ 'CNY' , 'AUD' , 'CAD' , 'EUR' , 'GBP' , 'HKD' , 'JPY' , 'SGD' , 'MYR' , 'NZD' , 'USD' ])) {
// && isset($this->client->country)
// && in_array($this->client->country->iso_3166_3, ['AUS', 'DNK', 'DEU', 'ITA', 'LUX', 'NOR', 'SVN', 'GBR', 'AUT', 'EST', 'GRC', 'JPN', 'MYS', 'PRT', 'ESP', 'USA', 'BEL', 'FIN', 'HKG', 'LVA', 'NLD', 'SGP', 'SWE', 'CAN', 'FRA', 'IRL', 'LTU', 'NZL', 'SVK', 'CHE'])) {
2021-01-27 13:36:10 +01:00
$types [] = GatewayType :: ALIPAY ;
}
2021-10-04 18:09:10 +02:00
2021-10-04 17:33:35 +02:00
if ( $this -> client
2021-10-09 23:39:50 +02:00
&& $this -> client -> currency ()
&& ( $this -> client -> currency () -> code == 'EUR' )
2021-10-04 17:33:35 +02:00
&& isset ( $this -> client -> country )
2022-06-21 11:57:17 +02:00
&& in_array ( $this -> client -> country -> iso_3166_3 , [ 'AUT' , 'BEL' , 'CHE' , 'CYP' , 'CZE' , 'BGR' , 'DNK' , 'DEU' , 'ESP' , 'FIN' , 'FRA' , 'HUN' , 'IRL' , 'ITA' , 'LVA' , 'LUX' , 'LTA' , 'MLT' , 'NLD' , 'NOR' , 'POL' , 'ROU' , 'SVK' , 'SVN' , 'SWE' , 'GBR' , 'EST' , 'GRC' , 'PRT' ])) { // TODO: More has to be added https://stripe.com/docs/payments/sepa-debit
2021-10-06 07:54:58 +02:00
$types [] = GatewayType :: SEPA ;
2021-10-04 17:33:35 +02:00
}
2021-01-27 13:36:10 +01:00
2021-10-10 00:10:59 +02:00
if ( $this -> client
2021-10-09 16:45:38 +02:00
&& isset ( $this -> client -> country )
2022-06-21 11:57:17 +02:00
&& in_array ( $this -> client -> country -> iso_3166_3 , [ 'POL' ])) {
2021-10-09 16:45:38 +02:00
$types [] = GatewayType :: PRZELEWY24 ;
}
2022-06-21 11:57:17 +02:00
if ( $this -> client
2021-10-10 00:37:42 +02:00
&& $this -> client -> currency ()
&& ( $this -> client -> currency () -> code == 'EUR' )
2021-10-09 10:09:04 +02:00
&& isset ( $this -> client -> country )
2022-06-21 11:57:17 +02:00
&& in_array ( $this -> client -> country -> iso_3166_3 , [ 'DEU' ])) {
2021-10-09 10:09:04 +02:00
$types [] = GatewayType :: GIROPAY ;
2021-10-10 09:58:32 +02:00
}
2021-10-10 11:43:55 +02:00
2021-10-09 23:39:50 +02:00
if ( $this -> client
&& $this -> client -> currency ()
&& ( $this -> client -> currency () -> code == 'EUR' )
2021-10-09 11:23:53 +02:00
&& isset ( $this -> client -> country )
2022-06-21 11:57:17 +02:00
&& in_array ( $this -> client -> country -> iso_3166_3 , [ 'NLD' ])) {
2021-10-09 11:23:53 +02:00
$types [] = GatewayType :: IDEAL ;
2022-06-21 11:57:17 +02:00
}
2021-10-09 11:23:53 +02:00
2021-10-10 06:51:51 +02:00
if ( $this -> client
&& $this -> client -> currency ()
&& ( $this -> client -> currency () -> code == 'EUR' )
&& isset ( $this -> client -> country )
2022-06-21 11:57:17 +02:00
&& in_array ( $this -> client -> country -> iso_3166_3 , [ 'AUT' ])) {
2021-10-10 06:51:51 +02:00
$types [] = GatewayType :: EPS ;
2022-06-21 11:57:17 +02:00
}
2021-10-10 06:51:51 +02:00
2022-01-20 08:29:30 +01:00
if ( $this -> client
&& $this -> client -> currency ()
&& ( $this -> client -> currency () -> code == 'MYR' )
&& isset ( $this -> client -> country )
2022-06-21 11:57:17 +02:00
&& in_array ( $this -> client -> country -> iso_3166_3 , [ 'MYS' ])) {
2022-01-20 08:29:30 +01:00
$types [] = GatewayType :: FPX ;
2022-06-21 11:57:17 +02:00
}
2022-01-20 08:29:30 +01:00
2021-10-10 11:43:55 +02:00
if ( $this -> client
&& $this -> client -> currency ()
&& ( $this -> client -> currency () -> code == 'EUR' )
&& isset ( $this -> client -> country )
2022-06-21 11:57:17 +02:00
&& in_array ( $this -> client -> country -> iso_3166_3 , [ 'BEL' ])) {
2021-10-10 11:43:55 +02:00
$types [] = GatewayType :: BANCONTACT ;
2022-06-21 11:57:17 +02:00
}
2021-10-10 11:43:55 +02:00
2021-10-11 18:15:34 +02:00
if ( $this -> client
&& $this -> client -> currency ()
&& ( $this -> client -> currency () -> code == 'AUD' )
&& isset ( $this -> client -> country )
2022-06-21 11:57:17 +02:00
&& in_array ( $this -> client -> country -> iso_3166_3 , [ 'AUS' ])) {
2021-10-11 18:15:34 +02:00
$types [] = GatewayType :: BECS ;
2022-06-21 11:57:17 +02:00
}
2021-10-11 18:15:34 +02:00
2021-10-12 16:46:42 +02:00
if ( $this -> client
&& $this -> client -> currency ()
&& in_array ( $this -> client -> currency () -> code , [ 'CAD' , 'USD' ])
&& isset ( $this -> client -> country )
2022-06-21 11:57:17 +02:00
&& in_array ( $this -> client -> country -> iso_3166_3 , [ 'CAN' , 'USA' ])) {
2021-10-12 16:46:42 +02:00
$types [] = GatewayType :: ACSS ;
2022-06-21 11:57:17 +02:00
}
2022-12-16 11:02:49 +01:00
if ( $this -> client
&& $this -> client -> currency ()
2022-12-16 11:25:23 +01:00
&& in_array ( $this -> client -> currency () -> code , [ 'GBP' ])
2022-12-16 11:02:49 +01:00
&& isset ( $this -> client -> country )
&& in_array ( $this -> client -> country -> iso_3166_3 , [ 'GBR' ])) {
$types [] = GatewayType :: BACS ;
}
2022-12-05 08:12:05 +01:00
if ( $this -> client
&& $this -> client -> currency ()
2022-12-09 14:25:18 +01:00
&& in_array ( $this -> client -> currency () -> code , [ 'EUR' , 'DKK' , 'GBP' , 'NOK' , 'SEK' , 'AUD' , 'NZD' , 'CAD' , 'PLN' , 'CHF' ])
2022-12-05 08:12:05 +01:00
&& isset ( $this -> client -> country )
2022-12-09 14:25:18 +01:00
&& in_array ( $this -> client -> country -> iso_3166_3 , [ 'AUT' , 'BEL' , 'DNK' , 'FIN' , 'FRA' , 'DEU' , 'IRL' , 'ITA' , 'NLD' , 'NOR' , 'ESP' , 'SWE' , 'GBR' ])) {
$types [] = GatewayType :: KLARNA ;
}
if ( $this -> client
&& $this -> client -> currency ()
&& in_array ( $this -> client -> currency () -> code , [ 'EUR' , 'DKK' , 'GBP' , 'NOK' , 'SEK' , 'AUD' , 'NZD' , 'CAD' , 'PLN' , 'CHF' , 'USD' ])
2022-12-05 08:12:05 +01:00
&& isset ( $this -> client -> country )
2022-12-07 09:18:09 +01:00
&& in_array ( $this -> client -> country -> iso_3166_3 , [ 'AUT' , 'BEL' , 'DNK' , 'FIN' , 'FRA' , 'DEU' , 'IRL' , 'ITA' , 'NLD' , 'NOR' , 'ESP' , 'SWE' , 'GBR' , 'USA' ])) {
2022-12-05 08:12:05 +01:00
$types [] = GatewayType :: KLARNA ;
}
2024-06-14 09:09:44 +02:00
2023-02-23 06:52:45 +01:00
if (
$this -> client
&& isset ( $this -> client -> country )
2023-02-26 09:57:30 +01:00
&& (
( in_array ( $this -> client -> country -> iso_3166_2 , [ 'FR' , 'IE' , 'NL' , 'DE' , 'ES' ]) && $this -> client -> currency () -> code == 'EUR' ) ||
( $this -> client -> country -> iso_3166_2 == 'JP' && $this -> client -> currency () -> code == 'JPY' ) ||
( $this -> client -> country -> iso_3166_2 == 'MX' && $this -> client -> currency () -> code == 'MXN' ) ||
2024-04-23 04:45:43 +02:00
( $this -> client -> country -> iso_3166_2 == 'GB' && $this -> client -> currency () -> code == 'GBP' ) ||
( $this -> client -> country -> iso_3166_2 == 'US' && $this -> client -> currency () -> code == 'USD' )
2023-02-26 09:57:30 +01:00
)
2023-02-23 06:52:45 +01:00
) {
$types [] = GatewayType :: DIRECT_DEBIT ;
}
2019-09-08 14:13:55 +02:00
return $types ;
2019-09-12 13:46:09 +02:00
}
2019-09-18 04:39:53 +02:00
public function viewForType ( $gateway_type_id )
2019-09-12 13:46:09 +02:00
{
2019-12-30 22:59:12 +01:00
switch ( $gateway_type_id ) {
case GatewayType :: CREDIT_CARD :
2020-03-23 18:10:42 +01:00
return 'gateways.stripe.credit_card' ;
2019-12-30 22:59:12 +01:00
case GatewayType :: SOFORT :
2020-03-23 18:10:42 +01:00
return 'gateways.stripe.sofort' ;
2019-12-30 22:59:12 +01:00
case GatewayType :: BANK_TRANSFER :
2020-03-23 18:10:42 +01:00
return 'gateways.stripe.ach' ;
2019-12-30 22:59:12 +01:00
case GatewayType :: SEPA :
2020-03-23 18:10:42 +01:00
return 'gateways.stripe.sepa' ;
2021-10-09 16:45:38 +02:00
case GatewayType :: PRZELEWY24 :
return 'gateways.stripe.przelewy24' ;
2019-12-30 22:59:12 +01:00
case GatewayType :: CRYPTO :
case GatewayType :: ALIPAY :
case GatewayType :: APPLE_PAY :
2020-03-23 18:10:42 +01:00
return 'gateways.stripe.other' ;
2021-10-09 11:24:47 +02:00
case GatewayType :: GIROPAY :
return 'gateways.stripe.giropay' ;
2022-12-05 08:12:05 +01:00
case GatewayType :: KLARNA :
return 'gateways.stripe.klarna' ;
2021-10-09 11:23:53 +02:00
case GatewayType :: IDEAL :
return 'gateways.stripe.ideal' ;
2021-10-10 06:51:51 +02:00
case GatewayType :: EPS :
return 'gateways.stripe.eps' ;
2021-10-10 11:43:55 +02:00
case GatewayType :: BANCONTACT :
return 'gateways.stripe.bancontact' ;
2021-10-11 18:15:34 +02:00
case GatewayType :: BECS :
return 'gateways.stripe.becs' ;
2022-12-16 11:02:49 +01:00
case GatewayType :: BACS :
return 'gateways.stripe.bacs' ;
2021-10-12 16:46:42 +02:00
case GatewayType :: ACSS :
return 'gateways.stripe.acss' ;
2022-01-20 08:29:30 +01:00
case GatewayType :: FPX :
return 'gateways.stripe.fpx' ;
2019-12-30 22:59:12 +01:00
default :
break ;
}
2019-09-08 14:13:55 +02:00
}
2019-09-13 00:33:48 +02:00
2021-01-13 13:31:00 +01:00
public function getClientRequiredFields () : array
{
2021-06-28 12:55:31 +02:00
$fields = [];
2021-02-15 14:58:25 +01:00
if ( $this -> company_gateway -> require_client_name ) {
$fields [] = [ 'name' => 'client_name' , 'label' => ctrans ( 'texts.client_name' ), 'type' => 'text' , 'validation' => 'required' ];
}
2024-03-05 01:26:34 +01:00
// if ($this->company_gateway->require_contact_name) {
2024-06-14 09:09:44 +02:00
$fields [] = [ 'name' => 'contact_first_name' , 'label' => ctrans ( 'texts.first_name' ), 'type' => 'text' , 'validation' => 'required' ];
$fields [] = [ 'name' => 'contact_last_name' , 'label' => ctrans ( 'texts.last_name' ), 'type' => 'text' , 'validation' => 'required' ];
2024-03-05 01:26:34 +01:00
// }
2021-02-15 14:58:25 +01:00
2024-03-05 01:26:34 +01:00
// if ($this->company_gateway->require_contact_email) {
2024-06-14 09:09:44 +02:00
$fields [] = [ 'name' => 'contact_email' , 'label' => ctrans ( 'texts.email' ), 'type' => 'text' , 'validation' => 'required,email:rfc' ];
2024-03-05 01:26:34 +01:00
// }
2021-02-15 14:58:25 +01:00
2021-06-28 12:55:31 +02:00
if ( $this -> company_gateway -> require_client_phone ) {
$fields [] = [ 'name' => 'client_phone' , 'label' => ctrans ( 'texts.client_phone' ), 'type' => 'tel' , 'validation' => 'required' ];
}
2021-02-15 14:58:25 +01:00
if ( $this -> company_gateway -> require_billing_address ) {
$fields [] = [ 'name' => 'client_address_line_1' , 'label' => ctrans ( 'texts.address1' ), 'type' => 'text' , 'validation' => 'required' ];
2023-10-26 04:57:44 +02:00
// $fields[] = ['name' => 'client_address_line_2', 'label' => ctrans('texts.address2'), 'type' => 'text', 'validation' => 'nullable'];
2021-02-15 14:58:25 +01:00
$fields [] = [ 'name' => 'client_city' , 'label' => ctrans ( 'texts.city' ), 'type' => 'text' , 'validation' => 'required' ];
$fields [] = [ 'name' => 'client_state' , 'label' => ctrans ( 'texts.state' ), 'type' => 'text' , 'validation' => 'required' ];
$fields [] = [ 'name' => 'client_country_id' , 'label' => ctrans ( 'texts.country' ), 'type' => 'text' , 'validation' => 'required' ];
}
2021-06-29 12:32:44 +02:00
2022-06-21 11:57:17 +02:00
if ( $this -> company_gateway -> require_postal_code ) {
2021-07-15 13:00:24 +02:00
$fields [] = [ 'name' => 'client_postal_code' , 'label' => ctrans ( 'texts.postal_code' ), 'type' => 'text' , 'validation' => 'required' ];
2021-08-09 14:05:39 +02:00
}
2021-02-15 14:58:25 +01:00
if ( $this -> company_gateway -> require_shipping_address ) {
$fields [] = [ 'name' => 'client_shipping_address_line_1' , 'label' => ctrans ( 'texts.shipping_address1' ), 'type' => 'text' , 'validation' => 'required' ];
2023-10-26 04:57:44 +02:00
// $fields[] = ['name' => 'client_shipping_address_line_2', 'label' => ctrans('texts.shipping_address2'), 'type' => 'text', 'validation' => 'sometimes'];
2021-02-15 14:58:25 +01:00
$fields [] = [ 'name' => 'client_shipping_city' , 'label' => ctrans ( 'texts.shipping_city' ), 'type' => 'text' , 'validation' => 'required' ];
$fields [] = [ 'name' => 'client_shipping_state' , 'label' => ctrans ( 'texts.shipping_state' ), 'type' => 'text' , 'validation' => 'required' ];
$fields [] = [ 'name' => 'client_shipping_postal_code' , 'label' => ctrans ( 'texts.shipping_postal_code' ), 'type' => 'text' , 'validation' => 'required' ];
$fields [] = [ 'name' => 'client_shipping_country_id' , 'label' => ctrans ( 'texts.shipping_country' ), 'type' => 'text' , 'validation' => 'required' ];
}
2023-02-02 08:19:37 +01:00
if ( $this -> company_gateway -> require_custom_value1 ) {
$fields [] = [ 'name' => 'client_custom_value1' , 'label' => $this -> helpers -> makeCustomField ( $this -> client -> company -> custom_fields , 'client1' ), 'type' => 'text' , 'validation' => 'required' ];
}
2024-01-14 05:05:00 +01:00
2023-02-02 08:19:37 +01:00
if ( $this -> company_gateway -> require_custom_value2 ) {
$fields [] = [ 'name' => 'client_custom_value2' , 'label' => $this -> helpers -> makeCustomField ( $this -> client -> company -> custom_fields , 'client2' ), 'type' => 'text' , 'validation' => 'required' ];
}
if ( $this -> company_gateway -> require_custom_value3 ) {
$fields [] = [ 'name' => 'client_custom_value3' , 'label' => $this -> helpers -> makeCustomField ( $this -> client -> company -> custom_fields , 'client3' ), 'type' => 'text' , 'validation' => 'required' ];
}
if ( $this -> company_gateway -> require_custom_value4 ) {
$fields [] = [ 'name' => 'client_custom_value4' , 'label' => $this -> helpers -> makeCustomField ( $this -> client -> company -> custom_fields , 'client4' ), 'type' => 'text' , 'validation' => 'required' ];
}
2021-02-15 14:58:25 +01:00
return $fields ;
2021-01-13 13:31:00 +01:00
}
2019-09-25 07:55:52 +02:00
/**
2020-06-01 14:03:18 +02:00
* Proxy method to pass the data into payment method authorizeView () .
2020-09-06 11:38:10 +02:00
*
* @ param array $data
2024-06-16 00:30:25 +02:00
* @ return \Illuminate\Http\RedirectResponse | mixed
2019-09-25 07:55:52 +02:00
*/
2020-06-01 14:03:18 +02:00
public function authorizeView ( array $data )
2019-09-14 14:34:05 +02:00
{
2020-06-01 14:03:18 +02:00
return $this -> payment_method -> authorizeView ( $data );
2019-09-16 13:03:25 +02:00
}
2019-09-25 07:55:52 +02:00
/**
2020-03-23 18:10:42 +01:00
* Processes the gateway response for credit card authorization .
*
2020-12-21 11:46:10 +01:00
* @ param \Illuminate\Http\Request $request
2024-06-16 00:30:25 +02:00
* @ return \Illuminate\Http\RedirectResponse | mixed
2019-09-25 07:55:52 +02:00
*/
2020-06-26 13:25:58 +02:00
public function authorizeResponse ( $request )
2019-09-16 13:03:25 +02:00
{
2020-06-01 14:14:41 +02:00
return $this -> payment_method -> authorizeResponse ( $request );
2019-09-14 14:34:05 +02:00
}
2019-09-25 04:07:33 +02:00
/**
2020-03-23 18:10:42 +01:00
* Process the payment with gateway .
2019-12-30 22:59:12 +01:00
*
2020-03-23 18:10:42 +01:00
* @ param array $data
2024-06-16 00:30:25 +02:00
* @ return \Illuminate\Http\RedirectResponse | mixed
2019-09-25 04:07:33 +02:00
*/
2019-09-25 06:03:28 +02:00
public function processPaymentView ( array $data )
2019-09-25 04:07:33 +02:00
{
2020-06-01 14:29:41 +02:00
return $this -> payment_method -> paymentView ( $data );
2019-09-25 04:07:33 +02:00
}
2021-01-27 13:36:10 +01:00
public function processPaymentResponse ( $request )
2019-09-25 06:03:28 +02:00
{
2020-06-01 16:19:03 +02:00
return $this -> payment_method -> paymentResponse ( $request );
2019-10-01 11:59:32 +02:00
}
2019-09-13 00:33:48 +02:00
/**
2020-09-06 11:38:10 +02:00
* Creates a new String Payment Intent .
2019-12-30 22:59:12 +01:00
*
2020-10-28 11:10:49 +01:00
* @ param array $data The data array to be passed to Stripe
2019-09-13 00:33:48 +02:00
* @ return PaymentIntent The Stripe payment intent object
2020-12-21 11:46:10 +01:00
* @ throws ApiErrorException
2019-09-13 00:33:48 +02:00
*/
2020-09-06 11:38:10 +02:00
public function createPaymentIntent ( $data ) : ? PaymentIntent
2019-09-13 00:33:48 +02:00
{
2019-09-16 06:59:59 +02:00
$this -> init ();
2021-05-25 18:30:50 +02:00
2021-05-11 23:58:18 +02:00
$meta = $this -> stripe_connect_auth ;
2019-09-17 13:54:14 +02:00
2023-02-16 02:36:09 +01:00
return PaymentIntent :: create ( $data , array_merge ( $meta , [ 'idempotency_key' => uniqid ( " st " , true )]));
2019-09-13 00:33:48 +02:00
}
2019-09-14 14:34:05 +02:00
2023-02-27 05:32:37 +01:00
public function getPaymentIntent ( $payment_intent_id ) : ? PaymentIntent
{
$this -> init ();
2023-03-18 08:24:56 +01:00
return PaymentIntent :: retrieve (
$payment_intent_id ,
$this -> stripe_connect_auth
);
2019-09-13 00:33:48 +02:00
}
2019-09-14 14:34:05 +02:00
/**
2019-12-30 22:59:12 +01:00
* Returns a setup intent that allows the user
2019-09-17 13:54:14 +02:00
* to enter card details without initiating a transaction .
2019-09-14 14:34:05 +02:00
*
2020-10-28 11:10:49 +01:00
* @ return SetupIntent
2020-12-21 11:46:10 +01:00
* @ throws ApiErrorException
2019-09-14 14:34:05 +02:00
*/
2020-09-06 11:38:10 +02:00
public function getSetupIntent () : SetupIntent
2019-09-14 14:34:05 +02:00
{
2019-09-16 06:59:59 +02:00
$this -> init ();
2019-09-17 13:54:14 +02:00
2022-12-05 08:42:28 +01:00
$params = [ 'usage' => 'off_session' ];
2021-05-11 23:58:18 +02:00
$meta = $this -> stripe_connect_auth ;
2023-02-16 02:36:09 +01:00
return SetupIntent :: create ( $params , array_merge ( $meta , [ 'idempotency_key' => uniqid ( " st " , true )]));
2019-09-14 14:34:05 +02:00
}
2023-12-18 05:25:16 +01:00
public function getSetupIntentId ( string $id ) : SetupIntent
{
$this -> init ();
return SetupIntent :: retrieve (
$id ,
$this -> stripe_connect_auth
);
}
2019-09-17 13:54:14 +02:00
/**
2020-09-06 11:38:10 +02:00
* Returns the Stripe publishable key .
* @ return null | string The stripe publishable key
2019-09-17 13:54:14 +02:00
*/
2020-06-24 16:07:12 +02:00
public function getPublishableKey () : ? string
2019-09-14 14:34:05 +02:00
{
return $this -> company_gateway -> getPublishableKey ();
}
2019-09-16 06:59:59 +02:00
2024-01-14 05:05:00 +01:00
public function getCustomer ( $customer_id ) : ? Customer
2022-05-19 07:48:31 +02:00
{
$customer = Customer :: retrieve ( $customer_id , $this -> stripe_connect_auth );
2024-06-28 07:16:08 +02:00
return $customer ? ? null ; // @phpstan-ignore-line
2022-05-19 07:48:31 +02:00
}
2019-09-17 13:54:14 +02:00
/**
2020-09-06 11:38:10 +02:00
* Finds or creates a Stripe Customer object .
2019-12-30 22:59:12 +01:00
*
2020-10-28 11:10:49 +01:00
* @ return null | Customer A Stripe customer object
2021-06-29 12:32:44 +02:00
* @ throws PresenterException
2020-12-21 11:46:10 +01:00
* @ throws ApiErrorException
2019-09-17 13:54:14 +02:00
*/
2020-10-28 11:10:49 +01:00
public function findOrCreateCustomer () : ? Customer
2019-12-30 22:59:12 +01:00
{
2019-09-16 06:59:59 +02:00
$customer = null ;
$this -> init ();
2021-05-25 18:30:50 +02:00
2023-08-08 12:39:46 +02:00
$client_gateway_token = ClientGatewayToken :: query ()
-> whereClientId ( $this -> client -> id )
2021-10-01 04:57:34 +02:00
-> whereCompanyGatewayId ( $this -> company_gateway -> id )
-> first ();
2019-09-16 06:59:59 +02:00
2021-09-01 00:22:24 +02:00
//Search by customer reference
2019-12-30 22:59:12 +01:00
if ( $client_gateway_token && $client_gateway_token -> gateway_customer_reference ) {
2021-09-01 00:22:24 +02:00
$customer = Customer :: retrieve ( $client_gateway_token -> gateway_customer_reference , $this -> stripe_connect_auth );
2019-09-17 07:59:09 +02:00
2022-06-21 11:57:17 +02:00
if ( $customer ) {
2021-09-01 00:22:24 +02:00
return $customer ;
2022-06-21 11:57:17 +02:00
}
2021-10-04 18:09:10 +02:00
}
2021-09-01 00:22:24 +02:00
//Search by email
$searchResults = \Stripe\Customer :: all ([
2022-06-21 11:57:17 +02:00
'email' => $this -> client -> present () -> email (),
'limit' => 2 ,
'starting_after' => null ,
], $this -> stripe_connect_auth );
2021-09-01 00:22:24 +02:00
2022-06-21 11:57:17 +02:00
if ( count ( $searchResults ) == 1 ) {
2022-04-07 12:08:30 +02:00
$customer = $searchResults -> data [ 0 ];
2022-06-21 11:57:17 +02:00
// $this->updateStripeCustomer($customer);
return $customer ;
2022-04-07 12:08:30 +02:00
}
2021-10-04 18:09:10 +02:00
2021-09-01 00:22:24 +02:00
//Else create a new record
$data [ 'name' ] = $this -> client -> present () -> name ();
2022-06-21 11:57:17 +02:00
$data [ 'phone' ] = substr ( $this -> client -> present () -> phone (), 0 , 20 );
2021-10-04 18:09:10 +02:00
2021-09-01 00:22:24 +02:00
if ( filter_var ( $this -> client -> present () -> email (), FILTER_VALIDATE_EMAIL )) {
$data [ 'email' ] = $this -> client -> present () -> email ();
2019-09-16 06:59:59 +02:00
}
2019-09-17 07:42:10 +02:00
2022-04-07 12:08:30 +02:00
$data [ 'address' ][ 'line1' ] = $this -> client -> address1 ;
$data [ 'address' ][ 'line2' ] = $this -> client -> address2 ;
$data [ 'address' ][ 'city' ] = $this -> client -> city ;
$data [ 'address' ][ 'postal_code' ] = $this -> client -> postal_code ;
$data [ 'address' ][ 'state' ] = $this -> client -> state ;
2022-06-21 11:57:17 +02:00
$data [ 'address' ][ 'country' ] = $this -> client -> country ? $this -> client -> country -> iso_3166_2 : '' ;
2022-04-07 12:08:30 +02:00
2023-02-16 02:36:09 +01:00
$customer = Customer :: create ( $data , array_merge ( $this -> stripe_connect_auth , [ 'idempotency_key' => uniqid ( " st " , true )]));
2021-09-01 00:22:24 +02:00
2022-06-21 11:57:17 +02:00
if ( ! $customer ) {
2020-12-21 11:46:10 +01:00
throw new Exception ( 'Unable to create gateway customer' );
2019-12-30 22:59:12 +01:00
}
2019-09-25 04:07:33 +02:00
2019-09-16 06:59:59 +02:00
return $customer ;
}
2022-04-07 12:08:30 +02:00
public function updateStripeCustomer ( $customer )
{
//Else create a new record
$data [ 'name' ] = $this -> client -> present () -> name ();
2022-06-21 11:57:17 +02:00
$data [ 'phone' ] = substr ( $this -> client -> present () -> phone (), 0 , 20 );
2022-04-07 12:08:30 +02:00
if ( filter_var ( $this -> client -> present () -> email (), FILTER_VALIDATE_EMAIL )) {
$data [ 'email' ] = $this -> client -> present () -> email ();
}
$data [ 'address' ][ 'line1' ] = $this -> client -> address1 ;
$data [ 'address' ][ 'line2' ] = $this -> client -> address2 ;
$data [ 'address' ][ 'city' ] = $this -> client -> city ;
$data [ 'address' ][ 'postal_code' ] = $this -> client -> postal_code ;
$data [ 'address' ][ 'state' ] = $this -> client -> state ;
2022-06-21 11:57:17 +02:00
$data [ 'address' ][ 'country' ] = $this -> client -> country ? $this -> client -> country -> iso_3166_2 : '' ;
2022-04-07 12:08:30 +02:00
2022-06-21 11:57:17 +02:00
try {
2022-04-07 12:08:30 +02:00
\Stripe\Customer :: update ( $customer -> id , $data , $this -> stripe_connect_auth );
2022-06-21 11:57:17 +02:00
} catch ( Exception $e ) {
nlog ( 'unable to update clients in Stripe' );
2022-04-07 12:08:30 +02:00
}
}
2022-09-13 01:59:16 +02:00
public function updateCustomer ()
{
2023-02-16 02:36:09 +01:00
if ( $this -> client ) {
2022-09-13 01:59:16 +02:00
$customer = $this -> findOrCreateCustomer ();
//Else create a new record
$data [ 'name' ] = $this -> client -> present () -> name ();
$data [ 'phone' ] = substr ( $this -> client -> present () -> phone (), 0 , 20 );
$data [ 'address' ][ 'line1' ] = $this -> client -> address1 ;
$data [ 'address' ][ 'line2' ] = $this -> client -> address2 ;
$data [ 'address' ][ 'city' ] = $this -> client -> city ;
$data [ 'address' ][ 'postal_code' ] = $this -> client -> postal_code ;
$data [ 'address' ][ 'state' ] = $this -> client -> state ;
$data [ 'address' ][ 'country' ] = $this -> client -> country ? $this -> client -> country -> iso_3166_2 : '' ;
$data [ 'shipping' ][ 'name' ] = $this -> client -> present () -> name ();
$data [ 'shipping' ][ 'address' ][ 'line1' ] = $this -> client -> shipping_address1 ;
$data [ 'shipping' ][ 'address' ][ 'line2' ] = $this -> client -> shipping_address2 ;
$data [ 'shipping' ][ 'address' ][ 'city' ] = $this -> client -> shipping_city ;
$data [ 'shipping' ][ 'address' ][ 'postal_code' ] = $this -> client -> shipping_postal_code ;
$data [ 'shipping' ][ 'address' ][ 'state' ] = $this -> client -> shipping_state ;
$data [ 'shipping' ][ 'address' ][ 'country' ] = $this -> client -> shipping_country ? $this -> client -> shipping_country -> iso_3166_2 : '' ;
\Stripe\Customer :: update ( $customer -> id , $data , $this -> stripe_connect_auth );
}
}
2020-10-10 05:32:10 +02:00
public function refund ( Payment $payment , $amount , $return_client_response = false )
2020-05-14 03:04:23 +02:00
{
2020-09-18 14:35:53 +02:00
$this -> init ();
2021-05-11 23:58:18 +02:00
$meta = $this -> stripe_connect_auth ;
2020-12-01 13:46:09 +01:00
/** Response from Stripe SDK/API. */
$response = null ;
2020-09-18 14:35:53 +02:00
2020-12-01 13:46:09 +01:00
try {
2021-09-09 13:46:03 +02:00
$response = \Stripe\Refund :: create ([
2021-10-04 18:09:10 +02:00
'charge' => $payment -> transaction_reference ,
2022-06-21 11:57:17 +02:00
'amount' => $this -> convertToStripeAmount ( $amount , $this -> client -> currency () -> precision , $this -> client -> currency ()),
2021-09-09 13:46:03 +02:00
], $meta );
2020-12-01 13:46:09 +01:00
2023-05-23 15:27:28 +02:00
if ( in_array ( $response -> status , [ $response :: STATUS_SUCCEEDED , 'pending' ])) {
2022-06-21 11:57:17 +02:00
SystemLogger :: dispatch ([ 'server_response' => $response , 'data' => request () -> all ()], SystemLog :: CATEGORY_GATEWAY_RESPONSE , SystemLog :: EVENT_GATEWAY_SUCCESS , SystemLog :: TYPE_STRIPE , $this -> client , $this -> client -> company );
2020-12-01 13:46:09 +01:00
return [
'transaction_reference' => $response -> charge ,
'transaction_response' => json_encode ( $response ),
'success' => $response -> status == $response :: STATUS_SUCCEEDED ? true : false ,
'description' => $response -> metadata ,
'code' => $response ,
];
}
2020-05-14 03:04:23 +02:00
2022-06-21 11:57:17 +02:00
SystemLogger :: dispatch ([ 'server_response' => $response , 'data' => request () -> all ()], SystemLog :: CATEGORY_GATEWAY_RESPONSE , SystemLog :: EVENT_GATEWAY_FAILURE , SystemLog :: TYPE_STRIPE , $this -> client , $this -> client -> company );
2020-06-24 16:07:12 +02:00
return [
2020-12-01 13:46:09 +01:00
'transaction_reference' => null ,
2020-09-18 14:35:53 +02:00
'transaction_response' => json_encode ( $response ),
2020-12-01 13:46:09 +01:00
'success' => false ,
'description' => $response -> failure_reason ,
'code' => 422 ,
2020-06-24 16:07:12 +02:00
];
2020-12-21 11:46:10 +01:00
} catch ( Exception $e ) {
2022-06-21 11:57:17 +02:00
SystemLogger :: dispatch ([ 'server_response' => $response , 'data' => request () -> all ()], SystemLog :: CATEGORY_GATEWAY_RESPONSE , SystemLog :: EVENT_GATEWAY_FAILURE , SystemLog :: TYPE_STRIPE , $this -> client , $this -> client -> company );
2020-05-14 03:04:23 +02:00
2020-12-29 22:10:03 +01:00
nlog ( $e -> getMessage ());
2020-06-24 16:07:12 +02:00
2020-12-01 13:46:09 +01:00
return [
'transaction_reference' => null ,
'transaction_response' => json_encode ( $response ),
'success' => false ,
'description' => $e -> getMessage (),
'code' => 422 ,
];
}
2020-05-14 03:04:23 +02:00
}
2019-09-16 06:59:59 +02:00
2020-06-09 14:42:23 +02:00
public function verificationView ( ClientGatewayToken $payment_method )
{
return $this -> payment_method -> verificationView ( $payment_method );
}
2020-11-01 18:47:48 +01:00
public function processVerification ( Request $request , ClientGatewayToken $payment_method )
2020-06-09 14:42:23 +02:00
{
2020-11-01 18:47:48 +01:00
return $this -> payment_method -> processVerification ( $request , $payment_method );
2020-06-09 14:42:23 +02:00
}
2021-08-04 16:19:01 +02:00
public function processWebhookRequest ( PaymentWebhookRequest $request )
2020-06-27 17:39:28 +02:00
{
2024-07-24 04:27:09 +02:00
nlog ( $request -> all ());
2023-02-16 02:36:09 +01:00
if ( $request -> type === 'customer.source.updated' ) {
2023-01-11 01:12:40 +01:00
$ach = new ACH ( $this );
$ach -> updateBankAccount ( $request -> all ());
}
2023-02-16 02:36:09 +01:00
if ( $request -> type === 'payment_intent.processing' ) {
2024-07-24 04:27:09 +02:00
PaymentIntentProcessingWebhook :: dispatch ( $request -> data , $request -> company_key , $this -> company_gateway -> id ) -> delay ( now () -> addSeconds ( 5 ));
2023-01-11 01:37:11 +01:00
return response () -> json ([], 200 );
}
2021-08-13 07:53:00 +02:00
2022-01-24 04:24:47 +01:00
//payment_intent.succeeded - this will confirm or cancel the payment
2022-06-21 11:57:17 +02:00
if ( $request -> type === 'payment_intent.succeeded' ) {
2024-07-24 04:27:09 +02:00
PaymentIntentWebhook :: dispatch ( $request -> data , $request -> company_key , $this -> company_gateway -> id ) -> delay ( now () -> addSeconds ( 5 ));
2022-06-21 11:57:17 +02:00
2022-01-24 04:24:47 +01:00
return response () -> json ([], 200 );
}
2023-02-23 06:52:45 +01:00
if ( $request -> type === 'payment_intent.partially_funded' ) {
2024-07-24 04:27:09 +02:00
PaymentIntentPartiallyFundedWebhook :: dispatch ( $request -> data , $request -> company_key , $this -> company_gateway -> id ) -> delay ( now () -> addSeconds ( 5 ));
2023-02-23 06:52:45 +01:00
return response () -> json ([], 200 );
}
2022-06-21 11:57:17 +02:00
if ( in_array ( $request -> type , [ 'payment_intent.payment_failed' , 'charge.failed' ])) {
2024-07-24 04:27:09 +02:00
PaymentIntentFailureWebhook :: dispatch ( $request -> data , $request -> company_key , $this -> company_gateway -> id ) -> delay ( now () -> addSeconds ( 2 ));
return response () -> json ([], 200 );
}
if ( $request -> type === 'charge.refunded' && $request -> data [ 'object' ][ 'status' ] == 'succeeded' ) {
ChargeRefunded :: dispatch ( $request -> data , $request -> company_key ) -> delay ( now () -> addSeconds ( 5 ));
2022-06-21 11:57:17 +02:00
2022-05-19 07:48:31 +02:00
return response () -> json ([], 200 );
}
2022-01-24 11:53:46 +01:00
if ( $request -> type === 'charge.succeeded' ) {
2021-08-04 16:19:01 +02:00
foreach ( $request -> data as $transaction ) {
2024-05-07 03:10:33 +02:00
2024-06-14 09:09:44 +02:00
$payment = Payment :: query ()
-> where ( 'company_id' , $this -> company_gateway -> company_id )
-> where ( function ( $query ) use ( $transaction ) {
2024-05-07 03:10:33 +02:00
2024-06-14 09:09:44 +02:00
if ( isset ( $transaction [ 'payment_intent' ])) {
$query -> where ( 'transaction_reference' , $transaction [ 'payment_intent' ]);
}
2024-05-07 03:10:33 +02:00
2024-06-14 09:09:44 +02:00
if ( isset ( $transaction [ 'payment_intent' ]) && isset ( $transaction [ 'id' ])) {
$query -> orWhere ( 'transaction_reference' , $transaction [ 'id' ]);
}
2024-05-07 03:10:33 +02:00
2024-06-14 09:09:44 +02:00
if ( ! isset ( $transaction [ 'payment_intent' ]) && isset ( $transaction [ 'id' ])) {
$query -> where ( 'transaction_reference' , $transaction [ 'id' ]);
}
})
-> first ();
2024-05-07 01:59:00 +02:00
2021-11-15 04:14:58 +01:00
2021-08-04 16:19:01 +02:00
if ( $payment ) {
2023-05-23 15:27:28 +02:00
if ( isset ( $transaction [ 'payment_method_details' ][ 'au_becs_debit' ])) {
$payment -> transaction_reference = $transaction [ 'id' ];
}
2021-08-04 16:19:01 +02:00
$payment -> status_id = Payment :: STATUS_COMPLETED ;
$payment -> save ();
}
}
2021-09-14 15:25:30 +02:00
} elseif ( $request -> type === 'source.chargeable' ) {
2021-09-14 14:01:28 +02:00
$this -> init ();
2021-09-14 15:25:30 +02:00
foreach ( $request -> data as $transaction ) {
2022-06-21 11:57:17 +02:00
if ( ! $request -> data [ 'object' ][ 'amount' ] || empty ( $request -> data [ 'object' ][ 'amount' ])) {
2022-05-13 23:54:19 +02:00
continue ;
2022-06-21 11:57:17 +02:00
}
2022-05-13 23:54:19 +02:00
2021-09-14 15:25:30 +02:00
$charge = \Stripe\Charge :: create ([
'amount' => $request -> data [ 'object' ][ 'amount' ],
'currency' => $request -> data [ 'object' ][ 'currency' ],
'source' => $request -> data [ 'object' ][ 'id' ],
], $this -> stripe_connect_auth );
if ( $charge -> captured ) {
2024-05-07 03:10:33 +02:00
2024-06-14 09:09:44 +02:00
$payment = Payment :: query ()
-> where ( 'company_id' , $this -> company_gateway -> company_id )
-> where ( function ( $query ) use ( $transaction ) {
2024-05-07 03:10:33 +02:00
2024-06-14 09:09:44 +02:00
if ( isset ( $transaction [ 'payment_intent' ])) {
$query -> where ( 'transaction_reference' , $transaction [ 'payment_intent' ]);
}
2024-05-07 03:10:33 +02:00
2024-06-14 09:09:44 +02:00
if ( isset ( $transaction [ 'payment_intent' ]) && isset ( $transaction [ 'id' ])) {
$query -> orWhere ( 'transaction_reference' , $transaction [ 'id' ]);
}
2024-05-07 03:10:33 +02:00
2024-06-14 09:09:44 +02:00
if ( ! isset ( $transaction [ 'payment_intent' ]) && isset ( $transaction [ 'id' ])) {
$query -> where ( 'transaction_reference' , $transaction [ 'id' ]);
}
})
-> first ();
2024-05-07 01:59:00 +02:00
2022-05-13 23:54:19 +02:00
2024-05-07 03:10:33 +02:00
2021-09-14 15:25:30 +02:00
if ( $payment ) {
$payment -> status_id = Payment :: STATUS_COMPLETED ;
$payment -> save ();
}
}
2021-09-14 14:01:28 +02:00
}
2023-03-18 08:24:56 +01:00
} elseif ( $request -> type === " payment_method.automatically_updated " ) {
2023-01-16 19:19:14 +01:00
// Will notify customer on updated information
2022-12-17 12:53:54 +01:00
return response () -> json ([], 200 );
2023-03-18 08:24:56 +01:00
} elseif ( $request -> type === " mandate.updated " ) {
if ( $request -> data [ 'object' ][ 'status' ] == " active " ) {
2023-03-11 09:05:12 +01:00
// Check if payment method existsn
2023-01-16 20:48:14 +01:00
$payment_method = ( string ) $request -> data [ 'object' ][ 'payment_method' ];
2023-03-11 09:05:12 +01:00
2023-01-16 19:19:14 +01:00
$clientgateway = ClientGatewayToken :: query ()
2023-01-16 20:48:14 +01:00
-> where ( 'token' , $payment_method )
2023-01-16 19:19:14 +01:00
-> first ();
2023-03-11 09:05:12 +01:00
2023-03-18 08:24:56 +01:00
if ( $clientgateway ) {
2023-03-11 09:05:12 +01:00
$meta = $clientgateway -> meta ;
$meta -> state = 'authorized' ;
$clientgateway -> meta = $meta ;
$clientgateway -> save ();
}
2023-01-16 20:48:14 +01:00
return response () -> json ([], 200 );
2023-03-18 08:24:56 +01:00
} elseif ( $request -> data [ 'object' ][ 'status' ] == " inactive " && $request -> data [ 'object' ][ 'payment_method' ]) {
2023-01-20 08:16:26 +01:00
// Delete payment method
2023-01-20 09:20:30 +01:00
$clientgateway = ClientGatewayToken :: query ()
-> where ( 'token' , $request -> data [ 'object' ][ 'payment_method' ])
-> first ();
2023-03-11 09:05:12 +01:00
2023-10-26 04:57:44 +02:00
if ( $clientgateway ) {
2023-05-17 01:16:43 +02:00
$clientgateway -> delete ();
2023-10-26 04:57:44 +02:00
}
2024-01-14 05:05:00 +01:00
2023-01-16 20:48:14 +01:00
return response () -> json ([], 200 );
2023-03-18 08:24:56 +01:00
} elseif ( $request -> data [ 'object' ][ 'status' ] == " pending " ) {
2023-01-16 20:48:14 +01:00
return response () -> json ([], 200 );
2023-01-16 19:19:14 +01:00
}
2021-09-14 14:01:28 +02:00
}
2021-05-29 12:57:39 +02:00
2021-08-04 16:19:01 +02:00
return response () -> json ([], 200 );
2020-06-27 17:39:28 +02:00
}
2020-09-06 11:38:10 +02:00
public function tokenBilling ( ClientGatewayToken $cgt , PaymentHash $payment_hash )
2020-07-14 14:50:16 +02:00
{
2021-01-27 11:38:28 +01:00
return ( new Charge ( $this )) -> tokenBilling ( $cgt , $payment_hash );
2020-07-15 07:05:02 +02:00
}
2020-09-18 09:48:53 +02:00
2020-10-28 16:31:47 +01:00
/**
2020-11-25 15:19:52 +01:00
* Attach Stripe payment method to Stripe client .
*
* @ param string $payment_method
* @ param mixed $customer
*
* @ return void
2020-10-28 16:31:47 +01:00
*/
public function attach ( string $payment_method , $customer ) : void
{
2021-05-12 04:03:46 +02:00
$this -> init ();
2020-10-28 16:31:47 +01:00
try {
$stripe_payment_method = $this -> getStripePaymentMethod ( $payment_method );
2021-05-12 04:03:46 +02:00
$stripe_payment_method -> attach ([ 'customer' => $customer -> id ], $this -> stripe_connect_auth );
2020-12-21 11:46:10 +01:00
} catch ( ApiErrorException | Exception $e ) {
2021-05-12 04:03:46 +02:00
nlog ( $e -> getMessage ());
2023-02-16 02:36:09 +01:00
SystemLogger :: dispatch (
[
2021-05-25 18:30:50 +02:00
'server_response' => $e -> getMessage (),
2021-05-12 04:03:46 +02:00
'data' => request () -> all (),
2021-05-25 18:30:50 +02:00
],
2023-02-16 02:36:09 +01:00
SystemLog :: CATEGORY_GATEWAY_RESPONSE ,
SystemLog :: EVENT_GATEWAY_FAILURE ,
SystemLog :: TYPE_STRIPE ,
$this -> client ,
$this -> client -> company
);
2020-10-28 16:31:47 +01:00
}
}
2020-09-18 09:48:53 +02:00
/**
* Detach payment method from the Stripe .
* https :// stripe . com / docs / api / payment_methods / detach
2020-10-28 11:10:49 +01:00
*
* @ param ClientGatewayToken $token
2020-12-01 13:46:09 +01:00
* @ return void
*/
2020-09-18 09:48:53 +02:00
public function detach ( ClientGatewayToken $token )
{
2021-05-12 04:03:46 +02:00
$this -> init ();
2022-06-21 11:57:17 +02:00
try {
2021-05-12 04:03:46 +02:00
$pm = $this -> getStripePaymentMethod ( $token -> token );
$pm -> detach ([], $this -> stripe_connect_auth );
} catch ( ApiErrorException | Exception $e ) {
nlog ( $e -> getMessage ());
2023-02-16 02:36:09 +01:00
SystemLogger :: dispatch (
[
2021-05-25 18:30:50 +02:00
'server_response' => $e -> getMessage (),
2021-05-12 04:03:46 +02:00
'data' => request () -> all (),
2021-05-25 18:30:50 +02:00
],
2023-02-16 02:36:09 +01:00
SystemLog :: CATEGORY_GATEWAY_RESPONSE ,
SystemLog :: EVENT_GATEWAY_FAILURE ,
SystemLog :: TYPE_STRIPE ,
$this -> client ,
$this -> client -> company
);
2020-09-18 09:48:53 +02:00
}
}
2020-12-01 13:46:09 +01:00
2020-10-10 05:32:10 +02:00
public function getCompanyGatewayId () : int
{
return $this -> company_gateway -> id ;
}
2020-10-28 16:31:47 +01:00
/**
* Retrieve payment method from Stripe .
*
2020-11-25 15:19:52 +01:00
* @ param string $source
*
2020-12-21 11:46:10 +01:00
* @ return PaymentMethod | void
2020-10-28 16:31:47 +01:00
*/
public function getStripePaymentMethod ( string $source )
{
try {
2021-05-11 23:58:18 +02:00
return PaymentMethod :: retrieve ( $source , $this -> stripe_connect_auth );
2020-12-21 11:46:10 +01:00
} catch ( ApiErrorException | Exception $e ) {
2021-06-15 18:52:15 +02:00
throw new PaymentFailed ( $e -> getMessage (), $e -> getCode ());
2020-10-28 16:31:47 +01:00
}
}
2021-05-12 05:00:46 +02:00
public function getAllConnectedAccounts ()
{
$this -> init ();
return Account :: all ();
}
2021-05-17 06:02:43 +02:00
2023-08-08 23:44:34 +02:00
public function setClientFromCustomer ( $customer ) : self
2021-09-14 14:01:28 +02:00
{
2023-08-08 23:44:34 +02:00
$this -> client = ClientGatewayToken :: query () -> where ( 'gateway_customer_reference' , $customer ) -> first () -> client ;
return $this ;
2021-09-14 14:01:28 +02:00
}
2021-05-17 06:02:43 +02:00
/**
* Imports stripe customers and their payment methods
2021-05-25 18:30:50 +02:00
* Matches users in the system based on the $match_on_record
2021-05-17 06:02:43 +02:00
* ie . email
2021-05-25 18:30:50 +02:00
*
2021-05-17 06:02:43 +02:00
* Phone
* Email
*/
2021-05-17 10:56:14 +02:00
public function importCustomers ()
2021-05-17 06:02:43 +02:00
{
2021-05-17 10:56:14 +02:00
return ( new ImportCustomers ( $this )) -> run ();
2021-05-17 06:02:43 +02:00
//match clients based on the gateway_customer_reference column
}
2021-08-15 07:13:20 +02:00
2021-08-18 02:15:11 +02:00
public function importMatchedClients ()
{
return ( new ImportCustomers ( $this )) -> match ();
}
2021-08-20 14:44:39 +02:00
public function importCustomer ( $customer_id )
{
return ( new ImportCustomers ( $this )) -> importCustomer ( $customer_id );
}
2021-08-15 07:13:20 +02:00
public function verifyConnect ()
{
return ( new Verify ( $this )) -> run ();
}
2021-08-17 06:01:11 +02:00
2022-01-06 01:19:29 +01:00
public function setApplePayDomain ( $domain )
2022-01-06 00:19:31 +01:00
{
2022-01-06 01:19:29 +01:00
$this -> init ();
\Stripe\ApplePayDomain :: create ([
2022-06-21 11:57:17 +02:00
'domain_name' => $domain ,
], $this -> stripe_connect_auth );
2022-01-06 00:19:31 +01:00
}
2021-08-17 06:01:11 +02:00
public function disconnect ()
{
2022-06-21 11:57:17 +02:00
if ( ! $this -> stripe_connect ) {
2021-08-17 06:01:11 +02:00
return true ;
2022-06-21 11:57:17 +02:00
}
2021-08-17 06:01:11 +02:00
2022-06-21 11:57:17 +02:00
if ( ! strlen ( $this -> company_gateway -> getConfigField ( 'account_id' )) > 1 ) {
2021-08-17 06:01:11 +02:00
throw new StripeConnectFailure ( 'Stripe Connect has not been configured' );
2022-06-21 11:57:17 +02:00
}
2021-08-17 06:01:11 +02:00
Stripe :: setApiKey ( config ( 'ninja.ninja_stripe_key' ));
try {
\Stripe\OAuth :: deauthorize ([
2022-06-21 11:57:17 +02:00
'client_id' => config ( 'ninja.ninja_stripe_client_id' ),
'stripe_user_id' => $this -> company_gateway -> getConfigField ( 'account_id' ),
2021-08-17 06:01:11 +02:00
]);
2021-10-09 09:07:05 +02:00
$config = $this -> company_gateway -> getConfig ();
2022-06-21 11:57:17 +02:00
$config -> account_id = '' ;
2021-10-09 09:07:05 +02:00
$this -> company_gateway -> setConfig ( $config );
$this -> company_gateway -> save ();
2022-06-21 11:57:17 +02:00
} catch ( \Exception $e ) {
2021-08-17 06:01:11 +02:00
throw new StripeConnectFailure ( 'Unable to disconnect Stripe Connect' );
}
return response () -> json ([ 'message' => 'success' ], 200 );
}
2021-09-21 13:38:35 +02:00
public function decodeUnicodeString ( $string )
{
return html_entity_decode ( $string , ENT_QUOTES , 'UTF-8' );
// return iconv("UTF-8", "ISO-8859-1//TRANSLIT", $this->decode_encoded_utf8($string));
}
2022-06-21 11:57:17 +02:00
public function decode_encoded_utf8 ( $string )
{
return preg_replace_callback ( '#\\\\u([0-9a-f]{4})#ism' , function ( $matches ) {
return mb_convert_encoding ( pack ( 'H*' , $matches [ 1 ]), 'UTF-8' , 'UCS-2BE' );
}, $string );
2021-09-21 13:38:35 +02:00
}
2024-03-15 02:52:08 +01:00
public function auth () : bool
{
$this -> init ();
try {
$this -> verifyConnect ();
return true ;
2024-06-14 09:09:44 +02:00
} catch ( \Exception $e ) {
2024-03-15 02:52:08 +01:00
}
return false ;
}
2019-12-30 22:59:12 +01:00
}