Home URL not redirecting in ajax after getting response
Reply: 0

URL not redirecting in ajax after getting response

hash_code
1#
hash_code Published in 2018-01-12 18:47:20Z

I'm stuck after getting a response from server URL not redirecting to Paypal.I'm getting URL in response from the server, but didn't know why its unable to redirect.I have tried many solution for this like window.location.assign(obj.success); and window.location.replace(obj.success); its working fine in console response shows the URL but not redirecting.

Here is my function

// FUNCTION START HERE
function my_action() {
global $paypal;
// echo plugins_url( 'includes/pay/pay.php?success=true', __FILE__ ); die;
$postData = $_POST;

if(isset($postData['action'])){
    if($postData['action'] == "my_action"){
        $fname =    isset($postData['name'])?$postData['name']:'';
        $email =    isset($postData['email'])?$postData['email']:'';
        $idUrl =    isset($postData['url'])?$postData['url']:'';
        $message =  isset($postData['message'])?$postData['message']:'';
        $package_type = isset($postData['package_type'])?$postData['package_type']:'';
        // 

        $payer = new Payer();
        $payer->setPaymentMethod('paypal');

        $packages = array('basic' => 79, 'pro' => 175, 'ultra' => 449, 'perDay' => 0.99, 'perWeek' => 16.99, 'perMonth' => 249);
        $price = $packages[$package_type];


        // initiate the item
        $item = new Item();    
        $item->setName($package_type)->setCurrency('USD')->setQuantity(1)->setPrice($price);

        //PACKAGES LIST
        $itemList = new ItemList();
        $itemList->setItems([$item]);


        $shipping = 0.0;
        $total = $price + $shipping;

        $details = new Details();
        $details->setShipping($shipping)->setSubTotal($total);

        $amount = new Amount();
        $amount->setCurrency('USD')->setTotal($total)->setDetails($details);


        $transaction = new Transaction();
        $transaction->setAmount($amount)->setItemList($itemList)->setDescription('SSM '.ucfirst($package_type).' Payment Plan')->setInvoiceNumber(uniqid());

        $redirectUrls = new RedirectUrls();

        $redirectUrls->setReturnUrl(plugins_url( 'includes/pay/pay.php?success=true', __FILE__ ))->setCancelUrl(plugins_url( 'includes/pay/pay.php?success=false', __FILE__ ));




         $payment = new Payment();
         $payment->setIntent('sale')->setPayer($payer)->setRedirectUrls($redirectUrls)->setTransactions([$transaction]);

        try {
            $payment->create($paypal);
        } catch (Exception $e) {
            print_r($e);
            echo $e->getCode(); // Prints the Error Code
            echo $e->getData();
            die();
        }

        $aprovalUrl = $payment->getApprovalLink();
        $return = array('success'=> 1, 'paypalurl'=> $aprovalUrl); 
        echo json_encode($return);
        exit;
    }
}

  } //FUNCTION ENDS HERE

and here is my js code

jQuery(document).ready(function($){
$('#queryForm').submit(function(){
var data = $(this).serialize();
data = data+"&action=my_action";

jQuery.ajax({
type: "post",
dataType: "json",
url: ajaxurl,
data: data,
success: function(responce){

var obj = JSON.parse(responce);

if(obj.success == 1)
{ 
    alert('success');
  // window.location.href = obj.paypalurl;

}else{

  alert(responce + 'error');

}

}


}); // AJAX ENDS HERE
return false;

}); // SUBMIT ENDS HERE

}); // JQUERY ENDS HERE
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.34245 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO