consoletvs/payzap

为 Laravel 提供简单的 PayPal 付款

1.1.0 2018-03-07 14:37 UTC

This package is auto-updated.

Last update: 2024-08-29 02:14:55 UTC


README

Payzap - 为 Laravel 提供简单的 PayPal 付款

描述

Payzap 是一种新的简单方法,只需几行代码即可将 PayPal 集成到您的 Laravel 应用程序中。它使用简单优雅的语法来创建支付。

安装

composer require consoletvs/payzap

将服务提供者注册到当前项目中(如果使用 Laravel 5.5+ 则不需要)

ConsoleTVs\Payzap\PayzapServiceProvider::class

发布配置

php artisan vendor:publish

示例付款控制器、视图和路由

  • PaymentController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use ConsoleTVs\Payzap\Classes\Payment;

class PaymentController extends Controller
{
    /**
     * Returns the payment view and setups the javascript logic.
     *
     * @author Erik Campobadal <soc@erik.cat>
     * @copyright 2017 erik.cat
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $payment = Payment::prepare()
            ->createUrl(route('api::payments.create'))
            ->executeUrl(route('api::payments.execute'))
            ->redirectUrl(route('payment.finished'))
            ->buttonId('paypal-button');

        return view('payment', compact('payment'));
    }

    /**
     * Creates the paypal payment using payzap.
     *
     * @author Erik Campobadal <soc@erik.cat>
     * @copyright 2017 erik.cat
     * @return \ConsoleTVs\Payzap\Classes\Payment
     */
    public function create()
    {
        return Payment::create()
            ->addItem([
                'name' => 'Product 1',
                'currency' => 'EUR',
                'quantity' => 1,
                'price' => 0.5,
            ])->addItem([
                'name' => 'Product 2',
                'currency' => 'EUR',
                'quantity' => 2,
                'price' => 0.25,
            ])->description("My first payment")
            ->generate();
    }

    /**
     * Executes the paypal payment and returns the result boolean in an array.
     *
     * @author Erik Campobadal <soc@erik.cat>
     * @copyright 2017 erik.cat
     * @param  Request $request
     * @return array
     */
    public function execute(Request $request)
    {
        return ['result' => Payment::execute($request->payment_id, $request->payer_id) !== false];
    }
}
  • payment.blade.php
<!DOCTYPE html>

<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
</head>

<body>
    <div id="paypal-button"></div>
    {!! $payment->scripts() !!}
</body>
  • 路由(web.php)
Route::get('/payment', 'PaymentController@index')->name('payment');
Route::get('/payment/finished', function () {
    return "Payment Executed.";
})->name('payment.finished');