<?php
/*
Plugin Name: درگاه پرداخت پارس پال ووکامرس
Plugin URI: http://www.parspal.com
Description: افزونه درگاه پرداخت پارس پال فروشگاه ساز ووکامرس
Version: 1.0
Author: Parspal Team
Author URI: http://www.parspal.com
*/
// Note : اين افزونه بر اساس افزونه درگاه پرداخت بانک ملت طراحي شده است -- woocommerce.ir
if(!class_exists('nusoap_client'))
require_once("nusoap.php");
add_action('plugins_loaded', 'woocommerce_parspal_init', 0);
function woocommerce_parspal_init() {
if ( !class_exists( 'WC_Payment_Gateway' ) ) return;
if($_GET['msg']!=''){
add_action('the_content', 'showParspalMessage');
}
function showParspalMessage($content){
$neclss = htmlentities($_GET['type']);
if($neclss == 'success')
$neclss = 'message';
return '<div class="box '.htmlentities($_GET['type']).'-box woocommerce-'.$neclss.'">'.urldecode($_GET['msg']).'</div>'.$content;
}
class WC_Parspal_Pay extends WC_Payment_Gateway {
protected $msg = array();
public function __construct(){
// Go wild in here
$this -> id = 'parspal';
$this -> method_title = __('پارس پال', 'mrova');
$this -> icon = WP_PLUGIN_URL . "/" . plugin_basename(dirname(__FILE__)) . '/images/logo.png';
$this -> has_fields = false;
$this -> init_form_fields();
$this -> init_settings();
$this -> title = $this -> settings['title'];
$this -> merchantid = $this -> settings['merchantid'];
$this -> password = $this -> settings['password'];
$this -> redirect_page_id = $this -> settings['redirect_page_id'];
$this -> msg['reversal'] = "";
$this -> msg['status'] = "";
$this -> msg['message'] = "";
$this -> msg['class'] = "";
add_action( 'woocommerce_api_' . strtolower( get_class( &$this ) ), array( $this, 'check_parspal_response' ) );
add_action('valid-parspal-request', array($this, 'successful_request'));
if ( version_compare( WOOCOMMERCE_VERSION, '2.0.0', '>=' ) ) {
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
} else {
add_action( 'woocommerce_update_options_payment_gateways', array( $this, 'process_admin_options' ) );
}
add_action('woocommerce_receipt_parspal', array($this, 'receipt_page'));
}
function init_form_fields(){
$this -> form_fields = array(
'enabled' => array(
'title' => __('فعال / غیر فعال کردن', 'mrova'),
'type' => 'checkbox',
'label' => __('انتخاب وضعیت درگاه پرداخت پارس پال', 'mrova'),
'default' => 'no'),
'title' => array(
'title' => __('عنوان درگاه', 'mrova'),
'type'=> 'text',
'description' => __('عنوان درگاه در هنگام انتخاب درگاه پرداخت', 'mrova'),
'default' => __('پرداخت از طریق درگاه پرداخت پارس پال', 'mrova')),
'description' => array(
'title' => __('توضیحات درگاه', 'mrova'),
'type' => 'textarea',
'description' => __('توضیحات نوشته شده در زیر لوگوی درگاه هنگام پرداخت', 'mrova'),
'default' => __('پرداخت با استفاده از درگاه برداخت پارس پال از طریق کلبه کارت های بانکی عضو شتاب', 'mrova')),
'merchantid' => array(
'title' => __('شناسه درگاه', 'mrova'),
'type' => 'text',
'description' => __('شناسه درگاه يا همان MerchantID')),
'password' => array(
'title' => __('کلمه عبور', 'mrova'),
'type' => 'text',
'description' => __('کلمه عبور يا همان Password')),
'redirect_page_id' => array(
'title' => __('برگه بازگشت'),
'type' => 'select',
'options' => $this -> get_pages('انتخاب برگه'),
'description' => __('ادرس بازگشت از پرداخت در هنگام پرداخت')
)
);
}
/**
* Admin Panel Options
* - Options for bits like 'title' and availability on a country-by-country basis
**/
public function admin_options(){
echo '<h3>'.__('درگاه پرداخت پارس پال', 'mrova').'</h3>';
// echo '<p>'.__('درگاه پرداخت بانک ملت ایران').'</p>';
echo '<table class="form-table">';
$this -> generate_settings_html();
echo '</table>';
}
/**
* There are no payment fields for parspal, but we want to show the description if set.
**/
function payment_fields(){
if($this -> description) echo wpautop(wptexturize($this -> description));
}
/**
* Receipt Page
**/
function receipt_page($order){
echo '<p>'.__('از سفارش شما متشکريم ، تا انتقال به درگاه پرداخت چند لحظه منتظر بمانيد ...', 'mrova').'</p>';
echo $this -> generate_parspal_form($order);
}
/**
* Process the payment and return the result
**/
function process_payment($order_id){
$order = &new WC_Order($order_id);
return array('result' => 'success', 'redirect' => add_query_arg('order',
$order->id, add_query_arg('key', $order->order_key, get_permalink(get_option('woocommerce_pay_page_id'))))
);
}
/**
* Check for valid parspal server callback
**/
function check_parspal_response(){
global $woocommerce;
$client = new nusoap_client('http://sandbox.parspal.com/WebService.asmx?wsdl','wsdl');
$Status = $_POST['status'];
$Refnumber = $_POST['refnumber'];
$Resnumber = $_POST['resnumber'];
$MerchantID = $this -> merchantid;
$Password = $this -> password;
$order_id = $woocommerce->session->parspal_woo_id;
$order = new WC_Order($order_id);
if($Status!=100){
$this -> msg['status'] = -1;
$this -> msg['message']= 'خطا در عمليات پرداخت ! عمليا پرداخت با موفقيت به پايان نرسيده است !';
}
if($order_id == $Resnumber && $Status == 100)
{
if($order -> status !=='completed')
{
$parameters = array("MerchantID" => $MerchantID , "Password" =>$Password , "Price" => $order->order_total,"RefNum" =>$Refnumber );
// Call the SOAP method
$result = $client->call('verifyPayment', $parameters);
// Check for a fault
$Status = $result['verifyPaymentResult']['ResultStatus'];
$PayPrice = $result['verifyPaymentResult']['PayementedPrice'];
if(strtolower($Status) == 'success')
{
unset($woocommerce->session->parspal_woo_id);
$this -> msg['status'] = 1;
$this -> msg['message'] ='پرداخت با موفقیت انجام گردید.';
$order -> payment_complete();
$order -> add_order_note('پرداخت انجام گردید<br/>شماره رسيد پرداخت: '.$Refnumber );
$order -> add_order_note($this -> msg['message']);
$woocommerce -> cart -> empty_cart();
}
else
{
$this -> msg['status'] = -88;
$this -> msg['message'] = 'خطايي در اعتبار سنجي پرداخت به وجود آمده است ! وضعيت خطا : '.$Status;
}
}
}else
{
$this -> msg['status'] = -80;
$this -> msg['message'] = 'خطا در عمليات پرداخت ! عمليات پرداخت با موفقيت به پايان نرسيده است !';
}
if ($this -> msg['status'] == 1){
$this -> msg['class']='success';
}
else
{
$order -> add_order_note($this -> msg['message']);
$this -> msg['class']='error';
}
$redirect_url = ($this -> redirect_page_id=="" || $this -> redirect_page_id==0)?get_site_url() . "/":get_permalink($this -> redirect_page_id);
//For wooCoomerce 2.0
$redirect_url = add_query_arg( array('msg'=> urlencode($this -> msg['message']), 'type'=>$this -> msg['class']), $redirect_url );
wp_redirect( $redirect_url );
exit;
}
/**
* Generate parspal button link
**/
public function generate_parspal_form($order_id){
global $woocommerce;
$order = &new WC_Order($order_id);
$redirect_url = ($this -> redirect_page_id=="" || $this -> redirect_page_id==0)?get_site_url() . "/":get_permalink($this -> redirect_page_id);
$redirect_url = add_query_arg( 'wc-api', get_class( $this ), $redirect_url );
$client = new nusoap_client('http://sandbox.parspal.com/WebService.asmx?wsdl', 'wsdl');
$MerchantID = $this -> merchantid;
$Password = $this -> password;
$orderId=rand(1, 9999999999999);
unset($woocommerce->session->parspal_woo_id);
$woocommerce->session->parspal_woo_id = $order_id;
$amount = str_replace(".00", "", $order -> order_total);;
$callBackUrl = $redirect_url;
$payerId = '0';
$parameters = array("MerchantID" => $MerchantID
, "Password" =>$Password
, "Price" =>$amount
, "ReturnPath" =>$callBackUrl
, "ResNumber" =>$order_id
, "Description" => urlencode('پرداخت سفارش شماره '. $order_id)
, "Paymenter" =>urlencode($order->shipping_first_name.' '.$order->shipping_last_name)
, "Email" =>$order->billing_email
, "Mobile" =>$order->billing_phone );
// Call the SOAP method
$result = $client->call('RequestPayment', $parameters);
// Check for a fault
if ($client->fault) {
echo '<h2>Fault</h2><pre>';
print_r($result);
echo '</pre>';
}
else {
// Check for errors
$PayPath = $result['RequestPaymentResult']['PaymentPath'];
$Status = $result['RequestPaymentResult']['ResultStatus'];
if ($Status != 'Succeed') {
$this -> msg['class'] = 'error';
$this -> msg['message'] = $Status;
echo '<div class="woocommerce-error"> در اتصال به درگاه پرداخت خطايي رخ داده است ! وضعيت خطا : '.$Status.'</div>';
}
else {
// Display the result
$script="<script language='JavaScript' type='text/javascript'>
<!--
window.location='".$PayPath."';
//-->
</script>";
echo '<a class="button alt" href="'.$PayPath.'" >در صورتي که پس از 10 ثانيه به درگاه پرداخت منتقل نشديد اينجا کليک نماييد ...</>'.$script ;
}
}
if($this -> msg['class']=='error')
$order -> add_order_note($this->msg['message']);
}
private function western_to_persian($str) {
$alphabet = array (
'Û°' => '۰', 'Û±' => '۱', 'Û²' => '۲', 'Û³' => '۳', 'Û´' => '۴', 'Ûµ' => '۵', 'Û¶' => '۶', 'Û·' => '۷', 'Û¸' => '۸',
'Û¹' => '۹', 'Ø¢' => 'آ', 'ا' => 'ا', 'Ø£' => 'أ', 'Ø¥' => 'إ', 'ؤ' => 'ؤ', 'ئ' => 'ئ', 'Ø¡' => 'ء', 'ب' => 'ب',
'Ù¾' => 'پ', 'ت' => 'ت', 'Ø«' => 'ث', 'ج' => 'ج', 'Ú†' => 'چ', 'Ø*' => 'ح', 'Ø®' => 'خ', 'د' => 'د', 'Ø°' => 'ذ',
'ر' => 'ر', 'ز' => 'ز', 'Ú˜' => 'ژ', 'س' => 'س', 'Ø´' => 'ش', 'ص' => 'ص', 'ض' => 'ض', 'Ø·' => 'ط', 'ظ' => 'ظ',
'ع' => 'ع', 'غ' => 'غ', 'Ù' => 'ف', 'Ù‚' => 'ق', 'Ú©' => 'ک', 'Ú¯' => 'گ', 'Ù„' => 'ل', 'Ù…' => 'م', 'Ù†' => 'ن',
'Ùˆ' => 'و', 'Ù‡' => 'ه', 'ÛŒ' => 'ی', 'ÙŠ' => 'ي', 'Û€' => 'ۀ', 'Ø©' => 'ة', 'ÙŽ' => 'َ', 'Ù' => 'ُ', 'Ù' => 'ِ',
'Ù‘' => 'ّ', 'Ù‹' => 'ً', 'ÙŒ' => 'ٌ', 'Ù' => 'ٍ', 'ØŒ' => '،', 'Ø›' => '؛', ',' => ',', 'ØŸ' => '؟'
);
foreach($alphabet as $western => $fa)
$str = str_replace($western, $fa, $str);
return $str;
}
// get all pages
function get_pages($title = false, $indent = true) {
$wp_pages = get_pages('sort_column=menu_order');
$page_list = array();
if ($title) $page_list[] = $title;
foreach ($wp_pages as $page) {
$prefix = '';
// show indented child pages?
if ($indent) {
$has_parent = $page->post_parent;
while($has_parent) {
$prefix .= ' - ';
$next_page = get_page($has_parent);
$has_parent = $next_page->post_parent;
}
}
// add to page list array array
$page_list[$page->ID] = $prefix . $page->post_title;
}
return $page_list;
}
}
/**
* Add the Gateway to WooCommerce
**/
function woocommerce_add_parspal_gateway($methods) {
$methods[] = 'WC_Parspal_Pay';
return $methods;
}
add_filter('woocommerce_payment_gateways', 'woocommerce_add_parspal_gateway' );
}
?>