php - 在 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>
解决方案
您可以在此处找到描述此迁移的文档:https ://stripe.com/docs/payments/checkout/migration
推荐阅读
- bixby - 不确定如何使用新信息更新结构
- javascript - 在上传之前预览多个图像
- animation - 约束动画未按预期动画
- sql - 如何在不破坏 SQL 逻辑的情况下将 JOINS 转换为子查询
- python - 避免在大型数据集上使用智能点/matmul 进行迭代
- python-3.x - 如何修复'RuntimeError: Locator ...超过 Locator.MAXTICKS - matplotlib'
- mongodb - Mongodb聚合推送文档数组字段与子文档数组字段以嵌套方式
- python - 如何使用 Pulp Gekko 和 Scipy 包修复具有不同结果的代码以解决线性优化问题?
- background - 如何逐渐模糊背景?
- python - 如何在 Python 中获取多个字符输入并对其进行处理