xola/omnipay-bundle

集成 Omnipay 3 与 Symfony 2+

安装次数: 17,270

依赖者: 0

建议者: 0

安全性: 0

星标: 9

关注者: 7

分支: 11

开放问题: 0

类型:symfony-bundle

v3.0 2020-01-22 18:06 UTC

This package is not auto-updated.

Last update: 2024-09-24 06:26:36 UTC


README

此扩展包将支付处理库 Omnipay 集成到 Symfony2

此扩展包支持 Omnipay 3

安装

要使用 Composer 安装,请将以下内容添加到您的 composer.json 文件中

{
    "require": {
        "xola/omnipay-bundle": "^3"
    }
}

将扩展包添加到您的应用程序内核。

// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new Xola\OmnipayBundle\OmnipayBundle(),
        // ...
    );
}

配置

(可选) 在 Omnipay 库中,您会通过编程方式设置网关所需参数。使用此扩展包,可以在您的 Symfony 配置文件中配置这些参数。

# app/config/password_dev.yml
parameters:
    # Custom gateway
    omnipay.my_custom_key.apiKey: myGatewayKey
    omnipay.my_custom_key.gateway: MyGateway

    # Default Stripe gateway
    omnipay.stripe_default.apiKey: myApiKey
    omnipay.stripe_default.gateway: Stripe

    # Gateway for Stripe Canada account
    omnipay.stripe_canada.apiKey: myStripeCanadaApiKey
    omnipay.stripe_canada.gateway: Stripe

    # Authorize.NET AIM
    omnipay.authorize_net_aim.transactionKey: myTransactionKey
    omnipay.authorize_net_aim.gateway: AuthorizeNet_AIM

在上面的示例配置中,my_custom_key 是您为每个网关定义的唯一键。 omnipay.my_custom_name.gateway 是 Omnipay 网关驱动程序的类名(例如 Stripe)。您可以为同一 Omnipay 网关定义多个键,并使用不同的凭据。在上面的配置中,我们为 Stripe 配置了两个网关定义——两者都使用 Stripe Omnipay 驱动程序,但它们各自使用不同的凭据集。

用法

使用新的 omnipay 服务创建网关对象

    // From within a controller. This will return an instance `\Omnipay\Stripe`. `stripe_default` is the key as
    // specified in the config.
    $gateway = $this->get('omnipay')->get('stripe_default');

    // From within a controller. This will return an instance of `\Omnipay\MyGateway` as specified in
    // `omnipay.my_custom_name.gateway`
    $gateway = $this->get('omnipay')->get('my_custom_name');

其余部分与您通常使用 Omnipay 的方式相同

$formData = ['number' => '4242424242424242', 'expiryMonth' => '11', 'expiryYear' => '2018', 'cvv' => '123'];
$response = $gateway->purchase(['amount' => '10.00', 'currency' => 'USD', 'card' => $formData])->send();

if ($response->isSuccessful()) {
    // payment was successful: update database
    print_r($response);
} elseif ($response->isRedirect()) {
    // redirect to offsite payment gateway
    $response->redirect();
} else {
    // payment failed: display message to customer
    echo $response->getMessage();
}

返回的网关类已经使用配置文件中定义的参数初始化。