【问题标题】:How set a valid publishable key with Ajax ? - for STRIPE如何使用 Ajax 设置有效的可发布密钥? - 用于条纹
【发布时间】:2019-11-19 12:40:52
【问题描述】:

我在结帐时使用stripe,但是当我想付款时返回此错误

您没有设置有效的可发布密钥。使用您的可发布密钥调用 Stripe.setPublishableKey()。

我有 3 个文件(config.php - stripeIPN.phphtml 按钮在哪里)


config.php

require_once "stripe-php-master/init.php";

$stripeDetails = array(
    "secretKey" =>  "sk_test_xxx",
    "publishableKey" => "pk_test_xxx"
);

\Stripe\Stripe::setApiKey($stripeDetails['secretKey']);

$pubkey = json_encode($stripeDetails['publishableKey']); 
return $pubkey; 

stripeIPN.php

require_once "config.php";

\Stripe\Stripe::setVerifySslCerts(false);
$token = $_POST['stripeToken'];

$charge = \Stripe\Charge::create(array(
   "amount" => 100,
    "currency" => "eur",
    "description" => "",
    "source" => $token
));

html

<script src="https://js.stripe.com/v3/"></script>

    <div id="button-stripe-vab">
        <form action="stripeIPN.php" method="POST">
            <script id="stripe-link"
                src="https://checkout.stripe.com/checkout.js" class="stripe-button" 
                data-key=""
                data-amount=""
                data-name=""
                data-description=""
                data-image="image.png"
                data-locale="auto"
                data-label="Pay with card">
            </script>
        </form>
    </div>  

data-key 我应该添加我的可发布密钥,实际上,如果我写的话,付款就可以了。 但我想用`Ajax添加它:

$.ajax({    
    type: "POST",   
    url: "config.php",  
    dataType:"json",    
    success: function (result) { //se funziona
        var pubkey = JSON.parse(JSON.stringify(result));    
        $('#stripe-link').attr("data-key",pubkey);              
    }
});

我知道我的代码很好,因为 ajax 可以工作并且使用 firebug 我知道 ajax 之后的 data-key 有我的 可发布密钥 但是为什么条带按钮返回错误? 我有一个疑问:也许条纹按钮(所以是 DOM)是在执行 ajax 之前创建的?

非常感谢

【问题讨论】:

  • 您在成功响应中使用了 data-key。你的目标是什么?什么时候用?
  • @ripa 我不太擅长 ajax...你说我不应该在成功响应中使用数据键?我想用 ajax 从 config.php 推断数据键
  • @ripa ajax 在就绪事件中
  • 好的 - 明白了。检查我的答案。我在发帖
  • @ripa 谢谢我在等

标签: php jquery ajax stripe-payments payment-gateway


【解决方案1】:

试试下面:-

var pubKey = "Your_key_here";

 $.ajax({    
   type: "POST",   
   url: "config.php",  
   dataType:"json", 
   data: { data_key: pubKey }   
   success: function (result) { //se funziona
      var pubkey = JSON.parse(JSON.stringify(result));    

   }
});

【讨论】:

  • 我认为您写的内容没有意义....如果我必须将我的公钥带回 .js 文件中,那么我应该直接将其写在 html 中。我不认为你理解我的问题......
  • 这是唯一的格式 - 如何在 ajax 文件中传递值。你可以谷歌它。如果你不想要这种方式,那么你必须考虑其他方式。不是阿贾克斯。尝试会话
猜你喜欢
  • 2020-10-12
  • 2017-03-01
  • 2021-09-22
  • 2020-05-06
  • 2018-10-18
  • 1970-01-01
  • 2018-12-17
  • 1970-01-01
  • 2016-01-01
相关资源
最近更新 更多