techlup/payment-gateway

此包最新版本(1.0.5)没有提供许可证信息。

将所有支付网关集成到您的项目中

1.0.5 2024-08-09 19:43 UTC

This package is auto-updated.

Last update: 2024-09-17 11:20:48 UTC


README

Php Payment Gateway V 1.0.5

使将支付网关集成到您的php项目变得容易。

使用composer安装

composer require techlup/payment-gateway

M-Pesa集成

配置应用程序

以下显示了根据您想使用的API所需的配置

$config = [
        'CONSUMER_KEY' => '',
        'CONSUMER_SECRET' => '',
        'PASS_KEY' => '', // required for STK push
        'SANDBOX' => true,
    ];

要切换沙盒和实时环境,您可以将以下内容添加到您的env文件中

进行商品购买的stk push

发起请求

首先将依赖项导入您的php类中,创建一个stkpush实例,然后像以下代码所示进行请求。

// import the dependency
use Techlup\PaymentGateway\Mpesa\StkPush;

// you can use the code below to format user phone number
preg_replace('/^(?:\+?254|0)?/', '254', $request->input("phone"));

$data = new StkPush($config);
$data->setCallbackUrl("{YOUR_API_CALLBACK}") // required
    ->setAmount("{AMOUNT}") // required
    ->setPhone("{PHONE_NUMBER}") // required 254*********
    ->setPartyB('123456')
    ->setShortCode('123456')
    ->setReference("{REF}") //reuired * e.g account number, room number, etc
    ->setRemarks("your remarks"); // optional

$response = $data->tillRequestPush();

请求提交后,以下响应将从mpesa返回。

请注意,$response是std class的对象。以下是您如何访问响应属性的示例。

例如,要访问MerchantRequestID,可以使用$response->MerchantRequestID

{
  "MerchantRequestID": "119215-30497********",
  "CheckoutRequestID": "ws_CO_18022023203127******",
  "ResponseCode": "0",
  "ResponseDescription": "Success. Request accepted for processing",
  "CustomerMessage": "Success. Request accepted for processing"
}

访问回调。

您发出的每个请求,m-pesa都会向您提供的API URL发起请求。

我已经为您简化了访问回调数据的方式。

// first import the dependencies
use Techlup\PaymentGateway\Mpesa\StkPush;

// get the callback data
$data = StkPush::getCallbackData();

以下是返回数据的格式,请记住,它是一个Std Class,不要与数组混淆。

{
  "status": 0,
  "MerchantRequestID": "119215-30497********",
  "CheckoutRequestID": "ws_CO_1802202319494*******",
  "ResultDesc": "The service request is processed successfully.",
  "Amount": 1,
  "MpesaReceiptNumber": "RBI6******",
  "Balance": "Balance",
  "TransactionDate": 20230218194956,
  "PhoneNumber": "254724******"
}

制作B2C请求

API请求

参考daraja API以了解传递的负载 https://developer.safaricom.co.ke/APIs/BusinessToCustomer

use Techlup\PaymentGateway\Mpesa\B2C;

$data = new B2C($config);
$data->setCallbackUrl("https://enm9smnsyaaq8.x.pipedream.net/")
    ->setAmount(1000)
    ->setOriginatorConversationId($uniqueId = uniqid('', true))
    ->setInitiatorName("username")
    ->setSecurityCredential('password')
    ->setCommandId("BusinessPayment")
    ->setPartyA("paybill")
    ->setPartyB('phone')
    ->setRemarks("here are my remarks")
    ->setOccasion("Testing");

$response = $data->pay();
echo json_encode($response);

API回调

使用以下代码从API回调获取结果。

$data = Techlup\PaymentGateway\Mpesa\B2C::getCallbackData();
echo json_encode($data);

失败请求

{
  "status": 2001,
  "OriginatorConversationID": "4ad2-40f1-8898-cef301c69e1c20604259",
  "ConversationID": "AG_20240809_*****",
  "ResultDesc": "The initiator information is invalid.",
  "TransactionID": "SH****97H",
  "fails": true
}

成功请求

{
  "status": 0,
  "OriginatorConversationID": "f0a2-4425-aa4b-1d01d2ea19ea127909458",
  "ConversationID": "AG_20240809_*****",
  "ResultDesc": "The service request is processed successfully.",
  "TransactionID": "SH96HNTPVG",
  "fails": false,
  "TransactionAmount": 1000,
  "TransactionReceipt": "SH96****",
  "ReceiverPartyPublicName": "2547*******75 - D*** ***",
  "TransactionCompletedDateTime": "09.08.2024 15:38:35",
  "B2CUtilityAccountAvailableFunds": 1152.03,
  "B2CWorkingAccountAvailableFunds": 0,
  "B2CRecipientIsRegisteredCustomer": "Y",
  "B2CChargesPaidAccountAvailableFunds": 0
}

PayPal集成

首先,您需要通过添加以下内容来配置您的环境。

PAYPAL_CLIENT_ID=""
PAYPAL_SECRET=""

您始终可以通过将 TECHLUP_PAYMENT_SANDBOX=true更新为false在实时和沙盒之间切换。

订阅

要使用PayPal将用户订阅到您的系统,请按照以下步骤操作。

  • 导入依赖项
use TTechlup\PaymentGateway\Paypal\Subscription;
  • 创建类实例
$paypal_subscription = new Subscription(base_path()); // base_path() is the base dir of your application where .env is located;
  • 订阅用户
// define the subscriber
$subscriber = array(
    'name' => array(
        'given_name' => 'John',
        'surname' => 'Doe'
    ),
    'email_address' => 'customer@example.com',
    'shipping_address' => array(
        'name' => array(
            'full_name' => 'John Doe'
        ),
        'address' => array(
            'address_line_1' => '',
            'address_line_2' => '',
            'admin_area_2' => '',
            'admin_area_1' => '',
            'postal_code' => '',
            'country_code' => 'US'
        )
    )
);

// specify your plan
// create the plan on paypal and the the plan id
$plan = new stdClass();
$plan->id = 'P-9PP51961TH493415EMW4NX5A';
$plan->value = '18';

// create the agreement
$agreement = $paypal_subscription->subscribe(
    $plan,
    $subscriber,
    'https://enx316omyr0k.x.pipedream.net/', // return url
    'https://enx316omyr0k.x.pipedream.net/' // cancel url
);

示例响应

{
  "status": "APPROVAL_PENDING",
  "id": "I-25GFPAT3X2MT",
  "create_time": "2024-01-30T12:21:02Z",
  "links": [
    {
      "href": "https://www.sandbox.paypal.com/webapps/billing/subscriptions?ba_token=BA-0RL61114E6855090G",
      "rel": "approve",
      "method": "GET"
    },
    {
      "href": "https://api.sandbox.paypal.com/v1/billing/subscriptions/I-25GFPAT3X2MT",
      "rel": "edit",
      "method": "PATCH"
    },
    {
      "href": "https://api.sandbox.paypal.com/v1/billing/subscriptions/I-25GFPAT3X2MT",
      "rel": "self",
      "method": "GET"
    }
  ]
}

现在将用户重定向到审批URL。用户将被重定向到您提供的返回URL

  • 检查订阅是否成功
    $subscriptionDetails = $paypal_subscription->getSubscriptionDetails('SUBSCRIPTION_ID');
    if ($subscriptionDetails['status'] === 'ACTIVE') {
        // Subscription was successfully approved and activated
        // Update database with subscription status
        // For example, update the user's subscription status in the database
    } else {
        // Subscription approval failed or encountered an error
        // Handle the error
        // For example, update the database to mark the subscription as failed
        // And redirect the user to an error page
    }
  • 取消订阅
$paypal_subscription->cancelSubscription('SUBSCRIPTION_ID');