bitcko/yii2-bitcko-paypal-api

Bitcko Yii2 Paypal Api 扩展用于通过 PayPal php SDK 在您的项目中集成 PayPal 支付。

安装量: 5,932

依赖关系: 0

建议者: 0

安全: 0

星标: 2

关注者: 1

分支: 8

开放问题: 2

类型:yii2-extension

dev-master 2018-12-27 09:53 UTC

This package is not auto-updated.

Last update: 2024-09-21 01:19:04 UTC


README

Yii2 Bitcko PayPal Api 扩展用于在您的网站上集成简单的 PayPal 支付。

安装

安装此扩展的首选方式是通过 composer

运行以下命令之一:

php composer.phar require bitcko/yii2-bitcko-paypal-api:dev-master

或者

"bitcko/bitcko/yii2-bitcko-paypal-api": "dev-master"

将以下内容添加到您的 composer.json 文件的 require 部分中。

使用方法

一旦安装了扩展,只需在代码中使用它即可

  1. 在 PayPal 中创建开发者账户,然后创建一个应用。 PayPal 开发者仪表板
  2. 复制并粘贴客户端 ID 和客户端密钥到 app 配置目录中存在的 params.php 文件中
<?php

return [
    'adminEmail' => 'admin@example.com',
    'payPalClientId'=>'app client id here',
    'payPalClientSecret'=>'app client secret here'
];
  1. 在 app 配置目录中存在的 web.php 文件的 components 部分中配置扩展
<?php
'components'=> [
    ...
 'PayPalRestApi'=>[
            'class'=>'bitcko\paypalrestapi\PayPalRestApi',
            'redirectUrl'=>'/site/make-payment', // Redirect Url after payment
            ]
            ...
        ]
  1. 控制器示例:首先调用 checkout 动作,这将重定向到您在上一步中提到的 redirectUrl,在本例中为 ("/site/make-payment")
<?php

namespace app\controllers;

use Yii;

use yii\web\Controller;

class SiteController extends Controller
{
   
    public function actionCheckout(){
        // Setup order information array with all items
        $params = [
            'method'=>'paypal',
            'intent'=>'sale',
            'order'=>[
                'description'=>'Payment description',
                'subtotal'=>44,
                'shippingCost'=>0,
                'total'=>44,
                'currency'=>'USD',
                'items'=>[
                    [
                        'name'=>'Item one',
                        'price'=>10,
                        'quantity'=>1,
                        'currency'=>'USD'
                    ],
                    [
                        'name'=>'Item two',
                        'price'=>12,
                        'quantity'=>2,
                        'currency'=>'USD'
                    ],
                    [
                        'name'=>'Item three',
                        'price'=>1,
                        'quantity'=>10,
                        'currency'=>'USD'
                    ],

                ]

            ]
        ];
        
        // In this action you will redirect to the PayPpal website to login with you buyer account and complete the payment
        Yii::$app->PayPalRestApi->checkOut($params);
    }

    public function actionMakePayment(){
         // Setup order information array 
        $params = [
            'order'=>[
                'description'=>'Payment description',
                'subtotal'=>44,
                'shippingCost'=>0,
                'total'=>44,
                'currency'=>'USD',
            ]
        ];
      // In case of payment success this will return the payment object that contains all information about the order
      // In case of failure it will return Null
      return  Yii::$app->PayPalRestApi->processPayment($params);

    }
}