Comment to 'Hard coded payment options in Stripe'
  • I'll share it on apps market as soon as it's finished for free. That way, I can have my lessons and everyone else the final solution. Deal?

    Hello. OK, @thomlin lets continue our lesson. :)

    1. You need to register a handler for 'stripe_v3_create_session' alert from 'Payments' module. I'll use the following names and prefixes in my example:

    • module's verndor - tester
    • module name - bx_dummy
    • module folder - dummy
    • module class prefix - BxDummy

    So, you need add the following code in enable.sql of your custom app.

    INSERT INTO `sys_alerts_handlers` (`name`, `class`, `file`, `service_call`) VALUES
    ('bx_dummy', 'BxDummyAlertsResponse', 'modules/tester/dummy/classes/BxDummyAlertsResponse.php', '');
    SET @iHandler := LAST_INSERT_ID();
    INSERT INTO `sys_alerts` (`unit`, `action`, `handler_id`) VALUES
    ('bx_payment', 'stripe_v3_create_session', @iHandler);
    

    and this one in disable.sql

    SET @iHandler := (SELECT `id` FROM `sys_alerts_handlers` WHERE `name` = 'bx_dummy' LIMIT 1);
    DELETE FROM `sys_alerts` WHERE `handler_id` = @iHandler;
    DELETE FROM `sys_alerts_handlers` WHERE `id` = @iHandler;
    

    2. Now we need to create BxDummyAlertsResponse class in modules/tester/dummy/classes/BxDummyAlertsResponse.php file and process the alert. So, the file should have the following code:

    <?php defined('BX_DOL') or die('hack attempt');
    /**
     * Copyright (c) UNA, Inc - https://una.io
     * MIT License - https://opensource.org/licenses/MIT
     * * @defgroup    The Art Collectors Club
     * @ingroup     UnaModules
     *
     * @{
     */
    
    class BxDummyAlertsResponse extends BxDolAlertsResponse
    {
        protected $_sModule;
        protected $_oModule;
    
        public function __construct()
        {
            parent::__construct();
    
            $this->_sModule = 'bx_dummy';
            $this->_oModule = BxDolModule::getInstance($this->_sModule);
        }
    
        public function response($oAlert)
        {
            $sMethod = '_process' . bx_gen_method_name($oAlert->sUnit . '_' . $oAlert->sAction);
            if(!method_exists($this, $sMethod))
                return;
    
            $this->$sMethod($oAlert);
        }
    
        protected function _processBxPaymentStripeV3CreateSession($oAlert)
        {
    	$aSession = $oAlert->aExtras['session_params'];
    
    	//TODO: Make your customizations here.
    
    	$oAlert->aExtras['session_params'] = $aSession;
        }
    }
    
    /** @} */
    

    So, in this case $aSession variable will contain the array which you wanted to modify and thefore you need to do something like the following:

    $aSession = $oAlert->aExtras['session_params'];
    $aSession['payment_method_types'] = array_merge($aSession['payment_method_types'], ['sofort', 'sepa_debit']);
    $oAlert->aExtras['session_params'] = $aSession;
    

    After that you need to disable/enable your app and test.

    • Now, that I set BxPaymentProviderStripeV3.php back to its original version, everything works like a charm! It's really magic. 🙌
      When does the lesson "Extending the module with putting data into the array by using Studio settings" start? 😉

      Hello. If you just want to control items of 'payment_method_types' parameter view Settings then it wouldn't be a problem. I mean to have a Dropdown with Card, Sofort and Sepa Debit choices.

      • How about a comma-separated list of arguments to fill the array with, individually? Of corse, the user will be responsible to configure his payment methods in Stripe accordingly.

        • Yes, it can be done this way. In this case you may use standard Text field. It's even more easier.