<?php /** * Plugin Name: AWPCP custom split payment * Plugin URI: http://alhoseany.com * Description: A plugin that adds the ability to split payment between admin and region managers by states in AWPCP plugin. * Version: 1.0 * Author: alhoseany * Author URI: http://alhoseany.com * License: GPL2 */ //register settings function awpcp_csp_settings_init() { register_setting('awpcp_csp_settings', 'awpcp_csp_settings'); } //add settings page to menu function add_awpcp_csp_settings_page() { add_menu_page(__('Split Payments Options'), __('Split Payments'), 'manage_options', 'awpcp_csp_settings', 'awpcp_csp_options'); } //add actions add_action('admin_init', 'awpcp_csp_settings_init'); add_action('admin_menu', 'add_awpcp_csp_settings_page'); //start settings page function awpcp_csp_options() { if (!current_user_can('manage_options')) { wp_die(__('You do not have sufficient permissions to access this page.')); } if (!function_exists('awpcp_regions_api')) { wp_die(__('This plugin needs the AWPCP plugin and the regions manager module to work.')); } if (!isset($_REQUEST['settings-updated'])) $_REQUEST['settings-updated'] = false; $all_regions = awpcp_regions_api(); $states = $all_regions->get_children(191); ?> <style> table { padding: 20px; } tr:hover { background: none repeat scroll 0 0 #F0F0FF; } td { vertical-align: middle !important; } </style> <div> <div id="icon-options-general"></div> <h2><?php _e('Split Payments Options') //your admin panel title ?></h2> <?php //show saved options message if (false !== $_REQUEST['settings-updated']) : ?> <div class="updated settings-error" id="setting-error-settings_updated"> <p><strong>Settings saved.</strong></p></div><br> <?php endif; ?> <form method="post" action="options.php"> <?php settings_fields('awpcp_csp_settings'); ?> <?php $options = get_option('awpcp_csp_settings'); ?> <table class="wp-list-table widefat fixed pages" cellspacing="0"> <thead> <tr> <th>State</th> <th>Paypal Email</th> <th>Percentage (without %)</th> </tr> </thead> <tbody> <?php $c = 0; foreach ($states as $state) { $this_state = $all_regions->find_by_id($state)->region_name; $this_state_id = $all_regions->find_by_id($state)->region_id; if($this_state){ ?> <tr class="<?= ($c++ % 2 == 1) ? '' : 'alternate' ?>"> <td><strong><?php echo $this_state; ?></strong></td> <td> <input id="awpcp_csp_settings[<?php echo $this_state; ?>_email]" type="email" size="36" name="awpcp_csp_settings[<?php echo $this_state; ?>_email]" value="<?php esc_attr_e($options[$this_state . '_email']); ?>" /> </td> <td> <input id="awpcp_csp_settings[<?php echo $this_state; ?>_percentage]" type="number" size="10" name="awpcp_csp_settings[<?php echo $this_state; ?>_percentage]" value="<?php if ($options[$this_state . '_percentage']) { esc_attr_e($options[$this_state . '_percentage']); } else { echo '10'; } ?>" /> </td> </tr> <?php } } ?> </table> <?php submit_button(); ?> </form> </div><!-- END wrap --> <?php } // Use this class to construct the NVP calls to the paypal service class PayPal_CallerService { public $API_UserName; public $API_Password; public $API_Signature; public $API_Endpoint; public $last_request; public $last_response; public $version; public $subject; public function __construct($credentials) { if (is_array($credentials)) { $this->API_UserName = (isset($credentials['API_USERNAME']) && !empty($credentials['API_USERNAME'])) ? $credentials['API_USERNAME'] : ''; $this->API_Password = (isset($credentials['API_PASSWORD']) && !empty($credentials['API_PASSWORD'])) ? $credentials['API_PASSWORD'] : ''; $this->API_Signature = (isset($credentials['API_SIGNATURE']) && !empty($credentials['API_SIGNATURE'])) ? $credentials['API_SIGNATURE'] : ''; $this->API_Endpoint = (isset($credentials['API_ENDPOINT']) && !empty($credentials['API_ENDPOINT'])) ? $credentials['API_ENDPOINT'] : 'https://api-3t.sandbox.paypal.com/nvp'; $this->version = (isset($credentials['VERSION']) && !empty($credentials['VERSION'])) ? $credentials['VERSION'] : '60.0'; $this->subject = (isset($credentials['SUBJECT']) && !empty($credentials['SUBJECT'])) ? $credentials['SUBJECT'] : ''; } else { throw new Exception('You must pass in an array of credentials to instantiate this class.'); } } public function callPayPal($methodName, $base_call = NULL) { $header_call = "&PWD=" . urlencode($this->API_Password) . "&USER=" . urlencode($this->API_UserName) . "&SIGNATURE=" . urlencode($this->API_Signature) . "&SUBJECT=" . urlencode($this->subject) . "&VERSION=" . urlencode($this->version); $call = $header_call . $base_call; //setting the curl parameters. $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->API_Endpoint); curl_setopt($ch, CURLOPT_VERBOSE, 1); //Turning off the server and peer verification(TrustManager Concept). curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); //If USE_PROXY constant set to TRUE in Constants.php, then only proxy will be enabled. //Set proxy name to PROXY_HOST and port number to PROXY_PORT in constants.php /* if(USE_PROXY) curl_setopt ($ch, CURLOPT_PROXY, PROXY_HOST.":".PROXY_PORT); */ //Check if version is included in $nvpStr else include the version. if (strlen(str_replace('VERSION=', '', strtoupper($call))) == strlen($call)) { $nvpStr = "&VERSION=" . urlencode($this->version) . $call; } $nvpreq = "METHOD=" . urlencode($methodName) . $call; $this->last_request = $nvpreq; //Setting the nvpreq as POST FIELD to curl curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq); //Getting response from server $response = curl_exec($ch); //Converting NVPResponse to an Associative Array $nvpResArray = $this->deformatNVP($response); $nvpReqArray = $this->deformatNVP($nvpreq); $_SESSION['nvpReqArray'] = $nvpReqArray; if (curl_errno($ch)) { throw new Exception('Curl error: ' . curl_errno($ch) . ' - ' . curl_error($ch)); } else { //Closing the curl curl_close($ch); } $this->last_response = $nvpResArray; return $nvpResArray; } /** * This function will take NVPString and convert it to an Associative Array and it will decode the response. * It is usefull to search for a particular key and displaying arrays. * * @nvpstr is NVPString. * @nvpArray is Associative Array. */ public static function deformatNVP($nvpstr) { $intial = 0; $nvpArray = array(); while (strlen($nvpstr)) { //postion of Key $keypos = strpos($nvpstr, '='); //position of value $valuepos = strpos($nvpstr, '&') ? strpos($nvpstr, '&') : strlen($nvpstr); /* getting the Key and Value values and storing in a Associative Array */ $keyval = substr($nvpstr, $intial, $keypos); $valval = substr($nvpstr, $keypos + 1, $valuepos - $keypos - 1); //decoding the respose $nvpArray[urldecode($keyval)] = urldecode($valval); $nvpstr = substr($nvpstr, $valuepos + 1, strlen($nvpstr)); } return $nvpArray; } } function awpcp_split_payments_plugin($transaction) { if ($transaction->is_completed() && $transaction->was_payment_successful() && !awpcp_current_user_is_admin()) { if ($transaction->get('ad-id') && class_exists('AWPCP_Ad')) { $regions = AWPCP_Ad::get_ad_regions($transaction->get('ad-id')); $options = get_option('awpcp_csp_settings'); if($options[$regions[0][city] . '_email'] && $options[$regions[0][city] . '_percentage'] > 0 ){ $pay_email = $options[$regions[0][city] . '_email']; $pay_percent = $options[$regions[0][city] . '_percentage']; }elseif($options[$regions[0][state] . '_email'] && $options[$regions[0][state] . '_percentage'] > 0 ){ $pay_email = $options[$regions[0][state] . '_email']; $pay_percent = $options[$regions[0][state] . '_percentage']; }else{ $pay_email = false; $pay_percent = false; } if ($pay_email && $pay_percent && $transaction->get_total_amount() > 0) { $region_manager_amount = ($pay_percent / 100) * $transaction->get_total_amount(); // pay the user $credentials['API_USERNAME'] = 'sales_api1.areamusicscene.com'; $credentials['API_PASSWORD'] = 'FHJZ4DNEV2CCCJ8L'; $credentials['API_SIGNATURE'] = 'AY4Ri-uV76WllFKd2cxduagZQFOEApdsk9MMP3r8cKGRbCtOtOnClQFs'; $credentials['API_ENDPOINT'] = 'https://api-3t.paypal.com/nvp'; $amount = urlencode($region_manager_amount); $id = urlencode($transaction->get('ad-id')); $note = urlencode('Comission payment for new ad.'); $subject = urlencode("Payment from areamusicscene.com"); $type = urlencode('EmailAddress'); $currency = urlencode('USD'); $customer_email = urlencode($pay_email); $base_call = "&L_EMAIL0=" . $customer_email . "&L_AMT0=" . $amount . "&L_UNIQUEID0=" . $id . "&L_NOTE0=" . $refund_note . "&EMAILSUBJECT=" . $subject . "&RECEIVERTYPE=" . $type . "&CURRENCYCODE=" . $currency; $PayPal = new PayPal_CallerService($credentials); $status = $PayPal->callPayPal("MassPay", $base_call); //Do something if it is successfully sent if ($status) { if ($status['ACK'] == "Success") { $msg = "New ad commision payment has been made.rn"; $msg .= "Commision Info : rn"; $msg .= "ad : " . url_showad($transaction->get('ad-id')) . " rn"; $msg .= "State : " . $regions[0][state] . " rn"; $msg .= "City : " . $regions[0][city] . " rn"; $msg .= "paypal email : " . $pay_email . " rn"; $msg .= "commission percentage : " . $pay_percent . "% rn"; $msg .= "commission amount : $" . $region_manager_amount . " rn"; $msg .= "--------------------------------------------------------------- rn"; //$admin_email = get_option( 'admin_email' ); $admin_email = 'sales@areamusicscene.com'; mail($admin_email, 'New ad commision payment ', $msg, 'From: "areamusicscene.com" <' . $admin_email . '>'); } else if ($status['ACK'] == "Failure") { $msg = "New ad commision payment has failed to be sent.rn"; $msg .= "Commision Info : rn"; $msg .= "ad : " . url_showad($transaction->get('ad-id')) . " rn"; $msg .= "State : " . $regions[0][state] . " rn"; $msg .= "City : " . $regions[0][city] . " rn"; $msg .= "paypal email : " . $pay_email . " rn"; $msg .= "commission percentage : " . $pay_percent . "% rn"; $msg .= "commission amount : $" . $region_manager_amount . " rn"; $msg .= "--------------------------------------------------------------- rn".var_dump($status); //$admin_email = get_option( 'admin_email' ); $admin_email = 'sales@areamusicscene.com'; mail($admin_email, 'ad commision payment failed', $msg, 'From: "areamusicscene.com" <' . $admin_email . '>'); //var_dump($status); } } } } // end payment code } } add_action('awpcp-transaction-status-updated', 'awpcp_split_payments_plugin');