rebel/rebel-rebelpay

Paystack Laravel 扩展包

v1.3.0 2023-07-27 13:59 UTC

README

Latest Version on Packagist

Total Downloads

这是一个Laravel Composer扩展包,简化了在Paystack上进行支付接受的过程。

安装

您可以通过Composer安装此包

composer require rebel/rebel-rebelpay

运行此命令以发布配置文件和所有必要的文件

php artisan rebel-rebelpay:install

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="rebel-rebelpay-config"

这是发布配置文件的内容

return [
    'publickey' => env('PAYSTACK_PUBLIC_KEY'),

    'secretkey' => env('PAYSTACK_SECRET_KEY'),

    'paystackurl' => env('PAYSTACK_PAYMENT_URL'),

    'merchantmail' => env('MERCHANT_EMAIL'),
];

特性

  • 支付
  • 从Paystack获取所有交易记录
  • 从Paystack获取单个交易记录
  • 获取所有成功的Paystack交易
  • 获取所有失败的Paystack交易
  • 获取所有未完成的Paystack交易
  • 以CSV格式导出交易
  • 获取交易历史记录

此包的工作流程

  1. 客户将被重定向到支付提供商的网站。
  • 客户在您的网站上完成结账表单后,将数据传递给包,然后客户将被重定向到paystack以完成支付。
  1. 客户到达Paystack平台
  • 在客户被重定向到paystack之后,他们可以根据与paystack的账户设置选择可用的支付选项并完成交易。
  1. 客户重定向到网站
  • 在客户在Paystack网站上完成交易后,他们将被重定向回我们在Laravel应用程序中设置的路径,而不是依赖于Paystack的回调webhook。

环境变量

  • PAYSTACK_PUBLIC_KEY= 插入您的Paystack公钥
  • PAYSTACK_SECRET_KEY= 插入您的Paystack私钥
  • PAYSTACK_PAYMENT_URL=https://api.paystack.co
  • MERCHANT_EMAIL= kwadejeffrey@gmail.com

使用/示例

$rebelPay = new Rebel\RebelPay();
echo $rebelPay->echoPhrase('Hello, Rebel!');
<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Rebel\RebelPay\Facades\RebelPay;

class Controller extends BaseController
{
    public function paystackCheckout(Request $request)
    {
        //Let's validate form data
        $validated_data = $request->validate([
            'first_name' => 'required', 
            'last_name' => 'required',
            'email' => 'required', 
            'amount' => 'required',
        ]);

        /**
         * Let's build our Paystack data
         * Always multiply amount by 100
         * First, Last and phone name are optional for direct payments
         * callback_url is optional since we can set a default callback on Paystacks' dashboard but if we set a value for it, it'll overwrite the dashboard default value.
         * I personally prefer setting a value in my code
         * You use any of your applications web routes' name as the callback_url
         */
        $data = [
            'email' => $validated_data->email,
            'amount' => $validated_data->amount * 100,
            'first_name' => $validated_data->first_name,
            'last_name' => $validated_data->last_name,
            'phone' => $validated_data->tel,
            'callback_url' => route('callback')
        ];

        /**
         * Let's call Rebelpay with the makePayment method
         * And let's inject the data array into the method
         * We'll be redirect to the paystack website to complete transaction
         * A json_decoded with these attributes (Status, Message and Data) is returned
         * Status has a value of "True"
         * Message has a value of "Verification successful"
         * The important keys to note in the data attribute is (status, reference, amount, authorization, customer, channel )
         * You can deploy your code is diverse ways using these attributes
         * You can clear the cart from DB or simply invalidate it depending on your DB structure
         */
        $res = RebelPay::makePayment($data);
        // You could do something like this
        if($res->data->status){
            Cart::where('user_id', $user_id)->delete();
        }
        //You could pass the status as a session
        return redirect('/')->with('message', $res->data->status);
        return view('rebelPay.pageOne');
    }

    /**
     * Create a customer on Paystack
     * 
     * @param array $data
     * 
     * @return string JSON-encoded string representing the customer information.
     */
    public function letsCreateACustomer()
    {
        $data = [
            "email" => "customer@email.com",
            "first_name" => "Zero",
            "last_name" => "Sumthing",
            "phone" => "+233500005737"
        ];

        $res = RebelPay::createCustomer($data);
        dd($res);
    }

    public function letsUpdateCustomer()
    {
        /**
     * Update customer profile
     *
     * @param array $data
     * @param string $id
     * @return string JSON-encoded string representing the customer information.
     */
        $res = RebelPay::updateCustomer(array $data, string $id)
    }

    /**
     * Get a specific customer's profile from Paystack
     *
     * @param  string  $identifier Can be either email or customer ID
     * @return string JSON-encoded string representing the customer information.
     */
    public function letsGetACustomer($identifier)
    {
        
        return $res = RebelPay::getClient($identifier);
    }

    /**
     * We'll use this function to return all our transactions from Paystack
     */
    public function getAllTransactionsFromPaystack()
    {
        /**
         * By default this we'll return a status, message, data and meta attributes
         * The data is what we need so pass it to the blade view or vue component
         * This method return the 100 transactions, you can alter the number
         * This method holds two arguments ($perPage = 100, $page = 1)
         * You can adjust them as you see fit 
         */
        $res = RebelPay::getAllTransactions();
        dd($res);
        return view('rebelPay.pageOne', [
            'transactions' => $res->data
        ]);
    }


    /**
     * We'll use this function to return all our Failed transactions from Paystack
     */
    public function getFailedTransactionsFromPaystack()
    {
        /**
         * By default this we'll return a status, message, data and meta attributes
         * The data is what we need so pass it to the blade view or vue component
         * This method return the 100 transactions, you can alter the number
         * This method holds two arguments ($perPage = 100, $page = 1)
         * You can adjust them as you see fit 
         */
        $res = RebelPay::getFailedTransactions();
        dd($res);
        return view('rebelPay.pageOne', [
            'transactions' => $res->data
        ]);
    }


    /**
     * We'll use this function to return all our successful transactions from Paystack
     */
    public function getSuccessfulTransactionsFromPaystack()
    {
        /**
         * By default this we'll return a status, message, data and meta attributes
         * The data is what we need so pass it to the blade view or vue component
         * This method return the 100 transactions, you can alter the number
         * This method holds two arguments ($perPage = 100, $page = 1)
         * You can adjust them as you see fit 
         */
        $res = RebelPay::getSuccessfulTransactions();
        dd($res);
        return view('rebelPay.pageOne', [
            'transactions' => $res->data
        ]);
    }


    /**
     * We'll use this function to return all our abandoned transactions from Paystack
     */
    public function getAbandonedTransactionsFromPaystack()
    {
        /**
         * By default this we'll return a status, message, data and meta attributes
         * The data is what we need so pass it to the blade view or vue component
         * This method return the 100 transactions, you can alter the number
         * This method holds two arguments ($perPage = 100, $page = 1)
         * You can adjust them as you see fit 
         */
        $res = RebelPay::getAbandonedTransactions();
        dd($res);
        return view('rebelPay.pageOne', [
            'transactions' => $res->data
        ]);
    }

    /**
     * Method to get a targeted transaction from Paystack
     */
    public function getTransactionFromPaystack(int $id)
    {
        /**
         * Pass the transaction id as an argument
         * e.g 2749916096
         */
        $res = RebelPay::getTransaction($id);
    }


}

测试

composer test

变更日志

有关最近更改的更多信息,请参阅变更日志

贡献

有关详细信息,请参阅贡献

安全漏洞

有关如何报告安全漏洞的信息,请参阅我们的安全策略

致谢

许可

MIT许可(MIT)。有关更多信息,请参阅许可文件