ryuske/wepay-laravel

适用于Laravel 4的WePay API包装器

dev-master 2015-08-31 20:35 UTC

This package is not auto-updated.

Last update: 2020-01-10 15:03:49 UTC


README

此包装器允许您使用标准Facade结构在Laravel中使用WePay API。如需有关wepay的更多信息,请访问https://www.wepay.com/

安装

首先,您需要编辑您的composer.json文件,如下所示

"require-dev": {
    "ryuske/wepay-laravel": "dev-master",
    "wepay/PHP-SDK": "*"
}

然后运行composer install

完成之后,您需要编辑app/config/app.php配置文件的2个部分。第一个部分是提供者数组,如下所示

'providers' => array(
    // ...

    'Ryuske\WepayLaravel\WepayLaravelServiceProvider'
)

第二个部分在别名数组中,如下所示

'aliases' => array(
    // ...

    'WepayWrapper'    => 'Ryuske\WepayLaravel\Facades\WepayLaravel'
)

您已经完成了包装器的安装!太棒了!但是...现在我们还需要进行配置。

配置

首先,运行命令php artisan config:publish ryuske/wepay-laravel

运行该命令后,打开文件app/config/packages/ryuske/wepay-laravel/config.php

现在,您主要需要更改此文件中client_idclient_secretaccess_tokenaccount_id的生产值。

如果您尚未设置开发环境,则设置useStaging = false,否则,也要填写测试数组的值。

设置这些配置值后,您就可以开始使用了。

代码示例

// This is the route where you would POST a form to
Route::post('/checkout', function() {
    $reference_id = 89678578; // This is some randomly generate ID for your records
    
    // On most API calls, the first parameter needs to be the access token of the person relieving money, the 2nd is the endpoint as per the WePay API docs and lastly the parameters for the API call as per the docs. 
    $response = WepayWrapper::request($user->wepay_token, '/credit_card/create', [
        'client_id' 		=> WepayWrapper::get('client_id'),
        'cc_number' 		=> Input::get('cc_number'),
        'cvv' 				=> Input::get('cvv'),
        'expiration_month' 	=> Input::get('expiration_month'),
        'expiration_year' 	=> Input::get('expiration_year'),
        'user_name' 		=> Input::get('name'),
        'email' 			=> Input::get('email'),
        'address' 			=> [
            'zip' 		=> Input::get('zip'),
            'country' 	=> Input::get('country')
        ]
    ]);
    
    WepayWrapper::request($user->wepay_token, '/credit_card/authorize', [
        'client_id' 		=> WepayWrapper::get('client_id'),
        'client_secret'		=> WepayWrapper::get('client_secret'),
        'credit_card_id'	=> $response->credit_card_id
    ]);
            
    WepayWrapper::request($user->wepay_token, '/checkout/create', [
        'account_id' 			=> $user->wepay_id, // This is the account id for whoever is recieving money
        'amount' 				=> Input::get('amount'),
        'currency' 				=> 'USD',
        'short_description' 	=> '<sku here>',
        'type'					=> 'GOODS',
        'long_description'		=> 'Purchase for <item here>, <sku here>',
        'reference_id'			=> $reference_id,
        'fallback_uri' 			=> action('CheckoutController@error'),
        'redirect_uri' 			=> action('ProductsController@show', ['id' => '<sku number here>']),
        'funding_sources' 		=> 'cc',
        'payment_method_id' 	=> $response->credit_card_id,
        'payment_method_type' 	=> 'credit_card'
    ]);
    
    $response = WepayWrapper::request($user->wepay_token, '/checkout/find', [
        'account_id' 			=> $user->wepay_id, // Again, this is the account id for whoever recieved the money
        'reference_id'			=> $reference_id,
    ]);
    
    // Here you would use the response to store values into the database or however you want to keep track of money that has been processed.			
    return $response;
});