dedanirungu / k2-connect-php
Kopokopo的PHP SDK
Requires
- guzzlehttp/guzzle: ^7.0.1
Requires (Dev)
- phpunit/phpunit: ^7
This package is auto-updated.
Last update: 2024-08-29 16:07:16 UTC
README
这是一个模块,用于帮助PHP开发者使用Kopokopo的API
安装
您可以通过composer安装PHP SDK。
推荐使用Composer安装SDK。
composer require kopokopo/k2-connect-php
初始化
应使用您的客户端ID和客户端密钥配置此包,您可以在Kopokopo应用的账户中获取
//Store your client id and client secret as environment variables //Including the kopokopo sdk use Kopokopo\SDK\K2; // do not hard code these values $options = [ 'clientId' => 'YOUR_CLIENT_ID', 'clientSecret' => 'YOUR_CLIENT_SECRET', 'apiKey' => 'YOUR_API_KEY', 'baseUrl' => 'https://sandbox.kopokopo.com' ]; $K2 = new K2($options);
初始化后,您可以按如下方式获取提供的服务实例
- 令牌 :
$tokens = $K2->TokenService(); - Webhooks :
$webhooks = $K2->Webhooks(); - STK PUSH :
$stk = $K2->StkService(); - 支付 :
$pay = $K2->PayService(); - 结算转账 :
$transfer = $K2->SettlementTransferService(); - 轮询服务 :
$polling = $K2->PollingService(); - 短信通知服务 :
$sms_notification = $K2->SmsNotificationService();
用法
令牌
发送数据到Kopokopo的API时,您需要传递一个访问令牌。
这将返回accessToken和expiresIn值
use Kopokopo\SDK\K2; // Do not hard code these values $options = [ 'clientId' => 'YOUR_CLIENT_ID', 'clientSecret' => 'YOUR_CLIENT_SECRET', 'apiKey' => 'YOUR_API_KEY', 'baseUrl' => 'https://sandbox.kopokopo.com' ]; $K2 = new K2($options); // Get one of the services $tokens = $K2->TokenService(); // Use the service $result = $tokens->getToken(); //print the result print_r($result);
Webhooks
- 消费
// TODO: review this $router->map('POST', '/webhook', function () { global $K2; global $response; $webhooks = $K2->Webhooks(); $json_str = file_get_contents('php://input'); var_dump($json_str); $response = $webhooks->webhookHandler($json_str, $_SERVER['HTTP_X_KOPOKOPO_SIGNATURE']); echo json_encode($response); });
- 订阅
$webhooks = $K2->Webhooks(); //To subscribe to a webhook $response = $webhooks->subscribe([ 'eventType' => 'buygoods_transaction_received', 'url' => 'https://:8000/webhook', 'scope' => 'till', 'scopeReference' => '000000', 'accessToken' => 'my_access_token' ]); print_r($response);
STK PUSH
$stk = $K2->StkService(); $result = $stk->initiateIncomingPayment([ 'paymentChannel' => 'M-PESA STK Push', 'tillNumber' => 'K000000', 'firstName' => 'Jane', 'lastName' => 'Doe', 'phoneNumber' => '0712345678', 'amount' => 3455, 'email' => 'example@example.com', 'callbackUrl' => 'https://:8000/test', 'accessToken' => 'myRand0mAcc3ssT0k3n', ]); print_r($result);
查看示例应用以获取其他用法示例。
服务
目前唯一支持的ISO货币代码是:KES
TokenService
-
使用
$TokenService->getToken()获取访问令牌。- 响应将具有以下结构
[ 'status' => 'success', 'data' => [ 'accessToken' => 'GT6576QGYdYh8i5s8DnxUQVphFewh-8eiO2', 'tokenType' => 'Bearer', 'expiresIn' => 3600, 'createdAt' => '2021-04-06T13:49:50+03:00' ] ]
注意:访问令牌是发送后续请求所必需的
- 使用
$TokenService->revokeToken(['accessToken' => 'myRand0mAcc3ssT0k3n'])撤销访问令牌。
注意:访问令牌不能用于发送后续请求
-
使用
$TokenService->introspectToken(['accessToken' => 'myRand0mAcc3ssT0k3n'])检查令牌。 -
使用
$TokenService->infoToken(['accessToken' => 'myRand0mAcc3ssT0k3n'])获取有关令牌的更多信息
StkService
-
$StkService->initiateIncomingPayment([ stkOptions ]):stkOptions:包含以下键的数组数组tillNumber:您从Kopo Kopo仪表板获取的在线支付简码必需firstName:客户的姓氏lastName:客户的姓氏phoneNumber:从中提取资金的电话号码。必需email:客户的电子邮件地址currency:3位ISO格式货币代码。必需amount:要收取的金额。必需callbackUrl:结果将发布到的URL必需paymentChannel:支付渠道。默认为:"M-PESA STK Push"。必需accessToken:从TokenService响应中获取必需metadata:它是一个包含最多5个键值对的哈希表
-
$StkService->getStatus([location ]):location:您发送请求时获取的请求位置accessToken:从TokenService响应中获取必需
更多信息,请参阅https://api-docs.kopokopo.com/#receive-payments-from-m-pesa-users-via-stk-push
PayService
-
PayService->addPayRecipient([ payRecipientOptions ]):payRecipientOptions:包含以下键的数组数组type:收款人类型必填- 移动钱包收款人(
mobile_wallet)firstName:收款人名必填lastName:收款人姓必填phoneNumber:收款人电话号码必填email:收款人电子邮件地址network:收款人网络必填
- 银行账户收款人(
bank_account)accountName:收款人账户名必填accountNumber:收款人账户号码必填bankBranchRef:从kopokopo仪表板获取的银行分支参考必填settlementMethod:结算方式必填
- 外部收银台收款人(
till)tillNumber:收款人收银台号码必填tillName:收款人收银台名称必填
- Paybill(
paybill)paybillName:收款人Paybill名称必填paybillNumber:收款人Paybill号码必填paybillAccountNumber:收款人账户号码必填
- 移动钱包收款人(
accessToken:从TokenService响应中获取必需
-
PayService->sendPay([ payOptions ]):payOptions:包含以下键的数组数组destinationType:收款人类型必填destinationReference:收款人参考必填currency:3位ISO格式货币代码。必需amount:要收取的金额。必需callbackUrl:结果将发布到的URL必需description:付款描述必填tags:与付款相关的标签category:付款所属的类别accessToken:从TokenService响应中获取必需metadata:它是一个包含最多5个键值对的哈希表
-
PayService->getStatus([ location ]):location:您发送请求时获取的请求位置accessToken:从TokenService响应中获取必需
更多信息,请参阅https://api-docs.kopokopo.com/#send-money-pay
SettlementTransferService
-
SettlementTransferService->createMerchantBankAccount([ accountOpts ]):accountOpts:包含以下键的数组数组accountName:结算账户名称必填bankBranchRef:结算银行分支参考必填accountNumber:结算账户号码必填settlementMethod:结算方式必填accessToken:从TokenService响应中获取必需
-
SettlementTransferService->createMerchantWallet([ accountOpts ]):accountOpts:包含以下键的数组数组phoneNumber:结算电话号码必填network:结算的移动货币网络必填accessToken:从TokenService响应中获取必需
-
SettlementTransferService->settleFunds([ settleOpts ]):settleOpts:包含以下键的数组数组destinationType:目的地类型目标转账必填destinationReference:目的地参考目标转账必填currency:3位ISO格式货币代码。目标转账必填amount:结算金额。目标转账必填PS:如不包含,则将结算全部余额。accessToken:从TokenService响应中获取必需
-
SettlementTransferService->getStatus([ location ]):location:您发送请求时获取的请求位置accessToken:从TokenService响应中获取必需
更多信息,请参阅api-docs#transfer
PollingService
-
PollingService->pollTransactions([ pollingOpts ]):pollingOpts:包含以下键的数组数组fromTime:轮询请求的起始时间toTime:轮询请求的结束时间scope:轮询请求的范围scopeReference:范围参考对于'Till'范围必填callbackUrl:结果将发布到的URL必需accessToken:从TokenService响应中获取必需
-
PollingService->getStatus([ statusOpts ]):statusOpts:包含以下键的数组数组location:从请求中获取的位置URL必填accessToken:从TokenService响应中获取必需
这适用于所有获取位置响应的请求。
更多信息,请参阅api-docs#polling
SmsNotificationService
-
SmsNotificationService->sendTransactionSmsNotification([ transactionNotificationOpts ]):transactionNotificationOpts:包含以下键的数组数组webhookEventReference:buygoods_transaction_received webhook的事件引用。message:要发送的消息callbackUrl:结果将发布到的URL必需accessToken:从TokenService响应中获取必需
-
SmsNotificationService->getStatus([ statusOpts ]):statusOpts:包含以下键的数组数组location:从请求中获取的位置URL必填accessToken:从TokenService响应中获取必需
这适用于所有获取位置响应的请求。
更多信息,请参阅 api-docs#transaction-sms-notifications
响应和结果
- 除了
TokenService之外的所有 POST 请求都是异步的。这意味着当请求完成时,结果将发布到您的自定义回调 URL。POST 请求的即时响应包含您发送的请求的locationURL,您可以使用它来查询状态。
注意:异步结果的处理方式类似于 webhook。
- 要访问响应或 webhook 负载的不同部分,请使用以下键进行访问
令牌响应
-
getToken() 成功响应
acessTokentokenTypeexpiresIncreatedAt
-
introspectToken() 成功响应
activescopeclientIdtokenTypeexp- 过期时间iat- 开始时间
-
infoToken() 成功响应
scopeexpiresInresourceOwnerIdapplicationIdtokenTypecreatedAt
Webhooks
-
Buygoods 收到
idtopiccreatedAteventTyperesourceIdreferenceoriginationTimesenderPhoneNumberamountcurrencytillNumbersystemstatussenderFirstNamesenderMiddleNamesenderLastNamelinkSelflinkResource
-
B2b 交易收到
idtopiccreatedAteventTyperesourceIdreferenceoriginationTimesendingTillamountcurrencytillNumbersystemstatuslinkSelflinkResource
-
商户间交易收到
idtopiccreatedAteventTyperesourceIdoriginationTimesendingMerchantamountcurrencystatuslinkSelflinkResource
-
Buygoods 交易已撤销
idtopiccreatedAteventTyperesourceIdreferenceoriginationTimesenderPhoneNumberamountcurrencytillNumbersystemstatussenderFirstNamesenderMiddleNamesenderLastNamelinkSelflinkResource
-
转账完成 webhook
-
id -
topic -
createdAt -
eventType -
resourceId -
originationTime -
amount -
currency -
status -
disbursements -
linkSelf -
linkResource -
destinationReference -
destinationType -
如果目的地类型是银行
settlementMethodbankBranchRefaccountNameaccountNumber
-
如果目的地类型是移动钱包
firstNamelastNamephoneNumbernetwork
-
-
客户创建 webhook
idtopiccreatedAteventTypefirstNamemiddleNamelastNamephoneNumberlinkSelflinkResource
结果
-
结算转账结果
idtypecreatedAtstatustransferBatchesamountcurrencylinkSelfcallbackUrl
-
支付结果
idtypestatuscreatedAttransferBatchesamountcurrencymetadatalinkSelfcallbackUrl
-
Stk Push 结果
-
成功结果
idtypeinitiationTimestatuseventTyperesourceIdreferenceoriginationTimesenderPhoneNumberamountcurrencytillNumbersystemsenderFirstNamesenderMiddleNamesenderLastNameresourceStatuserrorsmetadatalinkSelfcallbackUrl
-
失败结果
idtypeinitiationTimestatuseventTyperesourceerrorsmetadatalinkSelfcallbackUrl
-
-
轮询结果
idtypestatusfromTimetoTimescopescopeReferencetransactionslinkSelfcallbackUrl
-
交易短信通知结果
idtypestatusmessagewebhookEventReferencelinkSelfcallbackUrl
状态有效载荷
-
Webhook 订阅状态
idtypeeventTypewebhookUristatusscopescopeReference
-
商户银行账户状态
idtypeaccountNumberaccountNamebankBranchRefsettlementMethodstatusaccountReference
-
商户移动钱包状态
idtypefirstNamelastNamephoneNumbernetworkstatusaccountReference
-
结算转账状态
- 此有效载荷类似于
SettlementTransferResult有效载荷
- 此有效载荷类似于
-
支付状态
- 此有效载荷类似于
PaymentResult有效载荷
- 此有效载荷类似于
-
支付接收者状态
-
id -
type -
recipientType -
status -
recipientReference -
如果
recipientType == "Bank Account"accountNumberaccountNamebankBranchRefsettlementMethod
-
如果
recipientType == "Mobile Wallet"firstNamelastNamephoneNumbernetworkemail
-
如果
recipientType == "Till"tillNumbertillName
-
如果
recipientType == "Paybill"paybillNamepaybillNumberpaybillAccountNumber
-
-
Stk Push 状态
- 成功请求
- 此有效载荷类似于成功结果
- 失败请求
- 此有效载荷类似于失败结果
- 挂起请求
idtypeinitiationTimestatuseventTyperesourceerrorsmetadatalinkSelfcallbackUrl
- 成功请求
-
轮询状态
- 此有效载荷与
Polling结果有效载荷相同
- 此有效载荷与
-
交易短信通知状态
- 此有效载荷与
Transaction SMS Notification结果有效载荷相同
- 此有效载荷与
错误响应
-
errorCode -
errorMessage -
令牌错误响应
errorerrorDescription
有关预期有效载荷和错误代码的更多信息,请参阅 api 文档
作者
贡献
我们热情欢迎各位,只需发起一个pull request,我们会进行审查。
开发
运行所有测试
$ composer install $ php vendor/bin/phpunit tests --testdox
问题
如果您发现一个bug,请在我们的GitHub问题跟踪器上提交一个问题。
许可证
k2-connect-php遵循MIT许可证。有关详细信息,请参阅LICENSE。