change page after payment

akille67
Contributor
Contributor

Hi, I have inserted a button to pay with paypal for the services purchased on my site.
I can enter some transaction data in a db but when he returns to the page that calls the payment the user could accidentally pay again.
How can I change the page if the payment has been successful or how can I delete the payment button if it has already been done.
Thank you

 

is my code:

 <script>
  paypal.Buttons({
    createOrder: function(data, actions) {
      return actions.order.create({
        purchase_units: [{
          amount: {
            value: $amount
          },
            description: $type,        
        }
			]
		
      });
    },

	// --------- onApprove ---
  onApprove: function(data, actions) {
      return actions.order.capture().then(function(details) {
 //       alert('Transaction completed by ' + details.payer.name.given_name+' ApplicationId <?php echo $id; ?> :payerID '+data.payerID);

        // Call your server to save the transaction
        return fetch('payments.php', {
          method: 'post',
          headers: {
            'content-type': 'application/json'
          },
          body: JSON.stringify({
              orderID: data.orderID, 
              time: details.create_time, 
              status: details.status, 
              nom: details.payer.name.given_name, 
              prenom: details.payer.name.surname, 
              pays: details.payer.address.country_code, 
              valeur:details.purchase_units[0].amount.value
          })
        }).then(data => data.ok && data.json()).then(response => {
            alert(response.status);
        });         
      });
    }
  }).render('#paypal-button-container');
</script>

payment.php

<?php
    header('Content-Type: application/json');

$date = date('Y-m-d H:i:s');
    //Receive the RAW post data.
    $contentType = isset($_SERVER["CONTENT_TYPE"]) ?trim($_SERVER["CONTENT_TYPE"]) : '';

    if ($contentType === "application/json") {
        //Receive the RAW post data.
        $content = trim(file_get_contents("php://input"));
        $decoded = json_decode($content, true);

        //If json_decode failed, the JSON is invalid.
        if(! is_array($decoded)) {
            //echo "error";
        } else {
            $name = $decoded['nom'];
			$prenom = $decoded['prenom'];
			$name=$name." ".$prenom;
            $time = $decoded['time'];
            $id = $decoded['orderID'];
            $stat = $decoded['status'];
            $pays = $decoded['pays'];
            $val = $decoded['valeur'];

			include('conn.inc');
			include('common.inc');

		
			$q=new Query("INSERT INTO paypal (`nom`, `paytime`, `orderID`, `status`, `pays`, `valeur`, `paydata`) VALUES ('$name', '$time', '$id', '$stat', '$pays', '$val', '$date')");


          echo '{"status":"Acquisto effettuato.\nRiceverai una email con tutti i dati della prenotazione"}';

        }
    } else {
        echo '{"status":"Attenzione Errore nella fase di acquisto!!!"}';
    }

    ?>
Login to Me Too
0 REPLIES 0

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.