<?php /*

 Composr
 Copyright (c) Christopher Graham, 2004-2024

 See docs/LICENSE.md for full licensing information.


 NOTE TO PROGRAMMERS:
   Do not edit this file. If you need to make changes, save your changed file to the appropriate *_custom folder
   **** If you ignore this advice, then your website upgrades (e.g. for bug fixes) will likely kill your changes ****

*/

/**
 * @license    http://opensource.org/licenses/cpal_1.0 Common Public Attribution License
 * @copyright  Christopher Graham
 * @package    points
 */

/**
 * Hook class.
 * This is a very FUDGEd hook as most of the processing for points actually happens within the purchase module and eCommerce addon.
 * But we need this hook so the proper procedure of handling transactions takes place (involving ecom_trans_expecting) and points get charged.
 */
class Hook_payment_gateway_points
{

    /**
     * Get a standardised config map.
     *
     * @return array The config
     */
    public function get_config() : array
    {
        return [
            'supports_remote_memo' => false,
            'local_only' => false,
        ];
    }

    /**
     * Generate a transaction ID / trans-expecting ID.
     *
     * @return string A transaction ID
     */
    public function generate_trans_id() : string
    {
        require_code('crypt');
        return get_secure_random_string();
    }

    /**
     * Make a transaction (payment) button.
     * This is for IPN-style transactions (as opposed to local transactions).
     *
     * @param  ID_TEXT $trans_expecting_id Our internal temporary transaction ID
     * @param  ID_TEXT $type_code The product codename
     * @param  SHORT_TEXT $item_name The human-readable product title
     * @param  ID_TEXT $purchase_id The purchase ID
     * @param  float $price Transaction price in money
     * @param  float $tax Transaction tax in money
     * @param  float $shipping_cost Shipping cost
     * @param  ID_TEXT $currency The currency to use
     * @return Tempcode The button
     */
    public function make_transaction_button(string $trans_expecting_id, string $type_code, string $item_name, string $purchase_id, float $price, float $tax, float $shipping_cost, string $currency) : object
    {
        if (!addon_installed('ecommerce')) {
            return new Tempcode();
        }

        $url = build_url([
            'page' => 'purchase',
            'type' => 'finish',
            'points' => 1,
            'trans_expecting_id'=> $trans_expecting_id,
            'purchase_id' => $purchase_id,
            'type_code' => $type_code,
        ], '_SEARCH', [], true);

        require_lang('ecommerce');

        return do_template('BUTTON_SCREEN', ['_GUID' => '8c928f1f703e9ba232a7033adee19a31', 'TITLE' => do_lang_tempcode('MAKE_PAYMENT'), 'IMG' => 'buttons/proceed', 'IMMEDIATE' => false, 'URL' => $url]);
    }

    /**
     * Make a subscription (payment) button.
     * This is for IPN-style transactions (as opposed to local transactions).
     *
     * @param  ID_TEXT $trans_expecting_id Our internal temporary transaction ID
     * @param  ID_TEXT $type_code The product codename
     * @param  SHORT_TEXT $item_name The human-readable product title
     * @param  ID_TEXT $purchase_id The purchase ID
     * @param  float $price Transaction price in money
     * @param  float $tax Transaction tax in money
     * @param  ID_TEXT $currency The currency to use
     * @param  integer $length The subscription length in the units
     * @param  ID_TEXT $length_units The length units
     * @set d w m y
     * @return Tempcode The button
     */
    public function make_subscription_button(string $trans_expecting_id, string $type_code, string $item_name, string $purchase_id, float $price, float $tax, string $currency, int $length, string $length_units) : object
    {
        // Points do not support subscriptions
        warn_exit(do_lang_tempcode('INTERNAL_ERROR'));
        return new Tempcode(); // Make IDEs happy
    }

    /**
     * Make a subscription cancellation button.
     * This is for IPN-style transactions (as opposed to local transactions).
     *
     * @param  ID_TEXT $purchase_id The purchase ID
     * @return Tempcode The button
     */
    public function make_cancel_button(string $purchase_id) : object
    {
        // Points do not support subscriptions
        warn_exit(do_lang_tempcode('INTERNAL_ERROR'));
        return new Tempcode(); // Make IDEs happy
    }

    /**
     * Find whether the hook auto-cancels (if it does, auto cancel the given subscription).
     *
     * @param  AUTO_LINK $subscription_id ID of the subscription to cancel
     * @return ?boolean True: yes. False: no. (null: cancels via a user-URL-directioning)
     */
    public function auto_cancel(int $subscription_id) : ?bool
    {
        // Not supported by points
        return false;
    }
}
