dev-master
9999999-dev http://www.pilibaba.com/Pilipay is short for Pilibaba's payment. This library provides pilipay's API (in PHP).
MIT
The Requires
- php >=5.3.0
Wallogit.com
2017 © Pedro Peláez
Pilipay is short for Pilibaba's payment. This library provides pilipay's API (in PHP)., (*1)
Firstly please take a quick look at the HTTP API reference to get familiar with the basic bussiness logics., (*2)
As how to use in PHP, it's pretty simple:, (*3)
First of all, you should check the requirements via PilipayConfig::check(). If requirements is not satisfied, there would be some errors when using this library.
Example Code:, (*4)
// check requirements:
if (!PilipayConfig::check($errors)) {
// prompt errors to the merchant or administractor
echo "Error: Pilipay requirements is not satisfied: \n";
echo implode("\n", $errors);
}
Currently, the following requirements are to be checked:, (*5)
curl or fsockopen to make requestsopenssl to make HTTPS requestsautoload.php in order to auto load the classes in pilipay.$order = new PilipayOrder().$good = new PilipayGood().$order->addGood($good);.echo $order->renderSubmitForm(); die;
Sample code:, (*6)
// autoload require 'path/to/pilipay/autoload.php'; // create an order $order = new PilipayOrder(); $order->merchantNo = '1231312'; // a number for a merchant from pilibaba $order->appSecret = 'abcdefg'; // the secret key from pilibaba $order->currencyType = 'USD'; // indicates the unit of the following orderAmount, shipper, tax and price $order->orderNo = '1231231231'; $order->orderAmount = '1.23'; $order->pageUrl = 'https://www.example-shop.com/path/to/some/product'; $order->serverUrl = 'https://www.example-shop.com/path/to/paid/callback'; $order->shipper = '1.23'; $order->tax = '1.23'; // create a good $good = new PilipayGood(); $good->name = 'Product Name'; $good->pictureUrl = 'https://www.example-shop.com/path/to/product/picture'; $good->price = '1.23'; $good->productUrl = 'https://www.example-shop.com/path/to/product'; $good->productId = '123123'; $good->quantity = 1; $good->weight = 1.23; $good->weightUnit = 'kg'; // add the good to order $order->addGood($good); // if there are more goods, please add... //$good = new PilipayGood(); //... //$order->addGood($good); // render submit form, which would auto submit echo $order->renderSubmitForm(); die;
autoload.php in order to auto load the classes in pilipay.$order = new PilipayOrder();.$barcodePicUrl = $order->getBarcodePicUrl();.Sample code:, (*7)
// autoload require 'path/to/pilipay/autoload.php'; // create an order $order = new PilipayOrder(); // orderNo and merchantNo must be provided: $order->orderNo = '123123'; $order->merchantNo = '123123'; // get the barcode's picture URL: $barcodePicUrl = $order->getBarcodePicUrl(); // do whatever you want to with the barcode
autoload.php in order to auto load the classes in pilipay.$order = new PilipayOrder();.$order->updateTrackNo($trackNo);.Sample code:, (*8)
// autoload require 'path/to/pilipay/autoload.php'; // create an order $order = new PilipayOrder(); // orderNo and merchantNo must be provided: $order->orderNo = '123123'; $order->merchantNo = '123123'; // update $order->updateTrackNo($trackNo); // $trackNo must be the same with the track number on the package when shipping.
After the customer has paid, a request to $order->serverUrl would be sent. In order to properly deal this request, PilipayPayResult can be used. It's pretty simple. So just show the example code:, (*9)
// autoload
require 'path/to/pilipay/autoload.php';
// create an instance from the request
$payResult = PilipayPayResult::fromRequest();
// verify whether the request is valid:
if (!$payResult->verify($appSecret)){ // $appSecret is exactly the same with $order->appSecret
// error handling...
die('Invalid request');
}
// judge whether payment is successfully completed:
if (!$payResult->isSuccess()){
// deal failure
} else {
// deal success
}
When setting fields of an order or a good, submiting the order, and updating track number, if an error is encountered, a PilipayError will be thrown.
So a try ... catch block should be used to deal errors.
Example code:, (*10)
try{
// submit order, update track number...
} catch (PilipayError $e) {
// deal the error
// $e->getMessage() will be detailed reason.
}
PilipayLogger provides a extendable logging. PilipayLogger::setHandler() can be used to inject a logger handler. For example, logging to a file:, (*11)
PilipayLogger::instance()->setHandler(function($level, $msg){
file_put_contents('path/to/pilipay/log/file', sprintf('%s %s: %s'.PHP_EOL, date('Y-m-d H:i:s'), $level, $msg));
});
There are some helpful configurations:, (*12)
useHttps defines whether to use HTTPS - although HTTPS is recommend by default, you might want to use HTTP in some circumstances.useProductionEnv defines whether to use the production environment. It's default value is true. But when you are testing, it is recommend to set useProductionEnv to false. After that, you will not need to pay real money to complete an order. Orders will be simulated as if be paid.Example code:, (*13)
// Not recommended: use HTTP interface - maybe openssl on the server cannot work. PilipayConfig::setUseHttps(false); // When testing, do not use production environment: PilipayConfig::setUseProductionEnv(false);
Pilipay is short for Pilibaba's payment. This library provides pilipay's API (in PHP).
MIT