rendered = array();
$this->policy = array();
$this->affinipay_client = new AffiniPay_Client;
$this->mode = get_option( 'affinipay_operating_mode' );
foreach ( $atts as $att ) {
array_push( $this->atts, $att );
}
$this->valid_recurring = array(
'MONTH',
'MONTHLY',
'WEEK',
'WEEKLY',
'YEAR',
'YEARLY',
);
$this->form_fields = array(
'reference' => array(
'field' => 'reference',
'label' => 'Reference',
'placeholder' => 'Example: Invoice 123',
'validation' => 'reference',
'maxlength' => "128"
),
'amount' => array(
'field' => 'amount',
'label' => 'Amount',
'placeholder' => '29.95',
'validation' => 'numeric',
),
'customer_email' => array(
'field' => 'email',
'label' => 'Email Address',
'placeholder' => 'email@example.com',
'validation' => 'email',
),
'customer_name' => array(
'field' => 'name',
'label' => 'Cardholder Name',
'placeholder' => 'Joe Cardholder',
'validation' => 'alpha'
),
'customer_address' => array(
'field' => 'address1',
'label' => 'Mailing Address',
'placeholder' => '3700 N Capital of Texas',
'validation' => 'any',
),
'customer_address2' => array(
'field' => 'address2',
'label' => 'Apt / Suite',
'placeholder' => '',
'validation' => null,
),
'customer_city' => array(
'field' => 'city',
'label' => 'City',
'placeholder' => 'Austin',
'validation' => 'alpha',
),
'customer_country' => array(
'field' => 'country',
'label' => 'Country',
'placeholder' => '',
'validation' => 'alphanumeric',
),
'customer_state' => array(
'field' => 'state',
'label' => 'State',
'placeholder' => 'TX',
'validation' => 'state',
),
'customer_postal_code' => array(
'field' => 'postal_code',
'label' => 'Postal Code',
'placeholder' => '78730',
'validation' => 'postal_code',
'maxlength' => "9"
),
'customer_phone' => array(
'field' => 'phone',
'label' => 'Phone',
'placeholder' => '(123) 456-7890',
'validation' => 'phone',
'maxlength' => '22'
),
'number' => array(
'field' => 'number',
'label' => 'Card Number',
'placeholder' => '4242 4242 4242 4242',
'validation' => 'cc',
'required' => true
),
'exp_month' => array(
'field' => 'exp_month',
'label' => 'Month',
'placeholder' => '01',
'validation' => 'month',
),
'exp_year' => array(
'field' => 'exp_year',
'label' => 'Year',
'placeholder' => '2018',
'validation' => 'year',
),
'cvv' => array(
'field' => 'cvv',
'label' => 'CVV',
'placeholder' => '123',
'validation' => 'cvv',
),
);
}
function init( $atts = [], $content = null, $tag = null ) {
$output = null;
$this->atts = array_change_key_case( (array) $atts, CASE_LOWER );
$this->content = $content;
$this->atts = shortcode_atts([
'title' => 'Make a Payment',
'button_text' => 'Submit Payment',
'amount' => 0,
'account' => '',
'amount_field' => 'no',
'customer_fields' => 'no',
'recurs' => 'no',
'recurring_description' => '',
'recurring_interval' => 1,
'reference_field' => 'no',
'customer_email_field' => 'yes',
'customer_name_field' => 'yes',
'customer_address_field' => 'no',
'customer_address2_field' => 'no',
'customer_city_field' => 'no',
'customer_country_field' => 'no',
'customer_state_field' => 'no',
'customer_postalcode_field' => 'no',
'customer_phone_field' => 'no'
], $atts, $tag);
if (!is_ssl()) {
return '
' . 'Payment form unavailable because an SSL connection was not detected. Contact the webmaster.' . '
';
}
try {
if(!$this->get_required_fields()) return $this->showInvalidAccount();
} catch ( \ChargeIO_Error $e ) {
if ( $e instanceof AffiniPay_AuthenticationError ) {
if ( $this->affinipay_client->operating_mode != 'live' ) {
return '' . $e->getMessage() . '
';
}
}
}
if($this->atts['account'] == ''){
return $this->showInvalidAccount();
}
if ( $this->is_sane() ) {
return $this->render();
} else {
if ( $this->mode != 'live' ) {
return 'Invalid Configuration. Please check your API keys.
';
}
}
}
function showInvalidAccount(){
return 'Invalid Shortcode - Account not valid
';
}
function get_required_fields() {
$hasValidAccount = false;
try {
$merchant = $this->affinipay_client->getMerchant();
foreach ( $merchant->attributes['merchant_accounts'] as $account ) {
if ( $account['id'] == $this->atts['account'] ) {
$hasValidAccount = true;
$this->required = $account['required_payment_fields'];
break;
}
}
} catch ( \ChargeIO_Error $e ) {
throw $e;
}
return $hasValidAccount;
}
public function render() {
$output = null;
$output .= apply_filters( 'affinipay_before_payment_form', '' );
$output .= do_shortcode( '[affinipay-payment-form-open]' );
$output .= do_shortcode( '[affinipay-payment-form-wrapper-open]' );
if ( array_key_exists( 'customer_fields', $this->atts ) ) {
if ( $this->atts['customer_fields'] == 'yes' ) {
$output .= apply_filters( 'affinipay_before_render_customer_fields', '' );
$output .= do_shortcode( '[affinipay-payment-form-customer]' );
$output .= apply_filters( 'affinipay_after_render_customer_fields', '' );
}
}
if ( array_key_exists( 'amount_field', $this->atts ) ) {
$output .= apply_filters( 'affinipay_before_render_amount_field', '' );
$output .= do_shortcode( '[affinipay-payment-amount-field]' );
$output .= apply_filters( 'affinipay_after_render_amount_field', '' );
}
$output .= '';
if ( array_key_exists( 'reference_field', $this->atts ) ) {
if ( $this->atts['reference_field'] == 'yes' ) {
$output .= apply_filters( 'affinipay_before_render_reference_field', '' );
$output .= do_shortcode( '[affinipay-payment-reference-field]' );
$output .= apply_filters( 'affinipay_after_render_reference_field', '' );
}
if ( stristr( $this->required, 'reference' ) && ! in_array( 'reference', $this->rendered ) ) {
$output .= apply_filters( 'affinipay_before_render_reference_field', '' );
$output .= do_shortcode( '[affinipay-payment-reference-field]' );
$output .= apply_filters( 'affinipay_after_render_reference_field', '' );
}
}
if ( array_key_exists( 'customer_email_field', $this->atts ) ) {
if ( $this->atts['customer_fields'] == 'yes' || $this->atts['customer_email_field'] == 'yes' ) {
$output .= apply_filters( 'affinipay_before_render_email_field', '' );
$output .= do_shortcode( '[affinipay-customer-email-field]' );
$output .= apply_filters( 'affinipay_after_render_email_field', '' );
}
if ( stristr( $this->required, 'email' ) && ! in_array( 'email', $this->rendered ) ) {
$output .= apply_filters( 'affinipay_before_render_email_field', '' );
$output .= do_shortcode( '[affinipay-customer-email-field]' );
$output .= apply_filters( 'affinipay_after_render_email_field' );
}
}
if ( array_key_exists( 'customer_name_field', $this->atts ) ) {
if ( $this->atts['customer_name_field'] == 'yes' ) {
$output .= apply_filters( 'affinipay_before_render_name_field', '' );
$output .= do_shortcode( '[affinipay-customer-name-field]' );
$output .= apply_filters( 'affinipay_after_render_name_field', '' );
}
if ( stristr( $this->required, 'name' ) && ! in_array( 'name', $this->rendered ) ) {
$output .= apply_filters( 'affinipay_before_render_name_field', '' );
$output .= do_shortcode( '[affinipay-customer-name-field]' );
$output .= apply_filters( 'affinipay_after_render_name_field', '' );
}
}
if ( array_key_exists( 'customer_address_field', $this->atts ) ) {
if ( $this->atts['customer_address_field'] == 'yes' ) {
$output .= apply_filters( 'affinipay_before_render_address_field', '' );
$output .= do_shortcode( '[affinipay-customer-address1-field]' );
$output .= apply_filters( 'affinipay_after_render_address_field', '' );
}
if ( stristr( $this->required, 'address1' ) && ! in_array( 'address1', $this->rendered ) ) {
$output .= apply_filters( 'affinipay_before_render_address_field', '' );
$output .= do_shortcode( '[affinipay-customer-address1-field]' );
$output .= apply_filters( 'affinipay_after_render_address_field', '' );
}
}
if ( array_key_exists( 'customer_address2_field', $this->atts ) ) {
if ( $this->atts['customer_address2_field'] == 'yes' ) {
$output .= apply_filters( 'affinipay_before_render_address2_field', '' );
$output .= do_shortcode( '[affinipay-customer-address2-field]' );
$output .= apply_filters( 'affinipay_after_render_address2_field', '' );
}
if ( stristr( $this->required, 'address2' ) && ! in_array( 'address2', $this->rendered ) ) {
$output .= apply_filters( 'affinipay_before_render_address2_field', '' );
$output .= do_shortcode( '[affinipay-customer-address2-field]' );
$output .= apply_filters( 'affinipay_after_render_address2_field', '' );
}
}
if ( array_key_exists( 'customer_city_field', $this->atts ) ) {
if ( $this->atts['customer_city_field'] == 'yes' ) {
$output .= apply_filters( 'affinipay_before_render_city_field', '' );
$output .= do_shortcode( '[affinipay-customer-city-field]' );
$output .= apply_filters( 'affinipay_after_render_city_field', '' );
}
if ( stristr( $this->required, 'city' ) && ! in_array( 'city', $this->rendered ) ) {
$output .= apply_filters( 'affinipay_before_render_city_field', '' );
$output .= do_shortcode( '[affinipay-customer-city-field]' );
$output .= apply_filters( 'affinipay_after_render_city_field', '' );
}
}
if ( array_key_exists( 'customer_country_field', $this->atts ) ) {
if ( $this->atts['customer_fields'] == 'yes' || $this->atts['customer_country_field'] == 'yes' ) {
$output .= apply_filters( 'affinipay_before_render_country_field', '' );
$output .= do_shortcode( '[affinipay-customer-country-field]' );
$output .= apply_filters( 'affinipay_after_render_country_field', '' );
}
if ( stristr( $this->required, 'country' ) && ! in_array( 'country', $this->rendered ) ) {
$output .= apply_filters( 'affinipay_before_render_country_field', '' );
$output .= do_shortcode( '[affinipay-customer-country-field]' );
$output .= apply_filters( 'affinipay_after_render_country_field', '' );
}
}
if ( array_key_exists( 'customer_state_field', $this->atts ) ) {
if ( $this->atts['customer_state_field'] == 'yes' ) {
$output .= apply_filters( 'affinipay_before_render_state_field', '' );
$output .= do_shortcode( '[affinipay-customer-state-field]' );
$output .= apply_filters( 'affinipay_after_render_state_field', '' );
array_push( $this->rendered, 'state' );
}
if ( stristr( $this->required, 'state' ) && ! in_array( 'state', $this->rendered ) ) {
$output .= apply_filters( 'affinipay_before_render_state_field', '' );
$output .= do_shortcode( '[affinipay-customer-state-field]' );
$output .= apply_filters( 'affinipay_after_render_state_field', '' );
array_push( $this->rendered, 'state' );
}
}
if ( array_key_exists( 'customer_postalcode_field', $this->atts ) ) {
if ( $this->atts['customer_postalcode_field'] == 'yes' ) {
$output .= apply_filters( 'affinipay_before_postalcode_field', '' );
$output .= do_shortcode( '[affinipay-customer-postal-code-field]' );
$output .= apply_filters( 'affinipay_after_postalcode_field', '' );
}
if ( stristr( $this->required, 'postal_code' ) && ! in_array( 'postal_code', $this->rendered ) ) {
$output .= apply_filters( 'affinipay_before_postalcode_field', '' );
$output .= do_shortcode( '[affinipay-customer-postal-code-field]' );
$output .= apply_filters( 'affinipay_after_postalcode_field', '' );
}
}
if ( array_key_exists( 'customer_phone_field', $this->atts ) ) {
if ( $this->atts['customer_phone_field'] == 'yes' ) {
$output .= apply_filters( 'affinipay_before_render_phone_field', '' );
$output .= do_shortcode( '[affinipay-customer-phone-field]' );
$output .= apply_filters( 'affinipay_after_render_phone_field', '' );
}
if ( stristr( $this->required, 'phone' ) && ! in_array( 'phone', $this->rendered ) ) {
$output .= apply_filters( 'affinipay_before_render_phone_field', '' );
$output .= do_shortcode( '[affinipay-customer-phone-field]' );
$output .= apply_filters( 'affinipay_after_render_phone_field', '' );
}
}
$output .= apply_filters( 'affinipay_before_render_payment_fields', '' );
$output .= do_shortcode( '[affinipay-payment-form]' );
$output .= apply_filters( 'affinipay_after_render_payment_fields', '' );
if ( array_key_exists( 'recurs', $this->atts ) ) {
if ( in_array( strtoupper( $this->atts['recurs'] ), $this->valid_recurring ) ) {
$this->set_recurring_variants();
$output .= apply_filters( 'affinipay_before_render_recurring', '' );
$output .= do_shortcode( '[affinipay-payment-form-recurring]' );
$output .= apply_filters( 'affinipay_after_render_recurring', '' );
}
}
$output .= do_shortcode( '[affinipay-payment-form-button]' );
$output .= do_shortcode( '[affinipay-payment-form-wrapper-close]' );
$output .= do_shortcode( '[affinipay-payment-form-close]' );
$output .= apply_filters( 'affinipay_after_payment_form', '' );
if ( ! is_null( $this->content ) ) {
$output .= apply_filters( 'the_content', $this->content );
}
return $output;
}
public function renderOptions($start, $end){
$result = '';
for ($current = $start; $current <= $end; $current++) {
$item = str_pad($current, 2, "0", STR_PAD_LEFT);
$result .= '
';
}
return $result;
}
public function render_payment_form() {
$months = $this->renderOptions(1, 12);
$years = $this->renderOptions(intval(date("Y")), intval(date("Y")) + 20);
$output = null;
$required = stristr( $this->required, 'cvv') ? 'required' : '';
$output .= apply_filters( 'affinipay_before_payment_form', '' );
$output .= '
';
$output .= apply_filters( 'affinipay_after_payment_form', '' );
return $output;
}
function render_customer_fields() {
$output = null;
$output .= do_shortcode( '[affinipay-customer-name-field]' );
$output .= do_shortcode( '[affinipay-customer-address1-field]' );
$output .= do_shortcode( '[affinipay-customer-address2-field]' );
$output .= do_shortcode( '[affinipay-customer-city-field]' );
$output .= do_shortcode( '[affinipay-customer-state-field]' );
$output .= do_shortcode( '[affinipay-customer-postal-code-field]' );
$output .= do_shortcode( '[affinipay-customer-email-field]' );
$output .= do_shortcode( '[affinipay-customer-phone-field]' );
return $output;
}
function render_recurring_section() {
$output = null;
// if ($this->atts['recurring_fields'] == 'yes') {
// $output .= '
//
//
First Charge:
//
Today
//
//
//
Recurs:
//
//
//
//
// on '. date('l') .'
//
//
// ';
// } else {
$output .= '';
$output .= '';
// }
return $output;
}
function render_payment_form_wrapper_open() {
return '' . $this->atts['title'] . '
*Required fields';
}
function render_payment_form_wrapper_close() {
return '
';
}
function render_payment_form_open() {
return '';
}
function render_amount_field() {
if ( ! in_array( 'amount_field', $this->rendered ) ) {
array_push( $this->rendered, 'amount_field' );
if ( $this->atts['amount_field'] == 'yes' ) {
$data = $this->form_fields['amount'];
$data['value'] = number_format($this->atts['amount'], 2);
return $this->render_input_field( $data, true );
} else {
if ( $this->atts['amount'] == 0 ) {
return $this->render_input_field( $this->form_fields['amount'], true );
} else {
return '';
}
}
}
return null;
}
function render_reference_field() {
$content = $this->render_input_field( $this->form_fields['reference'] );
return $content;
}
function render_customer_email_field() {
$content = $this->render_input_field( $this->form_fields['customer_email'] );
return $content;
}
function render_customer_name_field() {
$content = $this->render_input_field( $this->form_fields['customer_name'] );
return $content;
}
function render_customer_address1_field() {
$content = $this->render_input_field( $this->form_fields['customer_address'] );
return $content;
}
function render_customer_address2_field() {
$content = $this->render_input_field( $this->form_fields['customer_address2'] );
return $content;
}
function render_customer_city_field() {
$content = $this->render_input_field( $this->form_fields['customer_city'] );
return $content;
}
function render_customer_country_field() {
$content = $this->render_input_field( $this->form_fields['customer_country'] );
return $content;
}
function render_customer_state_field() {
$content = $this->render_input_field( $this->form_fields['customer_state'] );
return $content;
}
/**
* @return string
*/
function render_customer_postal_code_field() {
$content = $this->render_input_field( $this->form_fields['customer_postal_code'] );
return $content;
}
function render_customer_phone_field() {
$content = $this->render_input_field( $this->form_fields['customer_phone'] );
return $content;
}
function render_payment_form_button() {
if ( ! in_array( 'payment_button', $this->rendered ) ) {
array_push( $this->rendered, 'payment_button' );
return '';
}
}
function load_payment_form_ui_js() {
?>
$recur_interval,
'interval_delay' => $recur_ends,
);
$params['recur'] = $recurring;
}
try {
$gw_resp = $this->affinipay_client->createCharge( $amount, $token, $params );
wp_send_json( $gw_resp->attributes, 200);
} catch ( \Exception $e ) {
wp_send_json( json_decode($e->getJson()), 500);
}
}
private function set_recurring_variants() {
switch ( strtoupper( $this->atts['recurs'] ) ) {
case 'MONTHLY':
$this->atts['recurs'] = 'MONTH';
break;
case 'WEEKLY':
$this->atts['recurs'] = 'WEEK';
break;
case 'YEARLY':
$this->atts['recurs'] = 'YEAR';
break;
default:
break;
}
}
private function is_sane() {
$valid_config = $this->check_empty_keys();
if ( $this->mode == 'live' && $valid_config ) {
$this->affinipay_client->set_operating_mode( 'live' );
$valid_config = $this->verify_https();
}
return $valid_config && $this->atts['account'] != '';
}
private function check_empty_keys() {
$public = get_option( 'affinipay_public_key' );
$secret = get_option( 'affinipay_secret_key' );
return $public !== '' && $secret !== '';
}
function verify_https() {
// TODO: Research a more reliable way to determine if the payment form is requested over https
return false;
}
/**
* @param string
* @param bool $forceRequired
* @return string
*/
function render_input_field( $opts, $forceRequired = false) {
$field = (array_key_exists( 'field', $opts ) ? $opts['field'] : null);
$placeholder = (array_key_exists( 'placeholder', $opts ) ? $opts['placeholder'] : null);
$validation = (array_key_exists( 'validation', $opts ) ? $opts['validation'] : null);
$label = (array_key_exists( 'label', $opts ) ? $opts['label'] : null);
$maxlength = (array_key_exists( 'maxlength', $opts ) ? ' maxlength="'.$opts['maxlength'] . '" ': null);
$value = (array_key_exists( 'value', $opts ) ? ' value="'.$opts['value'] . '" ': null);
$required = $forceRequired || stristr( $this->required, $opts['field']) ? 'required' : '';
if($required !== ''){
$label .= '*';
}
if ( ! in_array( $field, $this->rendered ) ) {
$html = '
';
array_push( $this->rendered, $field );
return $html;
} else {
return '';
}
}
}