首页 > 解决方案 > 在 codeigniter 中从 Legacy stripe checkout 迁移到新版本

问题描述

我正在做一个将作为市场网站工作的项目。我已经有条库作为初始付款方式,将从网站接收客户的付款,但似乎旧版本的条带已经升级,我想采用新版本。

public function stripe_payment_post()
{
    require_once(APPPATH . 'third_party/stripe/vendor/autoload.php');
    try {
        $token = $this->input->post('payment_id', true);
        $email = $this->input->post('email', true);
        $payment_amount = $this->input->post('payment_amount', true);
        $currency = $this->input->post('currency', true);
        //Init stripe
        $stripe = array(
            "secret_key" => $this->payment_settings->stripe_secret_key,
            "publishable_key" => $this->payment_settings->stripe_publishable_key,
        );
        
        \Stripe\Stripe::setApiKey($stripe['secret_key']);
        //customer
        $customer = \Stripe\Customer::create(array(
            'email' => $email,
            'source' => $token
        ));
        //charges
        $charge = \Stripe\Charge::create(array(
            'customer' => $customer->id,
            'amount' => $payment_amount,
            'currency' => $currency,
            'description' => trans("stripe_checkout")
        ));

        //add to database
        $data_transaction = array(
            'payment_method' => "Stripe",
            'payment_id' => $token,
            'currency' => $currency,
            'payment_amount' => get_price($payment_amount, 'decimal'),
            'payment_status' => $this->input->post('payment_status', true),
        );

    } catch (\Stripe\Error\Base $e) {
        $this->session->set_flashdata('error', $e->getMessage());
        $data = array(
            'status' => 0,
            'redirect' => generate_url("cart", "payment")
        );
        echo json_encode($data);
    } catch (Exception $e) {
        $this->session->set_flashdata('error', $e->getMessage());
        $data = array(
            'status' => 0,
            'redirect' => generate_url("cart", "payment")
        );
        echo json_encode($data);
    }
}

// 这里是客户端

<script src="https://checkout.stripe.com/v2/checkout.js"></script>
<script>
    var handler = StripeCheckout.configure({
        key: '<?php echo $this->payment_settings->stripe_publishable_key; ?>',
        image: 'https://stripe.com/img/documentation/checkout/marketplace.png',
        locale: 'auto',
        currency: '<?php echo $currency; ?>',
        token: function (token) {
            var data = {
                'payment_id': token.id,
                'email': token.email,
                'currency': '<?php echo $currency; ?>',
                'payment_amount': '<?php echo $total_amount; ?>',
                'payment_status': 'success',
               
                'sys_lang_id': sys_lang_id
            };
            data[csfr_token_name] = $.cookie(csfr_cookie_name);
            $.ajax({
                type: "POST",
                url: base_url + "stripe-payment-post",
                data: data,
                success: function (response) {
                    var obj = JSON.parse(response);
                    if (obj.result == 1) {
                        window.location.href = obj.redirect;
                    } else {
                        location.reload();
                    }
                }
            });
        }
    });
    document.getElementById('btn_stripe_checkout').addEventListener('click', function (e) {
        handler.open({
            name: '<?php echo html_escape($this->general_settings->application_name); ?>',
            description: '<?php echo trans("stripe_checkout"); ?>',
            amount: '<?php echo $total_amount; ?>'
        });
        e.preventDefault();
    });
    // Close Checkout on page navigation:
    window.addEventListener('popstate', function () {
        handler.close();
    });
</script>

标签: phpjquerystripe-paymentscodeigniter-3

解决方案


您可以在此处找到描述此迁移的文档:https ://stripe.com/docs/payments/checkout/migration


推荐阅读