kun391/yii2-paypal

使用Paypal进行处理

v1.0.1 2016-01-21 16:50 UTC

This package is not auto-updated.

Last update: 2024-09-14 17:32:07 UTC


README

Build Status Latest Stable Version Total Downloads Monthly Downloads Daily Downloads

需求

  • PHP >= 5.4
  • php5-curl (系统)

安装

将以下部分添加到 composer.json 文件中

php composer.phar require --prefer-dist kun391/yii2-paypal:"*"

或者

"kun391/yii2-paypal":"dev-master"

将以下部分添加到您的 Yii2 配置文件中,带有组件设置

在每个位置创建 config.php 文件用于 RESTAPI,以及 config.php 用于 Classic API

  • ClassicAPI
/**
 * Information PAYPAL's enviroments for classic API
 * @var string
 */

// E.g:
// If enviroment is Development you should use mode = sandbox and endpoint = api.sandbox.paypal.com
// [
//     'acct1.UserName'  => 'nguyentruongthanh.dn-facilitator-1_api1.gmail.com',
//     'acct1.Password'  => 'GRHYUV2DJHNBFTAA',
//     'acct1.Signature' => 'APP9kKh6roKmPNKj6yBK5oSwdD39ADujX4sfPXjr.hGf1wjRi1THwoVq',
//     'mode'            => 'sandbox',
// ];

// E.g:
// If enviroment is live you should use mode = live
// [
//     'acct1.UserName'  => 'nguyentruongthanh.dn-facilitator-1_api1.gmail.com',
//     'acct1.Password'  => 'GRHYUV2DJHNBFTAA',
//     'acct1.Signature' => 'APP9kKh6roKmPNKj6yBK5oSwdD39ADujX4sfPXjr.hGf1wjRi1THwoVq',
//     'mode'            => 'live',
// ];

return  [
    'acct1.UserName'  => 'nguyentruongthanh.dn-facilitator-1_api1.gmail.com',
    'acct1.Password'  => 'GRHYUV2DJHNBFTAA',
    'acct1.Signature' => 'APP9kKh6roKmPNKj6yBK5oSwdD39ADujX4sfPXjr.hGf1wjRi1THwoVq',
    'mode'            => 'sandbox',
];
  • RestAPI
/**
 * Information PAYPAL's enviroments
 * @var string
 */

// E.g:
// If enviroment is Development you should use mode = sandbox and endpoint = api.sandbox.paypal.com
// $setting = [
//     'endpoint'       => 'api.sandbox.paypal.com',
//     'client_id'      => 'AV92BhCOYzF4Vejrbphu1ksMn4KYSlvbzCTcbLdOMixBvAS7sQZhOvMNkMoG',
//     'secret'         => 'EDdjYm7i8w2XZwWGyTqPfPDJim2dUV1hX_3dhY0fR-HulrENli6043rY_0GO1ro1gnkxVe3bMWNDikvq',
//     'business_owner' => 'nguyentruongthanh.dn-facilitator-1@gmail.com',
// ];

// E.g:
// If enviroment is live you should use mode = live and endpoint = api.paypal.com
// $setting = [
//     'endpoint'       => 'api.paypal.com',
//     'client_id'      => 'AV92BhCOYzF4Vejrbphu1ksMn4KYSlvbzCTcbLdOMixBvAS7sQZhOvMNkMoG',
//     'secret'         => 'EDdjYm7i8w2XZwWGyTqPfPDJim2dUV1hX_3dhY0fR-HulrENli6043rY_0GO1ro1gnkxVe3bMWNDikvq',
//     'business_owner' => 'nguyentruongthanh.dn-facilitator-1@gmail.com',
// ];

$setting = [
    'endpoint'       => 'api.sandbox.paypal.com',
    'client_id'      => 'AX9sEz0g3cCzD_heoGyedx7LKSuEx1Lx7H8aGXIrzQmDhqV-V5bV0sbVFc195mNKbE81OkAPZZi_7dfa',
    'secret'         => 'EDdjYm7i8w2XZwWGyTqPfPDJim2dUV1hX_3dhY0fR-HulrENli6043rY_0GO1ro1gnkxVe3bMWNDikvq',
    'business_owner' => 'nguyentruongthanh.dn-facilitator-1@gmail.com',
];

return \yii\helpers\ArrayHelper::merge(['config' => [
        'mode'                   => 'sandbox',
        'http.ConnectionTimeOut' => 60,
        'log.LogEnabled'         => false,
        'log.FileName'           => '@api/runtime/PayPal.log',
        'log.LogLevel'           => 'FINE',
    ]
], $setting);

将以下部分添加到 main.php 文件的组件中

...
'payPalClassic'    => [
    'class'        => 'kun391\paypal\ClassicAPI',
    'pathFileConfig' => '',
],
'payPalRest'               => [
    'class'        => 'kun391\paypal\RestAPI',
    'pathFileConfig' => '',
    'successUrl' => '' //full url action return url
    'cancelUrl' => '' //full url action return url
],
...

=========

使用方法

REST API

  • 使用Paypal创建发票
//define params for request 
$params = [
    'currency' => 'Usd', // only support currency same PayPal
    'email' => 'nguyentruongthanh.dn@gmail.com',
    'items' => [
        [
            'name' => 'Vinamilk',
            'quantity' => 2,
            'price' => 100
        ],
        [
            'name' => 'Pepsi',
            'quantity' => 3,
            'price' => 90
        ]
    ]
];

// information invoice response
$response = Yii::$app->payPalRest->createInvoice($params);
  • 获取PayPal上的结账链接
//define params for request 
$params = [
    'currency' => 'USD', // only support currency same PayPal
    'description' => 'Buy some item',
    'total_price' => 470,
    'email' => 'nguyentruongthanh.dn@gmail.com',
    'items' => [
        [
            'name' => 'Vinamilk',
            'quantity' => 2,
            'price' => 100
        ],
        [
            'name' => 'Pepsi',
            'quantity' => 3,
            'price' => 90
        ]
    ]
];

$response = Yii::$app->payPalRest->getLinkCheckOut($params);

CLASSIC API

  • 获取账户信息
Attribute matchCriteria you can specify either NAME or NONE. Default: None
Attribute firstName and lastName (Required) if matchCriteria is name.

$params = [
    'email' => 'nguyentruongthanh.dn@gmail.com',
    'matchCriteria' => 'name',
    'firstName' => 'Thanh',
    'lastName'  => 'Nguyen'
];

$response = Yii::$app->payPalClassic->getAccountInfo($params);
  • 发送钱款(批量付款)
$params = [
    'email' => 'nguyentruongthanh.dn@gmail.com',
    'balance' => 200,
];

$response = Yii::$app->payPalClassic->sendMoney($params);
``