WordPress.org

Plugin Directory

Changeset 632221


Ignore:
Timestamp:
11/30/12 14:47:41 (17 months ago)
Author:
kovshenin
Message:

Allow PayPal credentials to be predefined with a filter.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • camptix/trunk/addons/payment-paypal.php

    r626956 r632221  
    3131    function camptix_init() { 
    3232        $this->options = array_merge( array( 
     33            'api_predef' => '', 
    3334            'api_username' => '', 
    3435            'api_password' => '', 
     
    4849     */ 
    4950    function payment_settings_fields() { 
    50         $this->add_settings_field_helper( 'api_username', __( 'API Username', 'camptix' ), array( $this, 'field_text' ) ); 
    51         $this->add_settings_field_helper( 'api_password', __( 'API Password', 'camptix' ), array( $this, 'field_text' ) ); 
    52         $this->add_settings_field_helper( 'api_signature', __( 'API Signature', 'camptix' ), array( $this, 'field_text' ) ); 
    53         $this->add_settings_field_helper( 'sandbox', __( 'Sandbox Mode', 'camptix' ), array( $this, 'field_yesno' ), 
    54             sprintf( __( "The PayPal Sandbox is a way to test payments without using real accounts and transactions. If you'd like to use Sandbox Mode, you'll need to create a %s account and obtain the API credentials for your sandbox user.", 'camptix' ), sprintf( '<a href="https://developer.paypal.com/">%s</a>', __( 'PayPal Developer', 'camptix' ) ) ) 
    55         ); 
     51 
     52        // Allow pre-defined accounts if any are defined by plugins. 
     53        if ( count( $this->get_predefined_accounts() ) > 0 ) 
     54            $this->add_settings_field_helper( 'api_predef', __( 'Predefined Account', 'camptix' ), array( $this, 'field_api_predef' ) ); 
     55 
     56        // Settings fields are not needed when a predefined account is chosen. 
     57        // These settings fields should *never* expose predefined credentials. 
     58        if ( ! $this->get_predefined_account() ) { 
     59            $this->add_settings_field_helper( 'api_username', __( 'API Username', 'camptix' ), array( $this, 'field_text' ) ); 
     60            $this->add_settings_field_helper( 'api_password', __( 'API Password', 'camptix' ), array( $this, 'field_text' ) ); 
     61            $this->add_settings_field_helper( 'api_signature', __( 'API Signature', 'camptix' ), array( $this, 'field_text' ) ); 
     62            $this->add_settings_field_helper( 'sandbox', __( 'Sandbox Mode', 'camptix' ), array( $this, 'field_yesno' ), 
     63                sprintf( __( "The PayPal Sandbox is a way to test payments without using real accounts and transactions. If you'd like to use Sandbox Mode, you'll need to create a %s account and obtain the API credentials for your sandbox user.", 'camptix' ), sprintf( '<a href="https://developer.paypal.com/">%s</a>', __( 'PayPal Developer', 'camptix' ) ) ) 
     64            ); 
     65        } 
     66    } 
     67 
     68    /** 
     69     * Predefined accounts field callback 
     70     * 
     71     * Renders a drop-down select with a list of predefined accounts 
     72     * to select from, as well as some js for better ux. 
     73     * 
     74     * @uses $this->get_predefined_accounts() 
     75     */ 
     76    function field_api_predef( $args ) { 
     77        $accounts = $this->get_predefined_accounts(); 
     78        if ( empty( $accounts ) ) 
     79            return; 
     80 
     81        ?> 
     82        <select id="camptix-predef-select" name="<?php echo esc_attr( $args['name'] ); ?>"> 
     83            <option value=""><?php _e( 'None', 'camptix' ); ?></option> 
     84            <?php foreach ( $accounts as $key => $account ) : ?> 
     85            <option value="<?php echo esc_attr( $key ); ?>" <?php selected( $args['value'], $key ); ?>><?php echo esc_html( $account['label'] ); ?></option> 
     86            <?php endforeach; ?> 
     87        </select> 
     88        <!-- Let's disable the rest of the fields unless None is selected --> 
     89        <script> 
     90        jQuery(document).ready(function($){ 
     91            var select = $('#camptix-predef-select')[0]; 
     92            $(select).on('change', function(){ 
     93                $('[name^="camptix_payment_options_paypal"]').each(function(){ 
     94                    // Don't disable myself. 
     95                    if (this == select) 
     96                        return; 
     97 
     98                    $(this).prop('disabled', select.value.length > 0); 
     99                    $(this).toggleClass('disabled', select.value.length > 0); 
     100                }); 
     101            }); 
     102        }); 
     103        </script> 
     104        <?php 
     105    } 
     106 
     107    /** 
     108     * Get an array of predefined PayPal accounts 
     109     * 
     110     * Runs an empty array through a filter, where one might specifiy a list of 
     111     * predefined PayPal credentials, through a plugin or something. 
     112     * 
     113     * @static $predefs 
     114     * @return array An array of predefined accounts (or an empty one) 
     115     */ 
     116    function get_predefined_accounts() { 
     117        static $predefs = false; 
     118        if ( false === $predefs ) 
     119            $predefs = apply_filters( 'camptix_paypal_predefined_accounts', array() ); 
     120 
     121        return $predefs; 
     122    } 
     123 
     124    /** 
     125     * Get a predefined account 
     126     * 
     127     * If the $key argument is false or not set, this function will look up the active 
     128     * predefined account, otherwise it'll look up the one under the given key. After a 
     129     * predefined account is set, PayPal credentials will be overwritten during API 
     130     * requests, but never saved/exposed. Useful with array_merge(). 
     131     * 
     132     * @return array An array with credentials, or an empty array if key not found. 
     133     */ 
     134    function get_predefined_account( $key = false ) { 
     135        $accounts = $this->get_predefined_accounts(); 
     136 
     137        if ( false === $key ) 
     138            $key = $this->options['api_predef']; 
     139 
     140        if ( ! array_key_exists( $key, $accounts ) ) 
     141            return array(); 
     142 
     143        return $accounts[ $key ]; 
    56144    } 
    57145 
     
    74162        if ( isset( $input['sandbox'] ) ) 
    75163            $output['sandbox'] = (bool) $input['sandbox']; 
     164 
     165        if ( isset( $input['api_predef'] ) ) { 
     166 
     167            // If a valid predefined account is set, erase the credentials array. 
     168            // We do not store predefined credentials in options, only code. 
     169            if ( $this->get_predefined_account( $input['api_predef'] ) ) { 
     170                $output = array_merge( $output, array( 
     171                    'api_username' => '', 
     172                    'api_password' => '', 
     173                    'api_signature' => '', 
     174                    'sandbox' => false, 
     175                ) ); 
     176            } else { 
     177                $input['api_predef'] = ''; 
     178            } 
     179 
     180            $output['api_predef'] = $input['api_predef']; 
     181        } 
    76182 
    77183        return $output; 
     
    401507        ); 
    402508 
     509        // Replace creds from a predefined account if any. 
     510        $options = array_merge( $this->options, $this->get_predefined_account( $this->options['api_predef'] ) ); 
     511 
    403512        $order = $this->get_order( $payment_token ); 
    404513        $this->fill_payload_with_order( $payload, $order ); 
     
    408517        if ( isset( $response['ACK'], $response['TOKEN'] ) && 'Success' == $response['ACK'] ) { 
    409518            $token = $response['TOKEN']; 
    410  
    411             $url = $this->options['sandbox'] ? 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout' : 'https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout'; 
     519            $url = $options['sandbox'] ? 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout' : 'https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout'; 
    412520            $url = add_query_arg( 'token', $token, $url ); 
    413521            wp_redirect( esc_url_raw( $url ) ); 
     
    452560     */ 
    453561    function request( $payload = array() ) { 
    454         $url = $this->options['sandbox'] ? 'https://api-3t.sandbox.paypal.com/nvp' : 'https://api-3t.paypal.com/nvp'; 
     562        // Replace creds from a predefined account if any. 
     563        $options = array_merge( $this->options, $this->get_predefined_account( $this->options['api_predef'] ) ); 
     564 
     565        $url = $options['sandbox'] ? 'https://api-3t.sandbox.paypal.com/nvp' : 'https://api-3t.paypal.com/nvp'; 
     566 
    455567        $payload = array_merge( array( 
    456             'USER' => $this->options['api_username'], 
    457             'PWD' => $this->options['api_password'], 
    458             'SIGNATURE' => $this->options['api_signature'], 
     568            'USER' => $options['api_username'], 
     569            'PWD' => $options['api_password'], 
     570            'SIGNATURE' => $options['api_signature'], 
    459571            'VERSION' => '88.0', // https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_nvp_PreviousAPIVersionsNVP 
    460572        ), (array) $payload ); 
     
    467579     */ 
    468580    function verify_ipn( $payload = array() ) { 
    469         $url = $this->options['sandbox'] ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr'; 
     581        // Replace creds from a predefined account if any. 
     582        $options = array_merge( $this->options, $this->get_predefined_account( $this->options['api_predef'] ) ); 
     583 
     584        $url = $options['sandbox'] ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr'; 
    470585        $payload = 'cmd=_notify-validate&' . http_build_query( $payload ); 
    471586        return wp_remote_post( $url, array( 'body' => $payload, 'timeout' => 20 ) ); 
Note: See TracChangeset for help on using the changeset viewer.