paynow/php-sdk

Paynow API的PHP SDK

1.0.5 2019-05-13 18:58 UTC

This package is auto-updated.

Last update: 2024-09-17 12:36:17 UTC


README

Paynow津巴布韦API的PHP SDK

先决条件

此库有一系列必须满足的先决条件才能正常工作

  1. PHP版本5.6或更高版本
  2. Curl扩展

安装

使用composer安装库

$ composer require paynow/php-sdk

并包含composer自动加载器

<?php
	require_once 'path/to/vendor/autoload.php';

	// Do stuff

或者

如果您没有安装composer,请首先在此处下载库。然后包含库中包含的自动加载器文件

<?php
	require_once 'path/to/library/autoloader.php';

	// Do stuff

使用示例

创建Paynow类的实例,可选地设置结果和返回URL

$paynow = new Paynow\Payments\Paynow(
	'INTEGRATION_ID',
	'INTEGRATION_KEY',
	'http://example.com/gateways/paynow/update',

	// The return url can be set at later stages. You might want to do this if you want to pass data to the return url (like the reference of the transaction)
	'http://example.com/return?gateway=paynow'
);

创建一个新的支付,传入该支付的引用(例如发票ID或您可以用来识别交易和用户电子邮件地址的任何内容

$payment = $paynow->createPayment('Invoice 35', 'user@example.com');

然后您可以开始向支付中添加项目

// Passing in the name of the item and the price of the item
$payment->add('Bananas', 2.50);
$payment->add('Apples', 3.40);

当您最终准备将支付发送到Paynow时,您可以使用$paynow对象中的send方法。

// Save the response from paynow in a variable
$response = $paynow->send($payment);

Paynow的响应将包含一些有用的信息,例如请求是否成功。如果是,例如,它包含用于将用户重定向以进行支付的URL。您可以在我们的wiki中查看响应中包含的数据的完整列表

如果请求成功,您应该考虑在数据库中保存Paynow发送的轮询URL

if($response->success()) {
    // Redirect the user to Paynow
    $response->redirect();

    // Or if you prefer more control, get the link to redirect the user to, then use it as you see fit
	$link = $response->redirectLink();

	// Get the poll url (used to check the status of a transaction). You might want to save this in your DB
	$pollUrl = $response->pollUrl();
}

移动交易

如果您想发送一个快速(移动)结账请求,唯一不同的是最后一步。您在$paynow对象中调用sendMobile而不是send方法。

sendMobile方法与send方法不同,它接受两个额外的参数,即发送支付请求的电话号码和用于请求的移动货币方法。注意,目前仅支持ecocash和onemoney

// Save the response from paynow in a variable
$response = $paynow->sendMobile($payment, '077777777', 'ecocash');

响应对象几乎与发送正常请求时得到的一样。有一些差异,首先,您不会得到一个要重定向的URL。相反,您会得到指示(理想情况下应向用户展示,指导他们在手机上进行支付)

if($response->success()) {
	// Get the poll url (used to check the status of a transaction). You might want to save this in your DB
	$pollUrl = $response->pollUrl();

	// Get the instructions
	$instrutions = $response->instructions();
}

检查交易状态

SDK公开了一个方便的方法,您可以使用它来检查交易状态。一旦您创建了Paynow类的实例。

// Check the status of the transaction with the specified pollUrl
// Now you see why you need to save that url ;-)
$status = $paynow->pollTransaction($pollUrl);

if($status->paid()) {
	// Yay! Transaction was paid for
} else {
	print("Why you no pay?");
}

完整使用示例

require_once('./paynow/vendor/autoload.php');

$paynow = new Paynow\Payments\Paynow(
	'INTEGRATION_ID',
	'INTEGRATION_KEY',
	'http://example.com/gateways/paynow/update',

	// The return url can be set at later stages. You might want to do this if you want to pass data to the return url (like the reference of the transaction)
	'http://example.com/return?gateway=paynow'
);

# $paynow->setResultUrl('');
# $paynow->setReturnUrl('');

$payment = $paynow->createPayment('Invoice 35', 'melmups@outlook.com');

$payment->add('Sadza and Beans', 1.25);

$response = $paynow->send($payment);


if($response->success()) {
    // Redirect the user to Paynow
    $response->redirect();

    // Or if you prefer more control, get the link to redirect the user to, then use it as you see fit
    $link = $response->redirectLink();

	$pollUrl = $response->pollUrl();


	// Check the status of the transaction
	$status = $paynow->pollTransaction($pollUrl);

}