首页 > 解决方案 > laravel - 如何在控制器中实现 Heartland 支付方式

问题描述

在我的网站上,我使用的是 Stripe,但由于某些原因决定更改为 Heartland 付款方式。由于我使用的是 laravel,所以我的知识是关于控制器和模型的,我试图了解如何使用 heartland 来做到这一点,但没有得到它。为了显示输入字段,它对我来说很好,所以现在我想通过单击提交按钮进行付款,但我不知道如何将这些输入信息发送到控制器以完成付款。请我需要很好的例子来显示控制器中的过程。我试图从此链接获取控制器内容,但没有找到明确的内容:https ://developer.heartlandpaymentsystems.com/Ecommerce/Card 提前谢谢

这里的代码:

路线:

   Route::get('/Payment/Charge', 'PaymentController@heartlandPost')->name('heartlandpost');

付款表格和jquery代码:

   <form id="payment-form" action="/Payment/Charge" method="get">
  <div id="credit-card"></div>
  </form>

   <script src="https://api2.heartlandportico.com/SecureSubmit.v1/token/gp-1.0.1/globalpayments.js"> </script>

  <script type="text/javascript">
   GlobalPayments.configure({
  publicApiKey: "pkapi_cert_*****"
   });

    // Create Form
   const cardForm = GlobalPayments.creditCard.form("#credit-card");

 cardForm.on("token-success", (resp) => {
  // add payment token to form as a hidden input
const token = document.createElement("input");
token.type = "hidden";
token.name = "payment_token";
token.value = resp.paymentReference;

  // Submit data to the integration's backend for processing
  const form = document.getElementById("payment-form");
  form.appendChild(token);
 form.submit();
});

 cardForm.on("token-error", (resp) => {
// show error to the consumer
});
</script>

控制器:

public function heartlandPost()

 {
 }

标签: phpjquerylaravel

解决方案


因此,通过阅读文档,您似乎需要执行以下操作:

<?php
use GlobalPayments\Api\ServicesConfig;
use GlobalPayments\Api\ServicesContainer;
use GlobalPayments\Api\Entities\Address;
use GlobalPayments\Api\PaymentMethods\CreditCardData;
use GlobalPayments\Api\Entities\Exceptions\ApiException;

public function heartlandPost()
{
    $config = new ServicesConfig();
    $config->secretApiKey = "skapi_cert_***";
    $config->developerId = "000000";
    $config->versionNumber = "0000";
    $config->serviceUrl = "https://cert.api2.heartlandportico.com";
    ServicesContainer::configure($config);

    $card = new CreditCardData();
    $card->token = $request->input('payment_token');
    $address = new Address();
    $address->postalCode = $request->input('postal_code');
    try {
        $response = $card->charge(10)
          ->withCurrency("USD")
          ->withAddress($address)
          ->execute();
    } catch (ApiException $e) {
        // handle error
    }
    // return your response to the client
}

您需要更新配置以匹配您自己的密钥和诸如此类的东西,而且我不确定您如何将金额/邮政编码传递到您的后端,因此请确保您也这样做。


推荐阅读