Created
May 10, 2016 15:15
-
-
Save greenhornet79/dd60506e5cae43f2c9f7db95889645d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 2Checkout Payment Gateway Class | |
* | |
* @package Leaky Paywall | |
* @subpackage Classes/Roles | |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License | |
* @since 1.0.0 | |
*/ | |
class Leaky_Paywall_Payment_Gateway_2checkout extends Leaky_Paywall_Payment_Gateway { | |
private $secret_key; | |
protected $publishable_key; | |
/** | |
* Get things going | |
* | |
* @since 3.5.1 | |
*/ | |
public function init() { | |
$settings = get_leaky_paywall_settings(); | |
$this->supports[] = 'one-time'; | |
$this->supports[] = 'recurring'; | |
// $this->supports[] = 'fees'; | |
$this->secret_word = isset( $settings['twocheckout_private_key'] ) ? trim( $settings['twocheckout_private_key'] ) : ''; | |
$this->test_mode = 'off' === $settings['test_mode'] ? false : true; | |
if( $this->test_mode ) { | |
$this->secret_key = isset( $settings['twocheckout_test_private'] ) ? trim( $settings['twocheckout_test_private'] ) : ''; | |
$this->publishable_key = isset( $settings['twocheckout_test_publishable'] ) ? trim( $settings['twocheckout_test_publishable'] ) : ''; | |
$this->seller_id = isset( $settings['twocheckout_test_seller_id'] ) ? trim( $settings['twocheckout_test_seller_id'] ) : ''; | |
$this->environment = 'sandbox'; | |
} else { | |
$this->secret_key = isset( $settings['twocheckout_live_private'] ) ? trim( $settings['twocheckout_live_private'] ) : ''; | |
$this->publishable_key = isset( $settings['twocheckout_live_publishable'] ) ? trim( $settings['twocheckout_live_publishable'] ) : ''; | |
$this->seller_id = isset( $settings['twocheckout_live_seller_id'] ) ? trim( $settings['twocheckout_live_seller_id'] ) : ''; | |
$this->environment = 'production'; | |
} | |
// load any dependencies or libraries | |
if( ! class_exists( 'Twocheckout' ) ) { | |
require_once LP_2CK_PATH . 'include/2checkout-php/lib/Twocheckout.php'; | |
} | |
} | |
/** | |
* Process registration | |
* | |
* @since 1.0.0 | |
*/ | |
public function process_signup() { | |
Twocheckout::privateKey( $this->secret_key ); | |
Twocheckout::sellerId( $this->seller_id ); | |
Twocheckout::sandbox( $this->test_mode ); | |
Twocheckout::verifySSL(false); // @todo: remove before production | |
if( empty( $_POST['token'] ) ) { | |
wp_die( __( 'Missing token, please try again or contact support if the issue persists.', 'issume-leaky-paywall' ), __( 'Error', 'issuem-leaky-paywall' ), array( 'response' => 400 ) ); | |
} | |
$settings = get_leaky_paywall_settings(); | |
$paid = false; | |
$customer_exists = false; | |
$mode = 'off' === $settings['test_mode'] ? 'live' : 'test'; | |
$level = get_level_by_level_id( $this->level_id ); | |
try { | |
if ( is_multisite_premium() && !empty( $level['site'] ) && !is_main_site( $level['site'] ) ) { | |
$site = '_' . $level['site']; | |
} else { | |
$site = ''; | |
} | |
$charge = Twocheckout_Charge::auth(array( | |
"merchantOrderId" => "123", | |
"token" => $_POST['token'], | |
"currency" => strtoupper( $this->currency ), | |
"total" => $this->amount, | |
"billingAddr" => array( | |
"name" => sanitize_text_field( $_POST['card_name'] ), | |
"addrLine1" => sanitize_text_field( $_POST['card_address'] ), | |
"city" => sanitize_text_field( $_POST['card_city'] ), | |
"state" => sanitize_text_field( $_POST['card_state'] ), | |
"zipCode" => sanitize_text_field( $_POST['card_zip'] ), | |
"country" => sanitize_text_field( $_POST['card_country'] ), | |
"email" => $this->email, | |
) | |
)); | |
if ($charge['response']['responseCode'] == 'APPROVED') { | |
$paid = true; | |
} | |
} catch (Twocheckout_Error $e) { | |
echo '<pre>'; | |
print_r( $e ); | |
echo '</pre>'; | |
} | |
if ( $paid ) { | |
// charge succeeded | |
$customer_id = $charge['response']['orderNumber']; | |
$meta_args = array( | |
'level_id' => $this->level_id, | |
'subscriber_id' => $customer_id, | |
'subscriber_email' => $this->email, | |
'price' => $this->amount, | |
'description' => $this->level_name, | |
'payment_gateway' => '2checkout', | |
'payment_status' => 'active', | |
'interval' => $this->length_unit, | |
'interval_count' => $this->length, | |
'site' => !empty( $level['site'] ) ? $level['site'] : '', | |
'plan' => '' | |
); | |
if ( is_user_logged_in() || !empty( $existing_customer ) ) { | |
$user_id = leaky_paywall_update_subscriber( NULL, $this->email, $customer_id, $meta_args ); //if the email already exists, we want to update the subscriber, not create a new one | |
} else { | |
// create the new customer as a leaky paywall subscriber | |
$user_id = leaky_paywall_new_subscriber( NULL, $this->email, $customer_id, $meta_args ); | |
} | |
if ( $user_id ) { | |
do_action( 'leaky_paywall_2checkout_signup', $user_id ); | |
// log the user in | |
wp_set_current_user( $user_id ); | |
wp_set_auth_cookie( $user_id, true ); | |
// redirect user after sign up | |
if ( !empty( $settings['page_for_after_subscribe'] ) ) { | |
wp_safe_redirect( get_page_link( $settings['page_for_after_subscribe'] ) ); | |
} else if ( !empty( $settings['page_for_profile'] ) ) { | |
wp_safe_redirect( get_page_link( $settings['page_for_profile'] ) ); | |
} else if ( !empty( $settings['page_for_subscription'] ) ) { | |
wp_safe_redirect( get_page_link( $settings['page_for_subscription'] ) ); | |
} | |
exit; | |
} else { | |
wp_die( __( 'An error occurred, please contact the site administrator: ', 'leaky-paywall' ) . get_bloginfo( 'admin_email' ), __( 'Error', 'leaky-paywall' ), array( 'response' => '401' ) ); | |
} | |
} | |
} | |
/** | |
* Process webhooks | |
* | |
* @since 1.0.0 | |
*/ | |
public function process_webhooks() { | |
// Determine if this is a webhook | |
if( ! isset( $_GET['listener'] ) || strtoupper( $_GET['listener'] ) != '2checkout' ) { | |
return; | |
} | |
// Now retrieve the webhook data from the $_POST super global | |
// add the functionality for processing webhooks | |
} | |
/** | |
* Add credit card fields | |
* | |
* @since 3.5.1 | |
*/ | |
public function fields() { | |
$settings = get_leaky_paywall_settings(); | |
ob_start(); | |
?> | |
<input name="token" type="hidden" value="" /> | |
<script type="text/javascript"> | |
// Called when token created successfully. | |
var successCallback = function(data) { | |
var myForm = document.getElementById('leaky-paywall-payment-form'); | |
// Set the token as the value for the token input | |
myForm.token.value = data.response.token.token; | |
// IMPORTANT: Here we call `submit()` on the form element directly instead of using jQuery to prevent and infinite token request loop. | |
myForm.submit(); | |
}; | |
// Called when token creation fails. | |
var errorCallback = function(data) { | |
if (data.errorCode === 200) { | |
// This error code indicates that the ajax call failed. We recommend that you retry the token request. | |
} else { | |
alert(data.errorMsg); | |
} | |
}; | |
var tokenRequest = function() { | |
// Setup token request arguments | |
var args = { | |
sellerId: "<?php echo $settings['twocheckout_sellerid']; ?>", | |
publishableKey: "<?php echo $settings['twocheckout_publishable_key']; ?>", | |
ccNo: jQuery(".card-number").val(), | |
cvv: jQuery(".cvc").val(), | |
expMonth: jQuery(".ccard-expiry-month").val(), | |
expYear: jQuery(".card-expiry-year").val() | |
}; | |
// Make the token request | |
TCO.requestToken(successCallback, errorCallback, args); | |
}; | |
jQuery(function() { | |
// Pull in the public encryption key for our environment | |
TCO.loadPubKey('sandbox'); | |
jQuery("#leaky-paywall-payment-form").submit(function(e) { | |
// Call our token request function | |
tokenRequest(); | |
// Prevent form from submitting | |
return false; | |
}); | |
}); | |
</script> | |
<?php | |
leaky_paywall_card_form_full(); | |
return ob_get_clean(); | |
} | |
/** | |
* Validate additional fields during registration submission | |
* | |
* @since 3.5.1 | |
*/ | |
public function validate_fields() { | |
} | |
/** | |
* Load Stripe JS | |
* | |
* @since 3.7.0 | |
*/ | |
public function scripts() { | |
wp_enqueue_script( '2checkout', 'https://www.2checkout.com/checkout/api/2co.min.js', array( 'jquery' ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment