vanyok / pay-pal-yii-integration
Yii2 PayPal Api 扩展,用于在项目中集成 PayPal 支付,使用 PayPal php SDK v2
dev-master
2019-03-12 20:34 UTC
Requires
- paypal/paypal-checkout-sdk: 1.0.0
- yiisoft/yii2: ~2.0.0
This package is auto-updated.
Last update: 2024-09-13 11:49:34 UTC
README
Yii2 PayPal Api 扩展用于在网站中集成简单的 PayPal 支付。
安装
安装此扩展的首选方式是通过 composer。
运行以下命令:
php composer.phar require vanyok/pay-pal-yii-integration:dev-master
或添加以下内容到你的 composer.json
文件的 require 部分:
"vanyok/pay-pal-yii-integration": "dev-master"
使用方法
扩展安装完成后,只需在代码中使用它即可。
- 在 PayPal 中创建开发者账户并创建一个应用。请访问 PayPal 开发者控制台。
- 复制并粘贴客户端 ID 和客户端密钥到位于应用配置目录中的 params.php 文件
<?php return [ 'adminEmail' => 'admin@example.com', 'PAYPAL-SANDBOX-CLIENT-ID'=>'...', 'PAYPAL-SANDBOX-CLIENT-SECRET'=>'...', 'PAYPAL-LIVE-CLIENT-ID'=>'...', 'PAYPAL-LIVE-CLIENT-SECRET'=>'...' ];
- 在应用配置目录中的 web.php 文件中配置扩展
<?php 'components'=> [ ... 'PayPalRestApi'=>[ 'class'=>'vanyok\paypalyii\PayPalRestApi', 'returnUrl'=>'/site/welcome', // Redirect Url after payment 'cancelUrl'=>'/site/canceled', 'currencyCode' => 'USD', 'debug' => true, 'mode' => 'sandbox',// 'live' or 'sandbox' 'store_orders'=>true, ] ... ]
- 控制器示例:首先调用 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 $items = array(); $item = new Item; $item->load(['Item' => [ 'name' => 'Item name', 'description' => 'Item Description', 'value'=>37 ]]); $items[] = $item; // In this action you will redirect to the PayPpal website to login with you buyer account and complete the payment Yii::$app->PayPalRestApi->checkOut($items); } public function actionWelcome(){ // Setup order information array $order_id = Yii::$app->request->get('token'); $order = Yii::$app->PayPalRestApi->getOrder($order_id); if($order->status == Order::ORDER_COMPLETED || $order->status == Order::ORDER_APPROVED){ ... } } }