付款成功后如何将数据插入数据库

时间:2018-12-04 06:57:34

标签: php mysql payment-gateway

成功付款后,我想将表单数据保存到数据库中。我在付款网关代码之间插入了插入查询,当付款失败时,它也存储数据。我正在使用payumoney网关。这是我的代码,请为我提出解决方案。

<?php

$MERCHANT_KEY = "";
$SALT = "";
// Merchant Key and Salt as provided by Payu.
//$PAYU_BASE_URL = "https://sandboxsecure.payu.in";     // For Sandbox Mode
$PAYU_BASE_URL = "https://secure.payu.in";          // For Production Mode

$action = '';

$posted = array();
if(!empty($_POST)) {
    //print_r($_POST);
  foreach($_POST as $key => $value) {    
    $posted[$key] = $value; 

  }
}
$formError = 0;

if(empty($posted['txnid'])) {
  // Generate random transaction id
  $txnid = substr(hash('sha256', mt_rand() . microtime()), 0, 20);
} else {
  $txnid = $posted['txnid'];
}
$hash = '';
// Hash Sequence
$hashSequence = "key|txnid|amount|productinfo|firstname|email|udf1|udf2|udf3|udf4|udf5|udf6|udf7|udf8|udf9|udf10";
if(empty($posted['hash']) && sizeof($posted) > 0) {
  if(
          empty($posted['key'])
          || empty($posted['txnid'])
          || empty($posted['amount'])
          || empty($posted['firstname'])
          || empty($posted['email'])
          || empty($posted['phone'])
          || empty($posted['productinfo'])
          || empty($posted['surl'])
          || empty($posted['furl'])
          || empty($posted['service_provider'])
  ) {
    $formError = 1;
  } else {
    //$posted['productinfo'] = json_encode(json_decode('[{"name":"tutionfee","description":"","value":"500","isRequired":"false"},{"name":"developmentfee","description":"monthly tution fee","value":"1500","isRequired":"false"}]'));
    $hashVarsSeq = explode('|', $hashSequence);
    $hash_string = '';  
    foreach($hashVarsSeq as $hash_var) {
      $hash_string .= isset($posted[$hash_var]) ? $posted[$hash_var] : '';
      $hash_string .= '|';
    }
    $hash_string .= $SALT;
    $hash = strtolower(hash('sha512', $hash_string));
    $action = $PAYU_BASE_URL . '/_payment';
    date_default_timezone_set('Asia/Kolkata');
    $timestamp = date("Y-m-d h:i:s");  
    $conn = mysqli_connect("localhost","root","","hoysalat_temple");

    $result= mysqli_query($conn,"INSERT INTO temp_donor_list(`donor_name`, `email`, `donor_no`, `amount`, `date_time`,`cuid`,`web_user_id`) VALUES ('".$posted['firstname']."', '".$posted['email']."', '".$posted['phone']."', '".$posted['amount']."','".$timestamp."','".$uniqeid."','".$uniqeid."')"); 
  }
} elseif(!empty($posted['hash'])) { 
  $hash = $posted['hash'];
  $action = $PAYU_BASE_URL . '/_payment';
}
?>

1 个答案:

答案 0 :(得分:0)

您应该在下面提及成功或失败的URL名称文件:

<input name="surl" value="Success URL Example: success.php" />
<input name="furl" value="Failure URL Example: failure.php" />

创建sucess.php文件,并将查询内容写入success.php文件中。创建failure.php文件并根据您的要求编写消息。

$result= mysqli_query($conn,"INSERT INTO temp_donor_list(`donor_name`, `email`, `donor_no`, `amount`, `date_time`,`cuid`,`web_user_id`) VALUES ('".$posted['firstname']."', '".$posted['email']."', '".$posted['phone']."', '".$posted['amount']."','".$timestamp."','".$uniqeid."','".$uniqeid."')");