Merge branch '1.11.x.buycourses-plugin-add-stripe-support' of git://github.com/NosoloredChamilo/chamilo-lms into NosoloredChamilo-1.11.x.buycourses-plugin-add-stripe-support

pull/4265/head
Yannick Warnier 4 years ago
commit cfc1d96523
  1. 3
      composer.json
  2. 7
      plugin/buycourses/CHANGELOG.md
  3. 1
      plugin/buycourses/README.md
  4. 23
      plugin/buycourses/database.php
  5. 9
      plugin/buycourses/lang/english.php
  6. 9
      plugin/buycourses/lang/spanish.php
  7. 102
      plugin/buycourses/src/buy_course_plugin.class.php
  8. 41
      plugin/buycourses/src/paymentsetup.php
  9. 7
      plugin/buycourses/src/process.php
  10. 172
      plugin/buycourses/src/process_confirm.php
  11. 30
      plugin/buycourses/src/stripe_cancel.php
  12. 64
      plugin/buycourses/src/stripe_response.php
  13. 30
      plugin/buycourses/src/stripe_success.php
  14. 18
      plugin/buycourses/view/paymentsetup.tpl

@ -123,7 +123,8 @@
"zendframework/zend-config": "~3.0", "zendframework/zend-config": "~3.0",
"zendframework/zend-feed": "~2.6|^3.0", "zendframework/zend-feed": "~2.6|^3.0",
"zendframework/zend-http": "~2.6|^3.0", "zendframework/zend-http": "~2.6|^3.0",
"zendframework/zend-soap": "~2.6|^3.0" "zendframework/zend-soap": "~2.6|^3.0",
"stripe/stripe-php": "*"
}, },
"require-dev": { "require-dev": {
"behat/behat": "~3.5", "behat/behat": "~3.5",

@ -1,3 +1,10 @@
v7.2 - 2021-11-22
====
Add Strype payments support.
The file update.php must be executed to update the structure of the tables
in the database.
v7.1 - 2021-10-26 v7.1 - 2021-10-26
==== ====
Fix install issue with DB field type. Fix install issue with DB field type.

@ -12,6 +12,7 @@ Currently, the plugin allows users to pay through:
- PayPal (requires a merchant account on PayPal at configuration time) - PayPal (requires a merchant account on PayPal at configuration time)
- Bank payments (requires manual confirmation of payments' reception) - Bank payments (requires manual confirmation of payments' reception)
- RedSys payments (Spanish payment gateway) (requires the download of an external file) - RedSys payments (Spanish payment gateway) (requires the download of an external file)
- Stripe payments (requieres a merchant account oin Stripe at configuration time)
The user receives an e-mail confirming the purchase and she/he can immediately The user receives an e-mail confirming the purchase and she/he can immediately
access to the course or session. access to the course or session.

@ -501,6 +501,19 @@ if (false === $sm->tablesExist(BuyCoursesPlugin::TABLE_COUPON_SERVICE_SALE)) {
$couponSaleTable->setPrimaryKey(['id']); $couponSaleTable->setPrimaryKey(['id']);
} }
if (false === $sm->tablesExist(BuyCoursesPlugin::TABLE_STRIPE)) {
$stripeTable = $pluginSchema->createTable(BuyCoursesPlugin::TABLE_STRIPE);
$stripeTable->addColumn(
'id',
Types::INTEGER,
['autoincrement' => true, 'unsigned' => true]
);
$stripeTable->addColumn('account_id', Types::STRING);
$stripeTable->addColumn('secret_key', Types::STRING);
$stripeTable->addColumn('endpoint_secret', Types::STRING);
$stripeTable->setPrimaryKey(['id']);
}
$queries = $pluginSchema->toSql($platform); $queries = $pluginSchema->toSql($platform);
foreach ($queries as $query) { foreach ($queries as $query) {
@ -517,6 +530,7 @@ $extraFieldTable = Database::get_main_table(TABLE_EXTRA_FIELD);
$culqiTable = Database::get_main_table(BuyCoursesPlugin::TABLE_CULQI); $culqiTable = Database::get_main_table(BuyCoursesPlugin::TABLE_CULQI);
$globalTable = Database::get_main_table(BuyCoursesPlugin::TABLE_GLOBAL_CONFIG); $globalTable = Database::get_main_table(BuyCoursesPlugin::TABLE_GLOBAL_CONFIG);
$tpvRedsysTable = Database::get_main_table(BuyCoursesPlugin::TABLE_TPV_REDSYS); $tpvRedsysTable = Database::get_main_table(BuyCoursesPlugin::TABLE_TPV_REDSYS);
$stripeTable = Database::get_main_table(BuyCoursesPlugin::TABLE_STRIPE);
$paypalExtraField = Database::select( $paypalExtraField = Database::select(
"*", "*",
@ -586,6 +600,15 @@ Database::insert(
] ]
); );
Database::insert(
$stripeTable,
[
'account_id' => '',
'secret_key' => '',
'endpoint_secret' => '',
]
);
$currencies = [ $currencies = [
['AD', 'Andorra', 'EUR', 'AND'], ['AD', 'Andorra', 'EUR', 'AND'],
['AE', 'United Arab Emirates', 'AED', 'ARE'], ['AE', 'United Arab Emirates', 'AED', 'ARE'],

@ -85,7 +85,7 @@ $strings['CancelOrder'] = "Cancel order";
$strings['BankAccountInformation'] = "Bank account details"; $strings['BankAccountInformation'] = "Bank account details";
$strings['BankAccount'] = "Bank account"; $strings['BankAccount'] = "Bank account";
$strings['OnceItIsConfirmedYouWillReceiveAnEmailWithTheBankInformationAndAnOrderReference'] = "Once confirmed, you will receive an e-mail with the bank details and an order reference."; $strings['OnceItIsConfirmedYouWillReceiveAnEmailWithTheBankInformationAndAnOrderReference'] = "Once confirmed, you will receive an e-mail with the bank details and an order reference.";
$strings['SubscriptionToCourseXSuccessful'] = "Your subscription to <a href=\"%s\"><strong>\"%s\"</strong></a> was completed successfully."; $strings['SubscriptionToCourseXSuccessful'] = "Completed process. Your subscription to <a href=\"%s\"><strong>\"%s\"</strong></a> was completed successfully.";
$strings['OrderCanceled'] = "Order canceled"; $strings['OrderCanceled'] = "Order canceled";
$strings['OrderStatus'] = "Order status"; $strings['OrderStatus'] = "Order status";
$strings['PayoutStatus'] = "Payment status"; $strings['PayoutStatus'] = "Payment status";
@ -266,3 +266,10 @@ $strings['ConfigureCoupon'] = "Configure coupon";
$strings['DiscountCoupon'] = "Discount coupons"; $strings['DiscountCoupon'] = "Discount coupons";
$strings['CouponsCode'] = "Code"; $strings['CouponsCode'] = "Code";
$strings['DoYouHaveACoupon'] = "Do you have a coupon?"; $strings['DoYouHaveACoupon'] = "Do you have a coupon?";
$strings['stripe_enable'] = "Enable Stripe";
$strings['StripeConfig'] = "Stripe configuration:";
$strings['InfoStripeCredentials'] = "To obtain the credentials you must first create an account in Stripe, copy the account id from your profile, go to the API Keys configuration secton and copy the secret key, finally you must register a new Endpoint in the webhooks section to https://{site}/plugin/buycourses/src/stripe_response.php for the payment_intent.succeeded event type and copy the secret of the Endpoint.";
$strings['StripeAccountId'] = "Account id:";
$strings['StripeSecret'] = "Secret key:";
$strings['StripeEndpointSecret'] = "Endpoint secret:";
$strings['PendingReasonByStripe'] = "<b>Pending</b>. Awaiting for payment confirmation ...";

@ -85,7 +85,7 @@ $strings['CancelOrder'] = "Cancelar Orden";
$strings['BankAccountInformation'] = "Información de la Cuenta Bancaria"; $strings['BankAccountInformation'] = "Información de la Cuenta Bancaria";
$strings['BankAccount'] = "Cuenta Bancaria"; $strings['BankAccount'] = "Cuenta Bancaria";
$strings['OnceItIsConfirmedYouWillReceiveAnEmailWithTheBankInformationAndAnOrderReference'] = "Una vez confirmado, recibira un e-mail con los datos bancarios y una referencia del pedido."; $strings['OnceItIsConfirmedYouWillReceiveAnEmailWithTheBankInformationAndAnOrderReference'] = "Una vez confirmado, recibira un e-mail con los datos bancarios y una referencia del pedido.";
$strings['SubscriptionToCourseXSuccessful'] = "<p class=\"lead\">Tu subscripción a <a href=\"%s\"><strong>\"%s\"</strong></a> se realizó correctamente.</p>"; $strings['SubscriptionToCourseXSuccessful'] = "<p class=\"lead\">Proceso completado. Tu subscripción a <a href=\"%s\"><strong>\"%s\"</strong></a> se realizó correctamente.</p>";
$strings['OrderCanceled'] = "Pedido cancelado"; $strings['OrderCanceled'] = "Pedido cancelado";
$strings['OrderStatus'] = "Estado del pedido"; $strings['OrderStatus'] = "Estado del pedido";
$strings['PayoutStatus'] = "Estado del pago"; $strings['PayoutStatus'] = "Estado del pago";
@ -266,3 +266,10 @@ $strings['ConfigureCoupon'] = "Configurar cupón";
$strings['DiscountCoupons'] = "Cupones descuento"; $strings['DiscountCoupons'] = "Cupones descuento";
$strings['CouponsCode'] = "Código"; $strings['CouponsCode'] = "Código";
$strings['DoYouHaveACoupon'] = "¿Tienes un cupón descuento?"; $strings['DoYouHaveACoupon'] = "¿Tienes un cupón descuento?";
$strings['stripe_enable'] = "Activar Stripe";
$strings['StripeConfig'] = "Configuración de Stripe:";
$strings['InfoStripeCredentials'] = "Para obtener las credenciales debes crear una cuenta en Stripe, copiar el id de cuenta de tu perfil, ir a la sección de configuración de las claves API y copiar la secret key, por último deberá registrar un nuevo Endpoint en la sección de webhooks a https://{site}/plugin/buycourses/src/stripe_response.php para el tipo de evento payment_intent.succeeded y copiar el secret del Endpoint.";
$strings['StripeAccountId'] = "Id de la cuenta:";
$strings['StripeSecret'] = "Secret key:";
$strings['StripeEndpointSecret'] = "Endpoint secret:";
$strings['PendingReasonByStripe'] = "<b>Pendiente</b>. Esperando confirmación del pago ...";

@ -39,6 +39,7 @@ class BuyCoursesPlugin extends Plugin
const TABLE_COUPON_SERVICE = 'plugin_buycourses_coupon_rel_service'; const TABLE_COUPON_SERVICE = 'plugin_buycourses_coupon_rel_service';
const TABLE_COUPON_SALE = 'plugin_buycourses_coupon_rel_sale'; const TABLE_COUPON_SALE = 'plugin_buycourses_coupon_rel_sale';
const TABLE_COUPON_SERVICE_SALE = 'plugin_buycourses_coupon_rel_service_sale'; const TABLE_COUPON_SERVICE_SALE = 'plugin_buycourses_coupon_rel_service_sale';
const TABLE_STRIPE = 'plugin_buycourses_stripe_account';
const PRODUCT_TYPE_COURSE = 1; const PRODUCT_TYPE_COURSE = 1;
const PRODUCT_TYPE_SESSION = 2; const PRODUCT_TYPE_SESSION = 2;
const PRODUCT_TYPE_SERVICE = 3; const PRODUCT_TYPE_SERVICE = 3;
@ -46,6 +47,7 @@ class BuyCoursesPlugin extends Plugin
const PAYMENT_TYPE_TRANSFER = 2; const PAYMENT_TYPE_TRANSFER = 2;
const PAYMENT_TYPE_CULQI = 3; const PAYMENT_TYPE_CULQI = 3;
const PAYMENT_TYPE_TPV_REDSYS = 4; const PAYMENT_TYPE_TPV_REDSYS = 4;
const PAYMENT_TYPE_STRIPE = 5;
const PAYOUT_STATUS_CANCELED = 2; const PAYOUT_STATUS_CANCELED = 2;
const PAYOUT_STATUS_PENDING = 0; const PAYOUT_STATUS_PENDING = 0;
const PAYOUT_STATUS_COMPLETED = 1; const PAYOUT_STATUS_COMPLETED = 1;
@ -104,6 +106,7 @@ class BuyCoursesPlugin extends Plugin
'tax_enable' => 'boolean', 'tax_enable' => 'boolean',
'use_currency_symbol' => 'boolean', 'use_currency_symbol' => 'boolean',
'tpv_redsys_enable' => 'boolean', 'tpv_redsys_enable' => 'boolean',
'stripe_enable' => 'boolean',
] ]
); );
} }
@ -127,7 +130,7 @@ class BuyCoursesPlugin extends Plugin
*/ */
public function isEnabled($checkEnabled = false) public function isEnabled($checkEnabled = false)
{ {
return $this->get('paypal_enable') || $this->get('transfer_enable') || $this->get('culqi_enable'); return $this->get('paypal_enable') || $this->get('transfer_enable') || $this->get('culqi_enable') || $this->get('stripe_enable');
} }
/** /**
@ -155,6 +158,7 @@ class BuyCoursesPlugin extends Plugin
self::TABLE_COUPON_SERVICE, self::TABLE_COUPON_SERVICE,
self::TABLE_COUPON_SALE, self::TABLE_COUPON_SALE,
self::TABLE_COUPON_SERVICE_SALE, self::TABLE_COUPON_SERVICE_SALE,
self::TABLE_STRIPE,
]; ];
$em = Database::getManager(); $em = Database::getManager();
$cn = $em->getConnection(); $cn = $em->getConnection();
@ -193,6 +197,7 @@ class BuyCoursesPlugin extends Plugin
self::TABLE_COUPON_SERVICE, self::TABLE_COUPON_SERVICE,
self::TABLE_COUPON_SALE, self::TABLE_COUPON_SALE,
self::TABLE_COUPON_SERVICE_SALE, self::TABLE_COUPON_SERVICE_SALE,
self::TABLE_STRIPE,
]; ];
foreach ($tablesToBeDeleted as $tableToBeDeleted) { foreach ($tablesToBeDeleted as $tableToBeDeleted) {
@ -412,6 +417,26 @@ class BuyCoursesPlugin extends Plugin
)"; )";
Database::query($sql); Database::query($sql);
$table = self::TABLE_STRIPE;
$sql = "CREATE TABLE IF NOT EXISTS $table (
id int unsigned NOT NULL AUTO_INCREMENT,
account_id varchar(255) NOT NULL,
secret_key varchar(255) NOT NULL,
endpoint_secret varchar(255) NOT NULL,
PRIMARY KEY (id)
)";
Database::query($sql);
$sql = "SELECT * FROM $table";
$res = Database::query($sql);
if (Database::num_rows($res) == 0) {
Database::insert($table, [
'account_id' => '',
'secret_key' => '',
'endpoint_secret' => '',
]);
}
Display::addFlash( Display::addFlash(
Display::return_message( Display::return_message(
$this->get_lang('Updated'), $this->get_lang('Updated'),
@ -630,6 +655,41 @@ class BuyCoursesPlugin extends Plugin
); );
} }
/**
* Save Stripe configuration params.
*
* @param array $params
*
* @return int Rows affected. Otherwise return false
*/
public function saveStripeParameters($params)
{
return Database::update(
Database::get_main_table(self::TABLE_STRIPE),
[
'account_id' => $params['account_id'],
'secret_key' => $params['secret_key'],
'endpoint_secret' => $params['endpoint_secret'],
],
['id = ?' => 1]
);
}
/**
* Gets the stored Stripe params.
*
* @return array
*/
public function getStripeParams()
{
return Database::select(
'*',
Database::get_main_table(self::TABLE_STRIPE),
['id = ?' => 1],
'first'
);
}
/** /**
* Save a transfer account information. * Save a transfer account information.
* *
@ -1116,6 +1176,7 @@ class BuyCoursesPlugin extends Plugin
self::PAYMENT_TYPE_TRANSFER, self::PAYMENT_TYPE_TRANSFER,
self::PAYMENT_TYPE_CULQI, self::PAYMENT_TYPE_CULQI,
self::PAYMENT_TYPE_TPV_REDSYS, self::PAYMENT_TYPE_TPV_REDSYS,
self::PAYMENT_TYPE_STRIPE,
] ]
) )
) { ) {
@ -1211,6 +1272,25 @@ class BuyCoursesPlugin extends Plugin
return Database::insert(self::TABLE_SALE, $values); return Database::insert(self::TABLE_SALE, $values);
} }
/**
* Update the sale reference.
*
* @param int $saleId The sale ID
* @param int $saleReference The new saleReference
*
* @return bool
*/
public function updateSaleReference($saleId, $saleReference)
{
$saleTable = Database::get_main_table(self::TABLE_SALE);
return Database::update(
$saleTable,
['reference' => $saleReference],
['id = ?' => (int) $saleId]
);
}
/** /**
* Get sale data by ID. * Get sale data by ID.
* *
@ -1230,6 +1310,25 @@ class BuyCoursesPlugin extends Plugin
); );
} }
/**
* Get sale data by reference.
*
* @param int $reference The sale reference
*
* @return array
*/
public function getSaleFromReference($reference)
{
return Database::select(
'*',
Database::get_main_table(self::TABLE_SALE),
[
'where' => ['reference = ?' => $reference],
],
'first'
);
}
/** /**
* Get a list of sales by the payment type. * Get a list of sales by the payment type.
* *
@ -1412,6 +1511,7 @@ class BuyCoursesPlugin extends Plugin
self::PAYMENT_TYPE_TRANSFER => $this->get_lang('BankTransfer'), self::PAYMENT_TYPE_TRANSFER => $this->get_lang('BankTransfer'),
self::PAYMENT_TYPE_CULQI => 'Culqi', self::PAYMENT_TYPE_CULQI => 'Culqi',
self::PAYMENT_TYPE_TPV_REDSYS => $this->get_lang('TpvPayment'), self::PAYMENT_TYPE_TPV_REDSYS => $this->get_lang('TpvPayment'),
self::PAYMENT_TYPE_STRIPE => 'Stripe',
]; ];
} }

@ -19,6 +19,7 @@ $transferEnable = $plugin->get('transfer_enable');
$tpvRedsysEnable = $plugin->get('tpv_redsys_enable'); $tpvRedsysEnable = $plugin->get('tpv_redsys_enable');
$commissionsEnable = $plugin->get('commissions_enable'); $commissionsEnable = $plugin->get('commissions_enable');
$culqiEnable = $plugin->get('culqi_enable'); $culqiEnable = $plugin->get('culqi_enable');
$stripeEnable = $plugin->get('stripe_enable') === 'true';
if (isset($_GET['action'], $_GET['id'])) { if (isset($_GET['action'], $_GET['id'])) {
if ($_GET['action'] == 'delete_taccount') { if ($_GET['action'] == 'delete_taccount') {
@ -395,6 +396,44 @@ $culqiForm->addCheckBox('integration', null, $plugin->get_lang('Sandbox'));
$culqiForm->addButtonSave(get_lang('Save')); $culqiForm->addButtonSave(get_lang('Save'));
$culqiForm->setDefaults($plugin->getCulqiParams()); $culqiForm->setDefaults($plugin->getCulqiParams());
// Stripe main configuration
$stripeForm = new FormValidator('stripe_config');
if ($stripeForm->validate()) {
$stripeFormValues = $stripeForm->getSubmitValues();
$plugin->saveStripeParameters($stripeFormValues);
Display::addFlash(
Display::return_message(get_lang('Saved'), 'success')
);
header('Location:'.api_get_self());
exit;
}
$stripeForm->addText(
'account_id',
$plugin->get_lang('StripeAccountId'),
false,
['cols-size' => [3, 8, 1]]
);
$stripeForm->addText(
'secret_key',
$plugin->get_lang('StripeSecret'),
false,
['cols-size' => [3, 8, 1]]
);
$stripeForm->addText(
'endpoint_secret',
$plugin->get_lang('StripeEndpointSecret'),
false,
['cols-size' => [3, 8, 1]]
);
$stripeForm->addButtonSave(get_lang('Save'));
$stripeForm->setDefaults($plugin->getStripeParams());
// breadcrumbs // breadcrumbs
$interbreadcrumb[] = [ $interbreadcrumb[] = [
'url' => api_get_path(WEB_PLUGIN_PATH).'buycourses/index.php', 'url' => api_get_path(WEB_PLUGIN_PATH).'buycourses/index.php',
@ -419,6 +458,8 @@ $tpl->assign('transfer_enable', $transferEnable);
$tpl->assign('culqi_enable', $culqiEnable); $tpl->assign('culqi_enable', $culqiEnable);
$tpl->assign('tpv_redsys_enable', $tpvRedsysEnable); $tpl->assign('tpv_redsys_enable', $tpvRedsysEnable);
$tpl->assign('tpv_redsys_form', $htmlTpvRedsys); $tpl->assign('tpv_redsys_form', $htmlTpvRedsys);
$tpl->assign('stripe_enable', $stripeEnable);
$tpl->assign('stripe_form', $stripeForm->returnForm());
$content = $tpl->fetch('buycourses/view/paymentsetup.tpl'); $content = $tpl->fetch('buycourses/view/paymentsetup.tpl');

@ -21,8 +21,9 @@ $paypalEnabled = $plugin->get('paypal_enable') === 'true';
$transferEnabled = $plugin->get('transfer_enable') === 'true'; $transferEnabled = $plugin->get('transfer_enable') === 'true';
$culqiEnabled = $plugin->get('culqi_enable') === 'true'; $culqiEnabled = $plugin->get('culqi_enable') === 'true';
$tpvRedsysEnable = $plugin->get('tpv_redsys_enable') === 'true'; $tpvRedsysEnable = $plugin->get('tpv_redsys_enable') === 'true';
$stripeEnable = $plugin->get('stripe_enable') === 'true';
if (!$paypalEnabled && !$transferEnabled && !$culqiEnabled && !$tpvRedsysEnable) { if (!$paypalEnabled && !$transferEnabled && !$culqiEnabled && !$tpvRedsysEnable && !$stripeEnable) {
api_not_allowed(true); api_not_allowed(true);
} }
@ -106,6 +107,10 @@ if (!$tpvRedsysEnable || !file_exists(api_get_path(SYS_PLUGIN_PATH).'buycourses/
unset($paymentTypesOptions[BuyCoursesPlugin::PAYMENT_TYPE_TPV_REDSYS]); unset($paymentTypesOptions[BuyCoursesPlugin::PAYMENT_TYPE_TPV_REDSYS]);
} }
if (!$stripeEnable) {
unset($paymentTypesOptions[BuyCoursesPlugin::PAYMENT_TYPE_STRIPE]);
}
$count = count($paymentTypesOptions); $count = count($paymentTypesOptions);
if ($count === 0) { if ($count === 0) {
$form->addHtml($plugin->get_lang('NoPaymentOptionAvailable')); $form->addHtml($plugin->get_lang('NoPaymentOptionAvailable'));

@ -371,5 +371,177 @@ switch ($sale['payment_type']) {
echo 'document.tpv_chamilo.submit();'; echo 'document.tpv_chamilo.submit();';
echo '</script>'; echo '</script>';
break;
case BuyCoursesPlugin::PAYMENT_TYPE_STRIPE:
$buyingCourse = false;
$buyingSession = false;
switch ($sale['product_type']) {
case BuyCoursesPlugin::PRODUCT_TYPE_COURSE:
$buyingCourse = true;
$course = $plugin->getCourseInfo($sale['product_id'], $coupon);
break;
case BuyCoursesPlugin::PRODUCT_TYPE_SESSION:
$buyingSession = true;
$session = $plugin->getSessionInfo($sale['product_id'], $coupon);
break;
}
$form = new FormValidator(
'success',
'POST',
api_get_self(),
null,
null,
FormValidator::LAYOUT_INLINE
);
if ($form->validate()) {
$formValues = $form->getSubmitValues();
if (isset($formValues['cancel'])) {
$plugin->cancelSale($sale['id']);
unset($_SESSION['bc_sale_id']);
unset($_SESSION['bc_coupon_id']);
header('Location: '.api_get_path(WEB_PLUGIN_PATH).'buycourses/index.php');
exit;
}
$stripeParams = $plugin->getStripeParams();
$currency = $plugin->getCurrency($sale['currency_id']);
\Stripe\Stripe::setApiKey($stripeParams['secret_key']);
\Stripe\Stripe::setAppInfo("ChamiloBuyCoursesPlugin");
$session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'],
'line_items' => [[
'price_data' => [
'unit_amount_decimal' => $sale['price'] * 100,
'currency' => $currency['iso_code'],
'product_data' => [
'name' => $sale['product_name'],
],
],
'quantity' => 1,
]],
'customer_email' => $_SESSION['_user']['email'],
'mode' => 'payment',
'success_url' => api_get_path(WEB_PLUGIN_PATH).'buycourses/src/stripe_success.php',
'cancel_url' => api_get_path(WEB_PLUGIN_PATH).'buycourses/src/stripe_cancel.php',
]);
if (!empty($session)) {
$messageTemplate = new Template();
$messageTemplate->assign('user', $userInfo);
$messageTemplate->assign(
'sale',
[
'date' => $sale['date'],
'product' => $sale['product_name'],
'currency' => $currency['iso_code'],
'price' => $sale['price'],
'reference' => $sale['reference'],
]
);
$messageTemplate->assign('transfer_accounts', $transferAccounts);
$messageTemplate->assign('info_email_extra', $infoEmailExtra);
MessageManager::send_message_simple(
$userInfo['user_id'],
$plugin->get_lang('bc_subject'),
$messageTemplate->fetch('buycourses/view/message_transfer.tpl')
);
if (!empty($globalParameters['sale_email'])) {
$messageConfirmTemplate = new Template();
$messageConfirmTemplate->assign('user', $userInfo);
$messageConfirmTemplate->assign(
'sale',
[
'date' => $sale['date'],
'product' => $sale['product_name'],
'currency' => $currency['iso_code'],
'price' => $sale['price'],
'reference' => $sale['reference'],
]
);
api_mail_html(
'',
$globalParameters['sale_email'],
$plugin->get_lang('bc_subject'),
$messageConfirmTemplate->fetch('buycourses/view/message_confirm.tpl')
);
}
Display::addFlash(
Display::return_message(
sprintf(
$plugin->get_lang('PurchaseStatusX'),
$plugin->get_lang('PendingReasonByStripe')
),
'success',
false
)
);
$plugin->updateSaleReference($saleId, $session->payment_intent);
unset($_SESSION['bc_coupon_id']);
header('HTTP/1.1 301 Moved Permanently');
header('Location: '.$session->url);
} else {
Display::addFlash(
Display::return_message(
$plugin->get_lang('ErrorOccurred'),
'error',
false
)
);
header('Location: ../index.php');
}
exit;
}
$form->addButton(
'confirm',
$plugin->get_lang('ConfirmOrder'),
'check',
'success',
'default',
null,
['id' => 'confirm']
);
$form->addButtonCancel($plugin->get_lang('CancelOrder'), 'cancel');
$template = new Template();
if ($buyingCourse) {
$template->assign('course', $course);
} elseif ($buyingSession) {
$template->assign('session', $session);
}
$template->assign('buying_course', $buyingCourse);
$template->assign('buying_session', $buyingSession);
$template->assign('terms', $globalParameters['terms_and_conditions']);
$template->assign('title', $sale['product_name']);
$template->assign('price', $sale['price']);
$template->assign('currency', $sale['currency_id']);
$template->assign('user', $userInfo);
$template->assign('transfer_accounts', $transferAccounts);
$template->assign('form', $form->returnForm());
$template->assign('is_bank_transfer', false);
$content = $template->fetch('buycourses/view/process_confirm.tpl');
$template->assign('content', $content);
$template->display_one_col_template();
break; break;
} }

@ -0,0 +1,30 @@
<?php
/* For license terms, see /license.txt */
/**
* Success page for the purchase of a course in the Buy Courses plugin.
*
* @package chamilo.plugin.buycourses
*/
require_once '../config.php';
$plugin = BuyCoursesPlugin::create();
$stripeEnabled = $plugin->get('stripe_enable') === 'true';
if (!$stripeEnabled) {
api_not_allowed(true);
}
$sale = $plugin->getSale($_SESSION['bc_sale_id']);
if (empty($sale)) {
api_not_allowed(true);
}
Display::addFlash(
Display::return_message($plugin->get_lang('ErrorContactPlatformAdmin'), 'error')
);
unset($_SESSION['bc_sale_id']);
header('Location: '.api_get_path(WEB_PLUGIN_PATH).'buycourses/src/course_catalog.php');
exit;

@ -0,0 +1,64 @@
<?php
require_once '../config.php';
$plugin = BuyCoursesPlugin::create();
$stripeEnabled = $plugin->get('stripe_enable') === 'true';
if (!$stripeEnabled) {
api_not_allowed(true);
}
$stripeParams = $plugin->getStripeParams();
$payload = @file_get_contents('php://input');
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
$event = null;
try {
$event = \Stripe\Webhook::constructEvent(
$payload, $sig_header, $stripeParams['endpoint_secret']
);
} catch(\UnexpectedValueException $e) {
http_response_code(400);
exit();
} catch(\Stripe\Exception\SignatureVerificationException $e) {
http_response_code(400);
exit();
}
switch ($event->type) {
case 'payment_intent.succeeded':
$paymentIntent = $event->data->object;
$sale = $plugin->getSaleFromReference($paymentIntent->id);
if (empty($sale) ) {
api_not_allowed(true);
}
$buyingCourse = false;
$buyingSession = false;
switch ($sale['product_type']) {
case BuyCoursesPlugin::PRODUCT_TYPE_COURSE:
$buyingCourse = true;
$course = $plugin->getCourseInfo($sale['product_id']);
break;
case BuyCoursesPlugin::PRODUCT_TYPE_SESSION:
$buyingSession = true;
$session = $plugin->getSessionInfo($sale['product_id']);
break;
}
$saleIsCompleted = $plugin->completeSale($sale['id']);
if ($saleIsCompleted) {
$plugin->storePayouts($sale['id']);
}
default:
echo 'Received unknown event type ' . $event->type;
}
http_response_code(200);

@ -0,0 +1,30 @@
<?php
/* For license terms, see /license.txt */
/**
* Success page for the purchase of a course in the Buy Courses plugin.
*
* @package chamilo.plugin.buycourses
*/
require_once '../config.php';
$plugin = BuyCoursesPlugin::create();
$stripeEnabled = $plugin->get('stripe_enable') === 'true';
if (!$stripeEnabled) {
api_not_allowed(true);
}
$sale = $plugin->getSale($_SESSION['bc_sale_id']);
if (empty($sale)) {
api_not_allowed(true);
}
Display::addFlash(
$plugin->getSubscriptionSuccessMessage($sale)
);
unset($_SESSION['bc_sale_id']);
header('Location: '.api_get_path(WEB_PLUGIN_PATH).'buycourses/src/course_catalog.php');
exit;

@ -136,3 +136,21 @@
</div> </div>
</div> </div>
{% endif %} {% endif %}
{% if stripe_enable == "true" %}
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">{{ 'StripeConfig'|get_plugin_lang('BuyCoursesPlugin') }}</h3>
</div>
<div class="panel-body">
<div class="row">
<div class="col-md-5">
<p>{{ 'InfoStripeCredentials'|get_plugin_lang('BuyCoursesPlugin') }}</p>
</div>
<div class="col-md-7">
{{ stripe_form }}
</div>
</div>
</div>
</div>
{% endif %}

Loading…
Cancel
Save