peggyforms / php-sdk
PeggyPay PHP SDK
Requires
- php: >=7.0.0
- guzzlehttp/guzzle: ~6.0|~7.0
Requires (Dev)
- phpunit/phpunit: 6.*
README
Peggy Pay PHP SDK
使用此 SDK 与 Peggy Pay 简易通信。
安装
composer require peggyforms/php-sdk dev-master
当前版本:1.1.17
基本用法
初始化
您必须始终通过使用您的 API 密钥初始化 PeggyPay 对象来开始。您可以在 您的账户 中找到您的 API 密钥
require "vendor/autoload.php"; $peggyPay = new PeggyForms\Api("myApiKey");
通过哈希获取提交
通过其哈希码轻松获取提交。Peggy Pay 的每次重定向都会在参数:peggyHash 中发送提交哈希
// Get the HTTP request param $hash = $peggyPay->get->param("peggyHash"); // And get the submission $submission = $peggyPay->submissions->get($hash);
轻松获取字段提交值
$submission->get("fieldName");
这是获取订单支付状态的方式
$submission->PaymentStatus; // complete/init/error
$submission->PaymentAmount;
高级用法 - 动态内容
对于具有 AJAX / HTTP 功能的计划,您可以
- 填充选择字段、列表、AJAX 代理和数据网格。
- 创建自定义字段验证
- 响应 POST 提交操作
选择字段
$peggyPay->response->choiceField( true, // Call succeded? [ new \PeggyForms\Classes\ListItem(1, "My dynamic option 1"), new \PeggyForms\Classes\ListItem(2, "My dynamic option 2") ] );
数据网格
$peggyPay->response->dataGrid( true, // Call succeded? [ // The columns new \PeggyForms\Classes\GridColumn("My grid column 1"), new \PeggyForms\Classes\GridColumn("My grid column 2"), new \PeggyForms\Classes\GridColumn("My grid column 3") ], [ // And the rows with items [ new \PeggyForms\Classes\GridRowItem("Col row 1 value 1"), new \PeggyForms\Classes\GridRowItem("Col row 1 value 2"), new \PeggyForms\Classes\GridRowItem("Col row 1 value 3"), ], [ new \PeggyForms\Classes\GridRowItem("Col row 2value 1"), new \PeggyForms\Classes\GridRowItem("Col row 2value 2"), new \PeggyForms\Classes\GridRowItem("Col row 2value 3"), ] ] );
验证
您可以使用编辑器中的默认验证工具来验证表单字段。当您需要自定义验证时,您可以使用 JavaScript。或者您可以通过 HTTP 请求进行验证。此示例是通过 HTTP 请求验证字段。
// Value of the field with the validation $value = $peggyPay->get->param("value"); // Other fields you added as parameters $yourFormField1 = $peggyPay->get->param("formfield-1-name"); $yourFormField2 = $peggyPay->get->param("formfield-2-name"); $validated = your_function($value, $yourFormField1, $yourFormField2); if ($validated === true) { $status = \PeggyForms\Constants\Validation::OK; } elseif ($validated === false) { $status = \PeggyForms\Constants\Validation::NOK; } else { $status = \PeggyForms\Constants\Validation::INIT; } $peggyPay->response->validation( true, // Call succeded $status, "My nice custom response message", [ "prop" => 102 ] // Your custom props for usage in your rules or display as text in your form );
填充 Ajax 代理字段
Ajax Proxy 字段如果您的 Web 服务提供多个数据集非常有用。例如,如果您的 API 调用返回产品列表和国家列表,则 AJAX 代理字段非常有用。只需一个 HTTP 请求就会执行,并且所有相关字段都将使用此结果作为数据源。
在此示例中,我们使用无类型对象,但您可以使用任何可序列化为 JSON 的对象。
$peggyPay->response->ajaxProxy( true, [ "products" => [ (object)[ "id" => 1, "name" => "My product 1" ], .. ], "countries" => [ (object)[ "id" => 1, "name" => "The Netherlands" ], .. ] ] );
在 Ajax 代理中填充价格字段
$peggyPay->response->ajaxProxy(
true,
[
// See further this document for specifications for price and discount fields
"discount" => [ new \PeggyForms\Classes\DiscountItem... ]
]
);
POST 提交操作
此示例对 POST 提交操作做出响应。提交哈希将始终作为 'submissionHash' 添加。
您可以在 Peggy Pay 编辑器中通过编写 {POST:data.StatusMessage}
在感谢页面或电子邮件正文中使用您的自定义属性,例如在 [表单] => [感谢] 正文: 阅读更多
$submissionHash = $peggyPay->get->param("submissionHash"); $field1 = $peggyPay->get->param("field1"); // ... $statusMessage = your_function($field1); // This example function should return a string with a message $peggyPay->response->post( // Call succeded? true, // Message to show when call failed, $statusMessage, // Properties to pass back to your page, to use in your thanks page or email body using {POST:myprop} in this example [ "myprop" => 100 ], // Optional you can change the thankspage to an redirect $peggyPay->post->returnAction( \PeggForms\Modules\Post::ReturnActionRedirect, "https://www.google.nl" ), // And use some data in the CSV export [ $peggyPay->response->exportColumn("uniqueColumnKey", "Column label", $yourValueForExport ) ] );
填充价格字段
价格字段用于以非常灵活的方式收集表单中的金额。使用动态数据,您也可以通过 HTTP 请求通过您的 Web 服务收集金额。
请查看价格字段设置的截图
// Currency is always passed by Peggy Pay, USD / EUR supported by now $currency = $peggyPay->get->param("currency", "EUR"); // Optioanlly get some params from Peggy Pay $amount = (int)$peggyPay->get->param("amount", 1); // Calculate the amount with your own functions $price = my_function($amount); // Price should be an integer representing cents $price2 = my_function_2($amount); // Price should be an integer representing cents $peggyPay->response->priceField( true, [ new \PeggyForms\Classes\PriceItem("My dynamic item", $price, $amount, $currency, "Id"), new \PeggyForms\Classes\PriceItem("Administration costs", $price2, 1, $currency, "AdminCosts") ] );
如果可能的话,强烈建议填写 Id 参数。
要填充折扣字段,请使用
$peggyPay->response->priceField( true, [ new \PeggyForms\Classes\DiscountItem("My dynamic item", $price, $amount, $currency), ] );