如何添加自定义字段,验证这些字段,向客户付款,然后进行成功日志交易?

时间:2019-07-12 05:37:46

标签: php paypal paypal-sandbox paypal-ipn

我正在尝试这样做,但是我有些困惑。

  1. 客户在表单中输入自己喜欢的颜色
  2. 客户在表单中输入他们喜欢的号码
  3. 客户提交表格
  4. 在后端,我检查他们最喜欢的颜色是red还是blue,他们最喜欢的颜色是7
  5. 如果他们的输入正确,我会将他们重定向到他们付款的PayPal。否则,我会向他们显示错误消息。
  6. 付款后,我假设PayPal监听器将监听交易。听到交易成功后,我才想创建一个包含客户喜欢的颜色和编号的文件。

我该怎么做?我很困惑,因为当客户单击“提交”时,它将直接转到PayPal结帐。我知道表单的action指向PayPal,但是所有有关使用PayPal编写结帐代码的教程都可以做到这一点。

然后,在成功付款后,如何获得客户喜欢的颜色和编号(因为我不再在index.php上继续付款。我在listener.php上听到交易成功。我没有想存储客户喜欢的号码或颜色。我只想存储他们成功购买的商品。

index.php

<?php

function validate_input($favColor, $favNum) {
    if (($favColor == "red" || $favColor == "blue") && $favNum == 7) {
        return true;
    }

    return false;
}

?>

<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top" class="form-validate">
   <div class="field-item">
        What is your favourite color? <input name="fav-color" type="text" required><br>
        What is your favourite number? <input name="fav-num" type="text" required><br>
        <input type="hidden" name="cmd" value="_s-xclick">
        <input type="hidden" name="hosted_button_id" value="MYHOSTBUTTONVALUE">
        <input type="image" src="https://www.paypalobjects.com/webstatic/en_US/i/buttons/buy-logo-medium.png" style="margin-top: 10px;" border="0" name="submit" alt="PayPal – The safer, easier way to pay online!">
    </div>
</form>

listener.php

<?php

    if ($_SERVER['REQUEST_METHOD'] != 'POST') {
        header('Location: index.php');
        exit();
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://ipnpb.sandbox.paypal.com/cgi-bin/webscr');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "cmd=_notify-validate&" . http_build_query($_POST));
    $response = curl_exec($ch);
    curl_close($ch);


    if ($response == "VERIFIED") {
        $favColor = CUSTOMERS RESPONSE; // How do I get this?
        $favNum = CUSTOMERS RESPONSE; // How do I get this?
        $msg = "Customer likes $favColor and $favNum";
        $fileName = "purchases.txt";

        file_put_contents($fileName, $msg);
    }

?>

0 个答案:

没有答案