PHP code example of peggyforms / php-sdk

1. Go to this page and download the library: Download peggyforms/php-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

peggyforms / php-sdk example snippets




$peggyPay = new PeggyForms\Api("myApiKey");

// 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;

// Get the HTTP request param
$hash = $peggyPay->get->param("peggyHash");

// And get the order
$order = $peggyPay->orders->getBySubmissionHash($hash);

PeggyForms\Classes\Order Object
(
    [PaymentType] => onetime
    ...
    [Amount] => 2198
    [AmountEx] => 1817
    [AmountVat] => 381
    [VatAmount] => 21
    [IsIncVat] => 1
    [IsVatShifted] => 0
    [Description] => SDK get order
    ...
    [OrderLines] => Array
        (
            [0] => PeggyForms\Classes\OrderLine Object
                (
                    [Description] => Clone bug
                    [Quantity] => 1
                    [Amount] => 1999
                    [AmountEx] => 1652.07
                    [AmountTax] => 346.93
                    [Tax] => 21
                    [IdTax] => 2
                    [IsTaxFree] => 0
                )
                ...
        )
)

$peggyPay->response->webhook(true);

$peggyPay->response->choiceField(
	true, // Call succeded?
	[
		new \PeggyForms\Classes\ListItem(1, "My dynamic option 1"),
		new \PeggyForms\Classes\ListItem(2, "My dynamic option 2")
	]
);

// 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
);

$peggyPay->response->ajaxProxy(
	true,
	[
		"products" => [
			(object)[
				"id" => 1,
				"name" => "My product 1"
			],
			..
		],
		"countries" => [
			(object)[
				"id" => 1,
				"name" => "The Netherlands"
			],
			..
		]
	]
);


$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 ) ]
);

// 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")
	]
);


$peggyPay->response->priceField(
	true,
	[
		new \PeggyForms\Classes\DiscountItem("My dynamic item", $price, $amount, $currency),
	]
);