55 lines
2.1 KiB
PHP
55 lines
2.1 KiB
PHP
<?php
|
|
|
|
// Store API key encrypted
|
|
/* This requires the user to add define('STRIPE_ENCRYPTION_KEY', 'your-secret-encryption-key'); to their wp-config.php */
|
|
|
|
|
|
// Store API key encrypted
|
|
function save_stripe_api_key($api_key) {
|
|
// Get the encryption key from wp-config.php (assuming it's defined there)
|
|
if (defined('STRIPE_ENCRYPTION_KEY')) {
|
|
$encryption_key = STRIPE_ENCRYPTION_KEY;
|
|
} else {
|
|
// Fallback: handle the case where the encryption key is not defined
|
|
// Ideally, throw an error or show an admin notice that the key is missing
|
|
return new WP_Error('missing_encryption_key', 'Encryption key not defined in wp-config.php');
|
|
}
|
|
|
|
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc')); // Generate an initialization vector
|
|
|
|
// Encrypt the API key
|
|
$encrypted_key = openssl_encrypt($api_key, 'aes-256-cbc', $encryption_key, 0, $iv);
|
|
|
|
// Store the encrypted key along with the IV
|
|
update_option('stripe_encrypted_key', base64_encode($encrypted_key)); // Save encrypted key
|
|
update_option('stripe_iv', base64_encode($iv)); // Save IV for decryption
|
|
}
|
|
|
|
// Retrieve the API key
|
|
function get_stripe_api_key() {
|
|
// Get the encrypted key and IV from the database
|
|
$encrypted_key = get_option('stripe_encrypted_key');
|
|
$iv = get_option('stripe_iv');
|
|
|
|
if ($encrypted_key && $iv) {
|
|
// Decode the base64-encoded values
|
|
$encrypted_key = base64_decode($encrypted_key);
|
|
$iv = base64_decode($iv);
|
|
|
|
// Get the encryption key from wp-config.php
|
|
if (defined('STRIPE_ENCRYPTION_KEY')) {
|
|
$encryption_key = STRIPE_ENCRYPTION_KEY;
|
|
} else {
|
|
// Fallback: handle the case where the encryption key is not defined
|
|
// Ideally, throw an error or show an admin notice that the key is missing
|
|
return new WP_Error('missing_encryption_key', 'Encryption key not defined in wp-config.php');
|
|
}
|
|
|
|
// Decrypt the API key
|
|
$api_key = openssl_decrypt($encrypted_key, 'aes-256-cbc', $encryption_key, 0, $iv);
|
|
|
|
return $api_key;
|
|
} else {
|
|
return false;
|
|
}
|
|
} |