intasend / intasend-php
IntaSend支付网关的官方PHP SDK
v1.1.1
2024-05-30 07:39 UTC
Requires
- php: >=7.0
- guzzlehttp/guzzle: ^7.0
README
官方文档
IntaSend支付网关的PHP SDK。IntaSend允许您通过几行代码轻松地将支付功能添加到您的应用程序中。
按照以下说明进行安装和开始使用。
访问我们的sandbox/developers测试您的API密钥。
查看我们的API文档以获取更多详细信息和有效载荷参考。
如何安装
composer require intasend/intasend-php
如何进行身份验证
IntaSend-php支持IntaSend的各种支付功能。以下是需要进行身份验证的凭证。
token - 是API令牌,用于状态检查、冲销请求、发送资金和钱包服务。 publishable_key - 也称为公钥,仅在支付收集/结账期间需要。
如何传递您的凭证
将您的凭证从.env
(推荐)添加到数组中,并将其包含在您的请求中。示例
$credentials = [
'token'=>'<YOUR-SECRET-TOKEN-HERE>',
'publishable_key'=>'<YOUR-PUBLISHABLE_KEY-HERE>'
];
$checkout = new Checkout();
$checkout->init($credentials);
如何使用结账URL接收支付
使用IntaSend,您可以生成一个安全的结账页面,将用户重定向到该页面以完成支付。
以下是如何设置的基本示例。请检查Laravel playground中的完整示例。
use IntaSend\IntaSendPHP\Checkout;
use IntaSend\IntaSendPHP\Customer;
$credentials = [
'publishable_key' => env('INTASEND_PUBLISHABLE_KEY'),
'test' => env('INTASEND_TEST_ENVIRONMENT', true),
];
$customer = new Customer();
$customer->first_name = "Joe";
$customer->last_name = "Doe";
$customer->email = "joe@doe.com";
$customer->country = "KE";
$amount = 10;
$currency = "KES";
// Add your website and redirect url where the user will be redirected on success
$host = "https://example.com";
$redirect_url = "https://example.com/callback";
$ref_order_number = "test-order-10";
$checkout = new Checkout();
$checkout->init($credentials);
$resp = $checkout->create($amount = $amount, $currency = $currency, $customer = $customer, $host=$host, $redirect_url = $redirect_url, $api_ref = $ref_order_number, $comment = null, $method = null);
// Redirect the user to the URL to complete payment
print_r($resp->url);
发送M-Pesa STK-Push
结账API生成一个URL,允许您进行M-Pesa收集和其他支付方式。如果您只想利用M-Pesa STK-Push选项,请考虑此collection->mpesa_stk_push()
选项。
use IntaSend\IntaSendPHP\Collection;
$credentials = [
'publishable_key' => env('INTASEND_PUBLISHABLE_KEY'),
'test' => env('INTASEND_TEST_ENVIRONMENT', true),
];
$collection = new Collection();
$collection->init($credentials);
$response = $collection->create($amount=10, $phone_number="2547...", $currency="KES", $method="MPESA_STK_PUSH", $api_ref="Your API Ref", $name="", $email="john@example.com");
print_r($response);
如何创建支付链接
支付链接是免费表单,您可以在电子邮件和社交媒体上与您的客户共享。与结账URL不同,创建支付链接时不需要/包含客户详细信息。预期客户将输入所有所需详细信息。
创建支付链接
use IntaSend\IntaSendPHP\PaymentLink;
$credentials = [
'token'=>'<YOUR-SECRET-TOKEN-HERE>',
'publishable_key'=>'<YOUR-PUBLISHABLE_KEY-HERE>'
];
$paymentLink = new PaymentLink();
$paymentLink->init($credentials);
$title = "Service 1";
$currency = "KES";
$amount = 100;
# Specify who should take care of the charges. Set to BUSINESS_PAYS for business to handle.
$mobile_tarrif = "CUSTOMER_PAYS";
$card_tarrif = "CUSTOMER_PAYS";
$is_active = "true";
$response = $paymentLink->create($title, $currency, $amount, $mobile_tarrif, $card_tarrif, $is_active);
print_r($response);
按ID列出或检索支付链接的详细信息
$response = $paymentLink->retrieve()
print_r($response);
$link_id = "AKSL1O1";
$response = $paymentLink->details($link_id);
print_r($response);
如何向M-Pesa B2C发送资金
use IntaSend\IntaSendPHP\Transfer;
$credentials = [
'token'=>'<YOUR-SECRET-TOKEN-HERE>',
'publishable_key'=>'<YOUR-PUBLISHABLE_KEY-HERE>'
];
$transactions = [
['account'=>'254723890353','amount'=>'20'],
['account'=>'254723890260','amount'=>'15']
];
$transfer = new Transfer();
$transfer->init($credentials);
$response=$transfer->mpesa("KES", $transactions);
//call approve method for approving last transaction
$response = $transfer->approve($response);
print_r($response);
// How to check or track the transfer status
$response = $transfer->status($response->tracking_id);
print_r($response);
如何向M-Pesa PayBill发送资金
要向M-Pesa PayBills发送资金,请在账户下指定业务编号和账户参考,如下所示。
use IntaSend\IntaSendPHP\Transfer;
$credentials = [
'token'=>'<YOUR-SECRET-TOKEN-HERE>',
'publishable_key'=>'<YOUR-PUBLISHABLE_KEY-HERE>'
];
$transactions = [
['account'=>'247247', 'account_type'=>'PayBill', 'account_reference'=>'1001200010', 'amount'=>'2000', 'narrative'=>'Trip']
];
$transfer = new Transfer();
$transfer->init($credentials);
$response=$transfer->mpesa("KES", $transactions);
//call approve method for approving last transaction
$response = $transfer->approve($response);
print_r($response);
如何向M-Pesa Till Number或PayBill发送资金
要向Till Numbers发送资金,只需指定账户号码。不需要账户参考。
use IntaSend\IntaSendPHP\Transfer;
$credentials = [
'token'=>'<YOUR-SECRET-TOKEN-HERE>',
'publishable_key'=>'<YOUR-PUBLISHABLE_KEY-HERE>'
];
$transactions = [
['name' => 'Business A','account'=>'524311','amount'=>'200', 'account_type'=>'PayBill', 'account_reference'=>'29822182', 'narrative'=> 'Bill Payment'],
['name' => 'Business B','account'=>'17626','amount'=>'150', 'account_type'=>'TillNumber', 'narrative'=> 'Purchase']
];
$transfer = new Transfer();
$transfer->init($credentials);
$response=$transfer->mpesa_b2b("KES", $transactions);
//call approve method for approving last transaction
$response = $transfer->approve($response);
print_r($response);
如何向银行发送资金
您需要银行代码和账户号码才能发送银行支付。以下是您的参考银行代码列表
use IntaSend\IntaSendPHP\Transfer;
$credentials = [
'token'=>'<YOUR-SECRET-TOKEN-HERE>',
'publishable_key'=>'<YOUR-PUBLISHABLE_KEY-HERE>'
];
$transactions = [
['name' => 'Joe Doe','account'=>'0129292920202','amount'=>'200', 'bank_code'=>'2', 'narrative'=> 'Bill Payment'],
['name' => 'Mary Doe','account'=>'525623632321','amount'=>'150', 'bank_code'=>'11', 'narrative'=> 'Purchase']
];
$transfer = new Transfer();
$transfer->init($credentials);
$response=$transfer->bank("KES", $transactions);
//call approve method for approving last transaction
$response = $transfer->approve($response);
print_r($response);
如何发送话费
use IntaSend\IntaSendPHP\Transfer;
$credentials = [
'token'=>'<YOUR-SECRET-TOKEN-HERE>',
'publishable_key'=>'<YOUR-PUBLISHABLE_KEY-HERE>'
];
$transactions = [
['account'=>'254723890353','amount'=>'20', 'narrative'=>'Airtime'],
['account'=>'254723890260','amount'=>'15', 'narrative'=>'Airtime']
];
$transfer = new Transfer();
$transfer->init($credentials);
$response=$transfer->airtime("KES", $transactions);
//call approve method for approving last transaction
$response = $transfer->approve($response);
print_r($response);
与钱包一起工作
如何创建新钱包、列出钱包和其他详细信息的示例
创建新钱包
use IntaSend\IntaSendPHP\Wallet;
$credentials = [
'token'=>'<YOUR-SECRET-TOKEN-HERE>',
'publishable_key'=>'<YOUR-PUBLISHABLE_KEY-HERE>'
];
$wallet = new Wallet();
$wallet->init($credentials);
$response = $wallet->create($currency='KES', $label='MY-WALLET-ID', $can_disburse=true);
print_r($response);
列出您账户中的所有钱包
$response = $wallet->retrieve();
print_r($response);
查看钱包详情及其交易
$response = $wallet->transactions('<wallet_id>');
print_r($response);
$response = $wallet->transactions($wallet_id);
print_r($response);
使用M-Pesa STK Push直接存入钱包
$response = $wallet->fund_mpesa_stk_push($wallet_id="<wallet_id>", $phone_number='2547...',$email='john@doe.com', $amount=10, $api_ref="API Request");
print_r($response);
使用结账方法直接存入钱包
use IntaSend\IntaSendPHP\Customer;
$customer = new Customer();
$customer->first_name = "Joe";
$customer->last_name = "Doe";
$customer->email = "joe@doe.com";
$customer->country = "KE";
$host = "https://example.com";
$redirect_url = "https://example.com";
$ref_order_number = "fund-wallet-10";
$response = $wallet->fund_checkout($wallet_id="<wallet_id>", $phone_number='2547..', $currency='USD', $customer=$customer, $amount=10, $host=$host, $redirect_url=$redirect_url, $api_ref=$ref_order_number, $card_tarrif = "BUSINESS-PAYS", $mobile_tarrif = "BUSINESS-PAYS");
print_r($response->url);
钱包间转账(内部转账)
在您的账户之间转移资金
$origin_wallet_id = "ABSKC10";
$destination_wallet_id = "DDS0911";
$amount = 100;
$narrative = "Commission deduction";
$response = $wallet->intra_transfer($origin_wallet_id, $destination_wallet_id, $amount, $narrative);
print_r($response);
外部钱包转账到M-PESA
use IntaSend\IntaSendPHP\Transfer;
$transactions = [
['account'=>'254...','amount'=>'20'],
['account'=>'254...','amount'=>'15']
];
$response=$transfer->mpesa("KES", $transactions=$transactions, $callback_url=null, $wallet_id='<wallet_id>');
print_r($response);
Like all other Send Money APIs, the above request is also a two step. Please go through the send money examples on full implementation for M-Pesa B2C, M-Pesa B2B, Bank Payouts and IntaSend P2P.
冲销管理
如何使用API处理退款示例
提出新的退款请求
use IntaSend\IntaSendPHP\Chagebacks;
$credentials = [
'token'=>'<YOUR-SECRET-TOKEN-HERE>',
'publishable_key'=>'<YOUR-PUBLISHABLE_KEY-HERE>'
];
$chagebacks = new Chagebacks();
$hagebacks->init($credentials);
$invoice_id = "INVOS012";
$amount = 100;
$reason = "Delayed delivery";
$response = $chagebacks->create($invoice_id, $amount, $reason);
print_r($response);
检索您账户中的退款/冲销列表
$response = $chagebacks->retrieve();
print_r($response);
获取冲销/退款请求的详细信息
$chagebacks_id = "CHSK102";
$response = $chagebacks->details($chagebacks_id);
print_r($response);