// Set up Spreedly object
// Third parameter, signing secret, is only required for hosted payment pages / 3D Secure
// Fourth parameter, response format, defaults to xml if not provided, can be set to 'json'
$sly = new Spreedly('YOUR_ENVIRONMENT_KEY', 'YOUR_ACCESS_SECRET', 'YOUR_SIGNING_SECRET', 'RESPONSE_FORMAT');
The default response format is xml, that can either be changed in the constructor (above) or changed at any point
echo $sly->get_response_format();
$sly->set_response_format('json');
echo $sly->get_response_format();
xml
json
Occasionally Spreedly provide a different URL for testing purposes.
echo $sly->get_base_url();
$sly->set_base_url('https://core-test.spreedly.com/v1');
echo $sly->get_base_url();
https://core.spreedly.com/v1
https://core-test.spreedly.com/v1
List supported gateways, including configuration settings
$result = $sly->list_supported_gateways();
foreach($resut->gateway as $g) {
echo $g->gateway_name . "(" . $g->gateway_type . ")<br />";
}
Return the details about a specific supported gateway
$result = $sly->show_supported_gateway('authorize_net');
$print_r($result);
https://docs.spreedly.com/gateways/adding
Spreedly test gateway:
$result = $sly->create_gateway('test');
echo $new_gateway->gateway->token;
Real example:
$result = $sly->create_gateway(
'authorize_net',
array(
'login' => 'YOUR_AUTHORIZE_NET_API_LOGIN',
'password' => 'YOUR_AUTHORIZE_NET_TRANSACTION_KEY'
)
);
echo $new_gateway->gateway->token;
You can’t update a gateway’s type, but you can update its credentials
https://docs.spreedly.com/gateways/updating
$result = $sly->update_gateway(
'GATEWAY_TOKEN',
array(
'login' => 'YOUR_AUTHORIZE_NET_API_LOGIN',
'password' => 'YOUR_AUTHORIZE_NET_TRANSACTION_KEY'
)
);
echo $result->message;
Gateways can't be deleted, only redacted (disabled, credentials removed)
https://docs.spreedly.com/gateways/redacting
$result = $sly->redact_gateway('GATEWAY_TOKEN');
echo $result->message;
https://docs.spreedly.com/gateways/getting#getting-all-gateways
$result = $sly->list_gateways();
foreach( $result->gateway as $gateway )
{
echo "(" . $gateway->name . ") ";
echo $gateway->token;
}
https://docs.spreedly.com/gateways/getting#getting-one-gateway
$result = $sly->show_gateway('GATEWAY_TOKEN');
print_r($result);
Payment methods include credit cards, bank accounts etc. When payment methods are captured in Spreedly (perhaps via a payment form) they are assigned a unique token for subsequent purchase/authorization.
Payment methods captured via transparent redirect are only held temporarily, use "Retain" if you need to keep them active.
https://docs.spreedly.com/payment-methods/storing#retaining-a-payment-method
$result = $sly->retain('PAYMENT_METHOD_TOKEN');
print $result->message;
Rather than delete a payment method, in Core you “redact” it, removing all sensitive information but leaving a place for any transactions to hang off of.
https://docs.spreedly.com/payment-methods/storing#redacting-a-payment-method
$result = $sly->redact('PAYMENT_METHOD_TOKEN');
print $result->message;
Most of the time, simply redacting a payment method will suffice because payment methods are for the most part only stored in Spreedly. There are times though when a payment method is stored on the gateway and you’d like to notify the gateway that it can no longer be used.
https://docs.spreedly.com/payment-methods/storing#removing-a-payment-method-from-a-gateway
$result = $sly->remove_from_gateway('PAYMENT_METHOD_TOKEN', 'GATEWAY_TOKEN');
print $result->message;
Get full details of a payment method.
https://docs.spreedly.com/reference/api/v1/payment_methods/show/
$payment_method = show_payment_method('PAYMENT_METHOD_TOKEN');
print_r($payment_method);
Get the list of transactions made on a payment method, across all gateways on an environment. Will include internal Spreedly transactions such as adding and redacting the payment method, in addition to things like purchases.
https://docs.spreedly.com/reference/api/v1/payment_methods/transactions/
$transactions = list_payment_method_transactions('PAYMENT_METHOD_TOKEN');
print_r($transactions);
A purchase call immediately takes funds from the payment method (assuming the transaction succeeds).
https://docs.spreedly.com/payment-methods/using#purchase
$transaction = $sly->purchase(
'PAYMENT_METHOD_TOKEN',
array(
'amount' => 100,
'ip' => '127.0.0.1'
....
));
print_r($transaction);
An authorize works just like a purchase; the difference being that it doesn’t actually take the funds.
https://docs.spreedly.com/payment-methods/using#authorize
$transaction = $sly->authorize(
'PAYMENT_METHOD_TOKEN',
array(
'amount' => 100,
'ip' => '127.0.0.1'
....
));
echo $transaction->token;
A capture will actually take the funds previously reserved via an authorization.
https://docs.spreedly.com/payment-methods/using#capture
$transaction = $sly->capture('TRANSACTION_TOKEN');
print_r($transaction);
Optionally specify an amount - less than the original authorization - to capture.
$transaction = $sly->capture('TRANSACTION_TOKEN', 50);
print_r($transaction);
Or more detailed transaction information.
$transaction = $sly->capture(
'TRANSACTION_TOKEN',
array(
'order_id' => 'ABC123',
'amount' => 50
));
print_r($transaction);
Void is used to cancel out authorizations and, with some gateways, to cancel actual payment transactions within the first 24 hours (credits are used after that; see below).
https://docs.spreedly.com/payment-methods/using#void
$transaction = $sly->void('TRANSACTION_TOKEN');
print_r($transaction);
Or provide more detailed transaction information.
$transaction = $sly->void(
'TRANSACTION_TOKEN',
array(
'order_id' => 'ABC123'
));
print_r($transaction);
A credit is like a void, except it actually reverses a charge instead of just canceling a charge that hasn’t yet been made. It’s a refund.
https://docs.spreedly.com/payment-methods/using#credit
$transaction = $sly->credit('TRANSACTION_TOKEN');
print_r($transaction);
Optionally specify an amount to refund.
$transaction = $sly->credit('TRANSACTION_TOKEN', 50);
print_r($transaction);
Or more detailed transaction information.
$transaction = $sly->credit(
'TRANSACTION_TOKEN',
array(
'order_id' => 'ABC123',
'amount' => 50
));
print_r($transaction);
Complete a transaction.
$result = $sly->complete_transaction('TRANSACTION_TOKEN');
List the transactions on a gateway, paginated, default ordering is oldest first.
https://docs.spreedly.com/reference/api/v1/gateways/transactions/
$transactions = $sly->list_transactions('GATEWAY_TOKEN');
Also supports changing the ordering to view the most recent first, plus pagination by providing the "SINCE_TOKEN":
$transactions = $sly->list_transactions('GATEWAY_TOKEN', 'desc', 'SINCE_TOKEN');
View details of a single gateway transaction.
https://docs.spreedly.com/reference/api/v1/receivers/show/
$transaction = $sly->show_transaction('TRANSACTION_TOKEN');
This API call allows you to see the full conversation Spreedly had with the payment gateway for a given transaction. You can see exactly what was sent to the gateway and exactly how the gateway responded.
Unlike the other methods in the Spreedly API this returns a plain text log rather than XML.
https://docs.spreedly.com/reference/api/v1/transactions/transcript/
$transcript = $sly->show_transcript('TRANSACTION_TOKEN');
echo $transcript;
For 3D Secure 1 Spreedly can post a callback XML
https://docs.spreedly.com/guides/offsite-payments/signed-requests/
$xml = '<transactions>
<transaction>
<amount type="integer">100</amount>
<on_test_gateway type="boolean">false</on_test_gateway>
<created_at type="datetime">2012-09-10T20:35:10Z</created_at>
<updated_at type="datetime">2012-09-10T20:35:11Z</updated_at>
<currency_code>USD</currency_code>
<succeeded type="boolean">true</succeeded>
<state>succeeded</state>
<token>5AG4P7FPjlfIA6aED6AgZvUEehx</token>
<transaction_type>OffsitePurchase</transaction_type>
<order_id nil="true"></order_id>
<ip nil="true"></ip>
<callback_url>http://example.com/handle_callback</callback_url>
<signed>
<signature>b81436daf0d695404c5bf7a2aecf049d460bb6e1</signature>
<fields>amount callback_url created_at currency_code ip on_test_gateway order_id state succeeded token transaction_type updated_at</fields>
<algorithm>sha1</algorithm>
</signed>
</transaction>
</transactions>';
$simplexml = simplexml_load_string($xml);
if( $sly->verify_transaction_signature($simplexml->transaction) ) {
echo "Signature valid";
} else {
echo "Signature not valid";
}