Hello社区请原谅我缺少详细信息,因为我是WordPress自定义的初学者我在WordPress中发布了一些自定义代码的表单通过ajax在订单表单上的所有设置完成后工作正常当用户提交表单得到零响应时我希望在PayPal上重定向它们的服务器。
这是我的功能
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);
json_encode($return);
}
}
}
这里我的js去了
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){
alert(responce);
var obj = JSON.parse(responce);
print_r(obj);
console.log(obj);
if(obj.success == 1)
{
window.location.href = obj.paypalurl;
}else{
alert(responce + 'error');
}
}
}); // AJAX ENDS HERE
return false;
}); // SUBMIT ENDS HERE
}); // JQUERY ENDS HERE
请帮帮我。谢谢