Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm try to implement the paypal express checkout.

(我正在尝试实施Paypal Express结帐。)

So I implement the button and write the result in my database.

(因此,我实现了按钮并将结果写入我的数据库。)

Nothing exceptional, it's the script they gave.

(没什么特别的,这是他们给的脚本。)

<script>
paypal.Buttons({
    createOrder: function(data, actions) {
      return actions.order.create({
        purchase_units: [{
          amount: {
            value: '0.01'
          }
        }]
      });
    },
    onApprove: function(data, actions) {
      return actions.order.capture().then(function(details) {
        // Call your server to save the transaction
        return fetch('recordDatabase.php', {
          method: 'post',
          mode: "same-origin",
          credentials: "same-origin",
          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
          })
        })
      });
    }
  }).render('#paypal-button-container');
</script>

The php to record in the database:

(在数据库中记录的php:)

<?php
$link = connect();

$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'];
        $time = $decoded['time'];
        $id = $decoded['orderID'];
        $stat = $decoded['status'];
        $pays = $decoded['pays'];
        $val = $decoded['valeur'];

        $secQuery = "INSERT INTO myDatabase(PSEUDO,PASSWORD,CONNECTION,INSCRIPTION,ANNIVERSAIRE,MAIL,IDPAYPAL,STATPAYPAL,NOMPAYER,PAYS,VALEUR) VALUES ('essai','123456',0,'$date','$time','email@mail','$id','$stat','$name','$pays','$val') ";
        if (mysqli_query($link,$secQuery)) {
            //echo "ok";
        } else {
            //echo "error";
        }

    }
} else {
    //echo "error";
}

So, the record in my database works fine, but my question is:

(因此,数据库中的记录工作正常,但我的问题是:)

How can I retrieve the echo error or ok in the javascript to confirm the user that everything is fine, or if an error happen.

(如何在JavaScript中检索echo错误或ok,以确认用户一切正常,或者发生错误。)

I tried another solution, to redirect the user from the php and add to the php:

(我尝试了另一种解决方案,将用户从php重定向并添加到php:)

header("Location: confirmation web page"); or

(要么)


echo "<script>window.location = 'confirmation web page'</script>";

but both solution doesn't work.

(但是两种解决方案都行不通。)

No redirection happen

(没有重定向发生)

  ask by laurent emin translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
279 views
Welcome To Ask or Share your Answers For Others

1 Answer

Correct if i'm wrong, recordDatabase.php is your php file that is storing the transactions.

(如果我错了,请更正, recordDatabase.php是存储事务的PHP文件。)

So, the return fetch('recordDatabase.php', { is returning the response from this file, your echo 'ok'; , echo 'error'; , the fetch is asyncronous, so it'will return a promise.

(因此, return fetch('recordDatabase.php', {正在恢复从该文件的响应,您的echo 'ok'; echo 'error';抓取是asyncronous,所以it'will回报的承诺。)

Add header('Content-Type: application/json');

(添加header('Content-Type: application/json');)

to your php file so it returns a json response.

(到您的php文件,以便它返回json响应。)

Also change your echo to echo '{"status":"ok"}';

(同样,将回声更改为echo '{"status":"ok"}';)

and echo '{"status":"error"}';

(并echo '{"status":"error"}';)

Now modify your fetch function,

(现在修改您的提取功能,)

return fetch('recordDatabase.php', {
     //same info here
})
.then((response) => response.json())
.then((responseData) => {
      if(responseData.status == "ok"){
           alert("it worked");
      }else{
           alert("it didn't work");
      }
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...