DROP TABLE IF EXISTS `plugin_stripe`;
CREATE TABLE IF NOT EXISTS `plugin_stripe` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `foreign_id` int(10) unsigned DEFAULT NULL,
  `stripe_id` varchar(255) DEFAULT NULL,
  `token` varchar(255) DEFAULT NULL,
  `amount` decimal(9,2) unsigned DEFAULT NULL,
  `currency` varchar(3) DEFAULT NULL,
  `description` varchar(255) DEFAULT NULL,
  `created` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `foreign_id` (`foreign_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

START TRANSACTION;

ALTER TABLE `bookings` MODIFY COLUMN `payment_method` enum('paypal','authorize','stripe','creditcard','cash','bank') DEFAULT NULL;
ALTER TABLE `bookings_payments` MODIFY COLUMN `payment_method` enum('paypal','authorize','stripe','creditcard','bank','cash') DEFAULT NULL;

DELETE FROM `options` WHERE `key` IN ('o_allow_stripe', 'o_stripe_public_key', 'o_stripe_private_key');
DELETE FROM `multi_lang` WHERE `model` = 'pjField' AND `field` = 'title' AND `foreign_id` IN (SELECT `id` FROM `fields` WHERE `key` IN ('opt_o_allow_stripe', 'opt_o_stripe_public_key', 'opt_o_stripe_private_key', 'payment_methods_ARRAY_stripe', 'payment_plugin_messages_ARRAY_stripe'));
DELETE FROM `fields` WHERE `key` IN ('opt_o_allow_stripe', 'opt_o_stripe_public_key', 'opt_o_stripe_private_key', 'payment_methods_ARRAY_stripe', 'payment_plugin_messages_ARRAY_stripe');

-- Make room so the Stripe gateway group sits together right after the
-- Authorize.net block (mirrors the PayPal/Authorize groups). Offline methods shift down.
UPDATE `options` SET `order` = 22 WHERE `tab_id` = 2 AND `key` = 'o_allow_cash';
UPDATE `options` SET `order` = 23 WHERE `tab_id` = 2 AND `key` = 'o_allow_creditcard';
UPDATE `options` SET `order` = 24 WHERE `tab_id` = 2 AND `key` = 'o_allow_bank';
UPDATE `options` SET `order` = 25 WHERE `tab_id` = 2 AND `key` = 'o_bank_account';

INSERT INTO `options` (`foreign_id`, `key`, `tab_id`, `value`, `label`, `type`, `order`, `is_visible`, `style`) VALUES
(1, 'o_allow_stripe', 2, 'Yes|No::No', 'Yes|No', 'enum', 17, 1, NULL),
(1, 'o_stripe_public_key', 2, '', NULL, 'string', 18, 1, NULL),
(1, 'o_stripe_private_key', 2, '', NULL, 'string', 19, 1, NULL);

INSERT INTO `fields` VALUES (NULL, 'opt_o_allow_stripe', 'backend', 'Options / Allow payments with Stripe', 'script', NULL);
SET @id := (SELECT LAST_INSERT_ID());
INSERT INTO `multi_lang` VALUES (NULL, @id, 'pjField', '::LOCALE::', 'title', 'Allow Stripe payments', 'script');

INSERT INTO `fields` VALUES (NULL, 'opt_o_stripe_public_key', 'backend', 'Options / Stripe public key', 'script', NULL);
SET @id := (SELECT LAST_INSERT_ID());
INSERT INTO `multi_lang` VALUES (NULL, @id, 'pjField', '::LOCALE::', 'title', 'Stripe public key', 'script');

INSERT INTO `fields` VALUES (NULL, 'opt_o_stripe_private_key', 'backend', 'Options / Stripe private key', 'script', NULL);
SET @id := (SELECT LAST_INSERT_ID());
INSERT INTO `multi_lang` VALUES (NULL, @id, 'pjField', '::LOCALE::', 'title', 'Stripe secret key', 'script');

INSERT INTO `fields` VALUES (NULL, 'payment_methods_ARRAY_stripe', 'arrays', 'payment_methods_ARRAY_stripe', 'script', NULL);
SET @id := (SELECT LAST_INSERT_ID());
INSERT INTO `multi_lang` VALUES (NULL, @id, 'pjField', '::LOCALE::', 'title', 'Stripe', 'script');

INSERT INTO `fields` VALUES (NULL, 'payment_plugin_messages_ARRAY_stripe', 'arrays', 'payment_plugin_messages_ARRAY_stripe', 'script', NULL);
SET @id := (SELECT LAST_INSERT_ID());
INSERT INTO `multi_lang` VALUES (NULL, @id, 'pjField', '::LOCALE::', 'title', 'Your order is saved. Redirecting to Stripe Checkout...', 'script');

COMMIT;
