如何使用Laravel进行API调用

时间:2018-01-15 11:18:59

标签: php laravel api token

我使用Laravel 5.1框架创建了一个应用程序。我也用信用卡收费使用Checkout.com和这个库https://github.com/checkout/checkout-php-library

在前面,我有验证信用卡的代码并向服务器发送请求:

<script src="https://cdn.checkout.com/js/frames.js"></script>
  <form id="payment-form" method="POST" action="{{url()}}/checkout">
  {!! csrf_field() !!}

    <div class="frames-container">
      <!-- form will be added here -->
    </div>
    <!-- add submit button -->
    <button id="pay-now-button" type="submit" disabled>Pay now</button>
  </form>

    <script>
    var paymentForm = document.getElementById('payment-form');
    var payNowButton = document.getElementById('pay-now-button');

    Frames.init({
      publicKey: 'pk_test_aaaaaaa-30af-41d9-bf58-99999999',
      containerSelector: '.frames-container',
      customerName: 'John Smith',
      billingDetails: {
        addressLine1: '623 Slade Street',
        addressLine2: 'Apartment 8',
        postcode: '31313',
        email: 'asd@asd.asd',
        country: 'US',
        city: 'Hinesville',
        phone: { number: '9125084652' }
      },
      cardValidationChanged: function () {
        // if all fields contain valid information, the Pay now
        // button will be enabled and the form can be submitted
        payNowButton.disabled = !Frames.isCardValid();
      },
      cardSubmitted: function () {
        payNowButton.disabled = true;
        // display loader
      }
    });
    paymentForm.addEventListener('submit', function (event) {
      event.preventDefault();
      Frames.submitCard()
        .then(function (data) {
          Frames.addCardToken(paymentForm, data.cardToken);
          paymentForm.submit();
        })
        .catch(function (err) {
          // catch the error
        });
    });
  </script>

现在在Controller我创建一个函数checkout:

 use com\checkout;
    class OrdersController extends Controller
    {

    public function payment() {

      return view('front.checkout');

    }

    public function checkout(Request $request) {

      $data = $request->all();


    $apiClient = new checkout\ApiClient('sk_test_baaaaaa-5116-999-9270-8d404615c8cd');
    // create a charge serive
    $charge = $apiClient->chargeService();

    try {
        /**  @var ResponseModels\Charge  $ChargeRespons **/
        $ChargeResponse = $charge->verifyCharge($data['cko-card-token']);

    } catch (com\checkout\helpers\ApiHttpClientCustomException $e) {
        echo 'Caught exception Message: ',  $e->getErrorMessage(), "\n";
        echo 'Caught exception Error Code: ',  $e->getErrorCode(), "\n";
        echo 'Caught exception Event id: ',  $e->getEventId(), "\n";
    }
[![enter image description here][1]][1]

}

当我提交带有测试卡的表格时,我得到了这个屏幕:

enter image description here

这是什么问题?为什么我收到此错误?请帮帮我...

0 个答案:

没有答案