如何在 Laravel 5.1 使用非 laravel 包
How to use non-laravel package at Laravel 5.1
我用 Laravel 5.1 我们用 Stripe 但现在我需要换成 checkout.com
Checkout.com 有一个 php 图书馆:https://github.com/checkout/checkout-php-library
我想在我的应用程序中实施。首先我 运行 :
composer require checkout/checkout-php-api
所以我安装了库,库在 vendor/checkout
文件夹中
我使用 OrderController 并创建 public 功能结帐:
require_once 'vendor\checkout\checkout-php-library\autoload.php';
use com\checkout;
class OrdersController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function payment() {
return view('front.checkout');
}
public function checkout(Request $request) {
$data = $request->all();
$apiClient = new ApiClient('sk_test_aaaaaa-5116-999-9270-999999999');
// 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";
}
}
现在,当我发出 POST 请求时,我得到:
FatalErrorException in OrdersController.php line 26: main(): Failed
opening required 'vendor\checkout\checkout-php-library\autoload.php'
(include_path='.;C:\php\pear')
如何将这个库集成到我的 Laravel 项目中?
更新:
在前端我有这个代码:
<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_aaaaaaaaa-000-41d9-9999-999999999',
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>
不包括包自动加载器。
将库添加到您的项目后,包含在库的根目录中找到的文件 autoload.php。
include 'checkout-php-api/autoload.php';
不要使用 include_once
包含包。
因为当您需要在应用程序的其他部分使用它时,它会变得超级混乱。
将 API 密钥放在源代码中是个坏主意。该密钥可能会留在您的 Git 中,或者会出现在您不希望它被发现的地方。另外,假设您的密钥已更改,您将不得不去寻找源代码中使用该密钥的所有部分来替换它。
阅读有关服务提供商 https://laravel.com/docs/5.1/providers 的信息,您可以在其中为 checkout/checkout-php-api
创建一个 'wrapper',您将能够在整个 Laravel 应用程序中使用它:
<?php
use AleksPer\Checkout\CheckoutAPI;
public function checkout(Request $request)
{
$data = $request->all();
/**
* There's no need to inject the API key here,
* Assuming it is injected when the library is bootstrapped.
*
*/
$apiClient = new CheckoutAPI();
$charge = $apiClient->chargeService();
}
或者,如果 registered/bootstrapped checkout-php-api
作为单例,您可以在整个应用程序中将其引用为 app('checkoutApi');
,并让您创建的服务提供商注入所需的参数,这让我想到了我的下一点:
将您的密钥放在项目的 .env
中,例如
CHECKOUT_SECRET_KEY=sk_***************
当然,要在您的服务提供商中加载该环境变量:env('CHECKOUT_SECRET_KEY')
。
谷歌搜索一下:
- 'creating composer packages'
- 'creating laravel packages'
- 'create api wrapper laravel'
你会找到很多答案。
我用 Laravel 5.1 我们用 Stripe 但现在我需要换成 checkout.com Checkout.com 有一个 php 图书馆:https://github.com/checkout/checkout-php-library
我想在我的应用程序中实施。首先我 运行 :
composer require checkout/checkout-php-api
所以我安装了库,库在 vendor/checkout
文件夹中
我使用 OrderController 并创建 public 功能结帐:
require_once 'vendor\checkout\checkout-php-library\autoload.php';
use com\checkout;
class OrdersController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function payment() {
return view('front.checkout');
}
public function checkout(Request $request) {
$data = $request->all();
$apiClient = new ApiClient('sk_test_aaaaaa-5116-999-9270-999999999');
// 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";
}
}
现在,当我发出 POST 请求时,我得到:
FatalErrorException in OrdersController.php line 26: main(): Failed opening required 'vendor\checkout\checkout-php-library\autoload.php' (include_path='.;C:\php\pear')
如何将这个库集成到我的 Laravel 项目中?
更新: 在前端我有这个代码:
<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_aaaaaaaaa-000-41d9-9999-999999999',
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>
不包括包自动加载器。
将库添加到您的项目后,包含在库的根目录中找到的文件 autoload.php。
include 'checkout-php-api/autoload.php';
不要使用 include_once
包含包。
因为当您需要在应用程序的其他部分使用它时,它会变得超级混乱。
将 API 密钥放在源代码中是个坏主意。该密钥可能会留在您的 Git 中,或者会出现在您不希望它被发现的地方。另外,假设您的密钥已更改,您将不得不去寻找源代码中使用该密钥的所有部分来替换它。
阅读有关服务提供商 https://laravel.com/docs/5.1/providers 的信息,您可以在其中为 checkout/checkout-php-api
创建一个 'wrapper',您将能够在整个 Laravel 应用程序中使用它:
<?php
use AleksPer\Checkout\CheckoutAPI;
public function checkout(Request $request)
{
$data = $request->all();
/**
* There's no need to inject the API key here,
* Assuming it is injected when the library is bootstrapped.
*
*/
$apiClient = new CheckoutAPI();
$charge = $apiClient->chargeService();
}
或者,如果 registered/bootstrapped checkout-php-api
作为单例,您可以在整个应用程序中将其引用为 app('checkoutApi');
,并让您创建的服务提供商注入所需的参数,这让我想到了我的下一点:
将您的密钥放在项目的 .env
中,例如
CHECKOUT_SECRET_KEY=sk_***************
当然,要在您的服务提供商中加载该环境变量:env('CHECKOUT_SECRET_KEY')
。
谷歌搜索一下:
- 'creating composer packages'
- 'creating laravel packages'
- 'create api wrapper laravel'
你会找到很多答案。