File: /home/assibfaf/public_html/prox-modules/stripe/stripe.php
<?php
use Proxim\Application;
use Proxim\Configuration;
use Proxim\Currency;
use Proxim\Module\Module;
use Proxim\Module\PaymentModule;
use Proxim\Order\AdditionalService;
use Proxim\Order\Order;
use Proxim\Site\Site;
use Proxim\User\Customer;
use Proxim\Util\ArrayUtils;
use Proxim\Validate;
class Stripe extends PaymentModule
{
/**
* Stripe configuration
*/
const STRIPE_ENABLED = 'STRIPE_ENABLED';
const STRIPE_MODE = 'STRIPE_MODE';
const STRIPE_LIVE_KEY = 'STRIPE_LIVE_KEY';
const STRIPE_LIVE_SECRET = 'STRIPE_LIVE_SECRET';
const STRIPE_TEST_KEY = 'STRIPE_TEST_KEY';
const STRIPE_TEST_SECRET = 'STRIPE_TEST_SECRET';
public function __construct()
{
$this->name = 'stripe';
$this->icon = 'fab fa-stripe';
$this->version = '1.0.0';
$this->prox_versions_compliancy = array('min' => '1.0.0', 'max' => Application::VERSION);
$this->author = 'Davison Pro';
$this->displayName = 'Credit Card';
$this->description = 'Set up Stripe as a payment method and enable users to pay without leaving your website';
$this->bootstrap = true;
parent::__construct();
}
public function isActive() {
return Configuration::get(self::STRIPE_ENABLED);
}
/**
* Echoes a template.
*
* @param string $templateName Template name
*/
public function showTemplate($templateName)
{
echo $this->getTemplateContent($templateName);
}
/**
* Return a template.
*
* @param string $templateName Template name
* @param array $additionnalParameters Additionnal parameters to inject on the Twig template
*
* @return string Parsed template
*/
private function getTemplateContent($templateName, $additionnalParameters = array())
{
$this->smarty->assign($additionnalParameters);
return $this->fetch(__DIR__ . '/views/' . $templateName.'.tpl');
}
public function processOrderCheckout($order, $currency) {
$app = $this->application;
$smarty = $this->smarty;
$user = $app->container->user;
$smarty->assign([
'stripe_key' => Configuration::get(self::STRIPE_MODE) == "live" ? Configuration::get(self::STRIPE_LIVE_KEY) : Configuration::get(self::STRIPE_TEST_KEY),
]);
return $this->getTemplateContent('stripe.order');
}
public function processAddPaymentCheckout($addPayment, $currency) {
$app = $this->application;
$smarty = $this->smarty;
$user = $app->container->user;
$smarty->assign([
'stripe_key' => Configuration::get(self::STRIPE_MODE) == "live" ? Configuration::get(self::STRIPE_LIVE_KEY) : Configuration::get(self::STRIPE_TEST_KEY),
]);
return $this->getTemplateContent('stripe.add-payment');
}
public function processWalletCheckout($amount, $currency) {
$app = $this->application;
$smarty = $this->smarty;
$user = $app->container->user;
$smarty->assign([
'stripe_key' => Configuration::get(self::STRIPE_MODE) == "live" ? Configuration::get(self::STRIPE_LIVE_KEY) : Configuration::get(self::STRIPE_TEST_KEY),
]);
return $this->getTemplateContent('stripe.wallet');
}
public function processTipCheckout($amount, $currency) {
$app = $this->application;
$smarty = $this->smarty;
$user = $app->container->user;
$smarty->assign([
'stripe_key' => Configuration::get(self::STRIPE_MODE) == "live" ? Configuration::get(self::STRIPE_LIVE_KEY) : Configuration::get(self::STRIPE_TEST_KEY),
]);
return $this->getTemplateContent('stripe.tip');
}
/**
* Hook action verify payment
* @param $params array
* @return void
*/
public function verifyPayment($node_id, $handle, $payload)
{
$app = $this->application;
$stripe_token = ArrayUtils::get($payload, 'stripeToken');
$idempotency_key = ArrayUtils::get($payload, 'idempotency_key');
$stripe_key = Configuration::get(self::STRIPE_MODE) == "live" ? Configuration::get(self::STRIPE_LIVE_SECRET) : Configuration::get(self::STRIPE_TEST_SECRET);
switch ($handle) {
case 'order':
$redirectUri = $app->base_uri . '/dashboard/orders';
$order = new Order( $node_id );
if (!Validate::isLoadedObject($order)) {
return $app->redirect( $app->base_uri . '/payment/stripe/failed?redirectUri=' . $redirectUri );
}
$currency = $order->currency;
$amount = ($order->total*$currency->conversion_rate)*100;
$redirectUri = $app->base_uri.'/dashboard/order/' . $order->id;
try {
\Stripe\Stripe::setApiKey($stripe_key);
$intent = \Stripe\PaymentIntent::create([
'payment_method_data' => [
'type' => 'card',
'card' => [
'token' => $stripe_token
],
],
'amount' => floor($amount),
'currency' => $currency->iso_code,
'confirmation_method' => 'manual',
'confirm' => true,
],
[
'idempotency_key' => $idempotency_key
]);
if ($intent->status == 'succeeded') {
$payload['paymentId'] = $intent->id;
PaymentModule::processPayment( $node_id, $handle, $payload );
return $app->redirect($app->base_uri . '/payment/stripe/success?redirectUri=' . $redirectUri);
} else {
return $app->redirect($app->base_uri . '/payment/stripe/failed?redirectUri=' . $redirectUri);
}
} catch(\Exception $e) {
}
break;
case 'add-payment':
$redirectUri = $app->base_uri . '/dashboard/orders';
$addPayment = new AdditionalService( $node_id );
if (!Validate::isLoadedObject($addPayment)) {
return $app->redirect($app->base_uri.'/payment/stripe/failed?redirectUri='.$redirectUri);
}
$order = new Order( (int) $addPayment->order_id );
if (!Validate::isLoadedObject($order)) {
return $app->redirect($app->base_uri.'/payment/stripe/failed?redirectUri='.$redirectUri);
}
$currency = $order->currency;
$amount = ($addPayment->total*$currency->conversion_rate)*100;
$redirectUri = $app->base_uri.'/dashboard/order/' . $order->id;
try {
\Stripe\Stripe::setApiKey($stripe_key);
$intent = \Stripe\PaymentIntent::create([
'payment_method_data' => [
'type' => 'card',
'card' => [
'token' => $stripe_token
],
],
'amount' => floor($amount),
'currency' => $currency->iso_code,
'confirmation_method' => 'manual',
'confirm' => true,
],
[
'idempotency_key' => $idempotency_key
]);
if ($intent->status == 'succeeded') {
$payload['paymentId'] = $intent->id;
PaymentModule::processPayment( $node_id, $handle, $payload );
return $app->redirect($app->base_uri.'/payment/stripe/success?redirectUri=' . $redirectUri);
} else {
return $app->redirect($app->base_uri.'/payment/stripe/failed?redirectUri=' . $redirectUri);
}
} catch(\Exception $e) {
}
break;
case 'wallet':
$redirectUri = $app->base_uri . '/dashboard/wallet';
$customer = new Customer( $node_id );
if (!Validate::isLoadedObject($customer)) {
return $app->redirect( $app->base_uri . '/payment/stripe/failed?redirectUri=' . $redirectUri );
}
$amount = ArrayUtils::get($payload, 'amount');
$currency_code = ArrayUtils::get($payload, 'currency');
$currency_id = Currency::getIdByIsoCode( $currency_code );
$currency = new Currency( $currency_id );
$amount = ($amount*$currency->conversion_rate)*100;
try {
\Stripe\Stripe::setApiKey($stripe_key);
$intent = \Stripe\PaymentIntent::create([
'payment_method_data' => [
'type' => 'card',
'card' => [
'token' => $stripe_token
],
],
'amount' => floor($amount),
'currency' => $currency->iso_code,
'confirmation_method' => 'manual',
'confirm' => true,
],
[
'idempotency_key' => $idempotency_key
]);
if ($intent->status == 'succeeded') {
$payload['paymentId'] = $intent->id;
PaymentModule::processPayment( $customer->id, $handle, $payload );
return $app->redirect($app->base_uri.'/payment/stripe/success?redirectUri=' . $redirectUri);
} else {
return $app->redirect($app->base_uri.'/payment/stripe/failed?redirectUri=' . $redirectUri);
}
} catch(\Exception $e) {
}
break;
case 'tip':
$redirectUri = $app->base_uri . '/dashboard';
$order = new Order( $node_id );
if (!Validate::isLoadedObject($order)) {
return $app->redirect( $app->base_uri . '/payment/stripe/failed?redirectUri=' . $redirectUri );
}
$redirectUri = $app->base_uri . '/dashboard/order/' . $order->id;
$amount = ArrayUtils::get($payload, 'amount');
$currency_code = ArrayUtils::get($payload, 'currency');
$currency_id = Currency::getIdByIsoCode( $currency_code );
$currency = new Currency( $currency_id );
$amount = $amount*100;
try {
\Stripe\Stripe::setApiKey($stripe_key);
$intent = \Stripe\PaymentIntent::create([
'payment_method_data' => [
'type' => 'card',
'card' => [
'token' => $stripe_token
],
],
'amount' => floor($amount),
'currency' => $currency->iso_code,
'confirmation_method' => 'manual',
'confirm' => true,
],
[
'idempotency_key' => $idempotency_key
]);
if ($intent->status == 'succeeded') {
$payload['paymentId'] = $intent->id;
PaymentModule::processPayment( $node_id, $handle, $payload );
return $app->redirect($app->base_uri.'/payment/stripe/success?redirectUri=' . $redirectUri);
} else {
return $app->redirect($app->base_uri.'/payment/stripe/failed?redirectUri=' . $redirectUri);
}
} catch(\Exception $e) {
}
break;
}
}
}