搜索 PHP 示例新条带“结帐"集成条纹-php [英] Search for PHP example new stripe "checkout" integration stripe-php

查看:21
本文介绍了搜索 PHP 示例新条带“结帐"集成条纹-php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要从旧的条带结帐迁移到新的结帐,我必须对代码进行哪些更改?我对他们的措辞感到困惑.我发现的大多数例子都是旧的(2015-1016 ......而且是旧方式")由于 SCA,Stripe 希望我升级到新的结帐方式

这是我的工作条带结帐,我有一个按钮可以打开结帐框

然后我在下一步给卡充值

 Stripe::setApiKey($params['private_live_key']);$pubkey = $params['public_live_key'];尝试 {$charge = Stripe_Charge::create(array(金额"=>$amount_cents,货币" =>$_SESSION['cc_currency'],来源"=>$_SESSION['stripeToken'],说明"=>$描述,"expand" =>array("balance_transaction")));

如果没有抛出错误,我会将客户转发到他的下载页面.

我想要一种非常简单的方式,我不需要客户、账单、定期付款或其他任何东西......只需要单次付款.我不希望客户地址之类的东西.付款和再见...

Stripe 说我必须改变这个过程.但他们的例子让我感到困惑:https://stripe.com/docs/payments/checkout/migration#api-products(例如,我从未创建过客户……我为什么要创建?)

有人可以告诉我要迁移到新的结帐版本需要做什么吗?

解决方案

基本设置(您可以从这里构建)

后端:

更新您的 Stripe PHP 库.

按照以下格式从 \Stripe\Charge 更改为 \Stripe\PaymentIntent:

$charge = \Stripe\Charge::create(['来源' =>$token_id,'金额' =>金额,'货币' =>'美元',]);

<小时>

$intent = \Stripe\PaymentIntent::create(['payment_method_data' =>['类型' =>'卡片','卡' =>['令牌' =>$token_id],],'金额' =>金额,'货币' =>'美元','confirmation_method' =>'手动的','确认' =>真的,]);

<小时>

前端:

更新您的 Stripe JS 以使用 v3.

<script src='https://js.stripe.com/v3/' type='text/javascript'></script>

更新处理您的付款表单的 JS 代码:

document.addEventListener("DOMContentLoaded", function(event) {var stripe = Stripe('xxxxxxxxxx');//测试可发布的 API 密钥var 元素 = stripe.elements();var card = elements.create('card');//将卡片 UI 组件的实例添加到 `card-element` 

中card.mount('#card-element');//处理事件和错误card.addEventListener('change', function(event) {var displayError = document.getElementById('card-errors');如果(事件.错误){displayError.textContent = event.error.message;} 别的 {displayError.textContent = '';}});函数stripeTokenHandler(令牌){//将令牌 ID 插入到表单中,以便将其提交给服务器var form = document.getElementById('payment-form');var hiddenInput = document.createElement('input');hiddenInput.setAttribute('type', '隐藏');hiddenInput.setAttribute('name', 'stripeToken');hiddenInput.setAttribute('value', token.id);form.appendChild(hiddenInput);//提交表单表单提交();}函数 createToken() {stripe.createToken(card).then(function(result) {如果(结果.错误){//如果有错误通知用户var errorElement = document.getElementById('card-errors');errorElement.textContent = result.error.message;} 别的 {//将令牌发送到您的服务器stripeTokenHandler(result.token);}});};//在提交表单时创建一个令牌.var form = document.getElementById('payment-form');form.addEventListener('submit', function(e) {e.preventDefault();创建令牌();});});

编辑您的 HTML 表单:

 
<div class="form-row"><label for="card-element">信用卡或借记卡<div id="card-element"><!-- 您的表单放在这里 --></div>

<!-- 用于显示表单错误--><div id="card-errors" role="alert"></div>

<button type="submit">Pay</button></表单>

What do I have to change in my code to immigrate from legacy stripe checkout to the new checkout?? I am confused with their wording. And most examples I find are old (2015-1016...and are the "old way") Stripe wants me to upgrade to new checkout because of SCA

This is my working stripe checkout, I have a button that opens the checkout box

<script>
var handler = StripeCheckout.configure({
  key: '<? echo $stripe_p_key;?>',
  image: 'https://stripe.com/img/documentation/checkout/marketplace.png',
  locale: 'auto',
  token: function(token) {
    var $form = $('#f2');
    var token = token.id;
    showloader('loaderid');
        $form.prepend($('<input type="hidden" style="display:none" name="stripeToken">').val(token));
        $form.prepend($('<input type="hidden" style="display:none" name="cc_currency">').val('<? echo $dialog_waehrung_kreditkarte;?>'));
      $form.get(0).submit();  

  }
});

document.getElementById('customButton').addEventListener('click', function(e) {
  // Open Checkout with further options:
  handler.open({
    name: '',
    description: '<? echo $dialog_titel;?>',
    zipCode: true,
    currency: '<? echo $dialog_waehrung_kreditkarte;?>',
    email: '<? echo $dialog_email_kreditkarte;?>',
    amount: <? echo $dialog_preis_kreditkarte;?>
  });
  e.preventDefault();
});

// Close Checkout on page navigation:
window.addEventListener('popstate', function() {
  handler.close();
});
</script>

then I charge the card in the next step

    Stripe::setApiKey($params['private_live_key']);
    $pubkey = $params['public_live_key'];
    try {
        $charge = Stripe_Charge::create(array(       
              "amount" => $amount_cents,
              "currency" => $_SESSION['cc_currency'],
              "source" => $_SESSION['stripeToken'],
              "description" => $description,
        "expand" =>array("balance_transaction")
        )             
        );

If no error is thrown I forward the customer to his download page.

I want a very simple way, I do not need customers, bills, recruing payments or whatever..just single payments. I do not want customers address or such things. Payment and goodbye...

Stripe says I have to change this process. But their example is confusing for me: https://stripe.com/docs/payments/checkout/migration#api-products (I did never create a customer for exampley...why should I?)

Can someone tell me what I have to do to migrate to the new checkout version?

解决方案

Basic setup (you can build it up from here)

Back-end:

Update your Stripe PHP Library.

Change from \Stripe\Charge to \Stripe\PaymentIntent following this format:

$charge = \Stripe\Charge::create([
    'source' => $token_id,
    'amount' => $amount,
    'currency' => 'usd',
]);


$intent = \Stripe\PaymentIntent::create([
        'payment_method_data' => [
            'type' => 'card',
            'card' => ['token' => $token_id],
        ],
        'amount' => $amount,
        'currency' => 'usd',
        'confirmation_method' => 'manual',
        'confirm' => true,
    ]);


Front-end:

Update your Stripe JS to use v3.

<script src='https://js.stripe.com/v3/' type='text/javascript'></script>

Update JS code that handles your payment form:

document.addEventListener("DOMContentLoaded", function(event) {
    var stripe = Stripe('xxxxxxxxxx'); // test publishable API key
    var elements = stripe.elements();

    var card = elements.create('card');
    // Add an instance of the card UI component into the `card-element` <div>
    card.mount('#card-element');

    // Handle events and errors
    card.addEventListener('change', function(event) {
      var displayError = document.getElementById('card-errors');
      if (event.error) {
        displayError.textContent = event.error.message;
      } else {
        displayError.textContent = '';
      }
    });

    function stripeTokenHandler(token) {
      // Insert the token ID into the form so it gets submitted to the server
      var form = document.getElementById('payment-form');
      var hiddenInput = document.createElement('input');
      hiddenInput.setAttribute('type', 'hidden');
      hiddenInput.setAttribute('name', 'stripeToken');
      hiddenInput.setAttribute('value', token.id);
      form.appendChild(hiddenInput);

      // Submit the form
      form.submit();
    }

    function createToken() {
      stripe.createToken(card).then(function(result) {
        if (result.error) {
          // Inform the user if there was an error
          var errorElement = document.getElementById('card-errors');
          errorElement.textContent = result.error.message;
        } else {
          // Send the token to your server
          stripeTokenHandler(result.token);
        }
      });
    };

    // Create a token when the form is submitted.
    var form = document.getElementById('payment-form');
    form.addEventListener('submit', function(e) {
      e.preventDefault();
      createToken();
    });
});

Edit your HTML form:

    <form action="process_payment.php" method="post" id="payment-form">
          <div class="form-row">
            <label for="card-element">
              Credit or debit card
            </label>
            <div id="card-element"><!-- Your form goes here --></div>                  
            </div>        
            <!-- Used to display form errors -->
            <div id="card-errors" role="alert"></div>
          </div>
        <button type="submit">Pay</button>
    </form>

这篇关于搜索 PHP 示例新条带“结帐"集成条纹-php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆