Make a Purchase

Once you have a gateway provisioned for your environment, and have collected payment information from your users, you can begin making purchases and other transactions.

Create purchase transaction

Submitting the payment form to Spreedly doesn’t do anything except store that payment method - regardless of whether it’s valid or not - for later usage. Now that you have a token representing the payment method in hand, you can actually run a purchase against a specific gateway from your authenticated server environment (where you can safely store and access your Spreedly environment’s access key).

Using the token of the gateway you want to transact against, as well as the token of the payment method you want to charge, you can create a purchase by POSTing to the Spreedly API endpoint at: https://core.spreedly.com/v1/gateways/<gateway_token>/purchase.<format>

$ curl https://core.spreedly.com/v1/gateways/LlkjmEk0xNkcWrNixXa1fvNoTP4/purchase.json \
  -u 'Ll6fAtoVSTyVMlJEmtpoJV8S:RKOCG5D8D3fZxDSg504D0IxU2XD4Io5VXmyzdCtTivHFTTSy' \
  -H 'Content-Type: application/json' \
  -d '{
        "transaction": {
          "payment_method_token": "56wyNnSmuA6CWYP7w0MiYCVIbW6",
          "amount": 100,
          "currency_code": "USD",
          "retain_on_success": true
        }
      }'

$ curl https://core.spreedly.com/v1/gateways/LlkjmEk0xNkcWrNixXa1fvNoTP4/purchase.xml \
  -u 'Ll6fAtoVSTyVMlJEmtpoJV8S:RKOCG5D8D3fZxDSg504D0IxU2XD4Io5VXmyzdCtTivHFTTSy' \
  -H 'Content-Type: application/xml' \
  -d '<transaction>
        <payment_method_token>56wyNnSmuA6CWYP7w0MiYCVIbW6</payment_method_token>
        <amount>100</amount>
        <currency_code>USD</currency_code>
        <retain_on_success>true</retain_on_success>
      </transaction>'

The amount is always given as an integer number of cents. So if you’d like to run a purchase of $83.45, you’d pass an amount of 8345.

Running that will give you the following response:

{
  "transaction": {
    "on_test_gateway": true,
    "created_at": "2016-08-12T15:04:58Z",
    "updated_at": "2016-08-12T15:04:58Z",
    "succeeded": true,
    "state": "succeeded",
    "token": "CtxOBoDCceaA93IMFTri3UYHiMt",
    "transaction_type": "Purchase",
    "order_id": null,
    "ip": null,
    "description": null,
    "email": null,
    "merchant_name_descriptor": null,
    "merchant_location_descriptor": null,
    "gateway_specific_fields": null,
    "gateway_specific_response_fields": {
    },
    "gateway_transaction_id": "64",
    "amount": 100,
    "currency_code": "USD",
    "retain_on_success": true,
    "payment_method_added": false,
    "message_key": "messages.transaction_succeeded",
    "message": "Succeeded!",
    "gateway_token": "8dvNMSuDr3kTlMIQxbkIaE5KNlX",
    "response": {
      "success": true,
      "message": "Successful purchase",
      "avs_code": null,
      "avs_message": null,
      "cvv_code": null,
      "cvv_message": null,
      "pending": false,
      "result_unknown": false,
      "error_code": "",
      "error_detail": null,
      "cancelled": false,
      "fraud_review": null,
      "created_at": "2016-08-12T15:04:58Z",
      "updated_at": "2016-08-12T15:04:58Z"
    },
    "shipping_address": {
      "name": "Newfirst Newlast",
      "address1": null,
      "address2": null,
      "city": null,
      "state": null,
      "zip": null,
      "country": null,
      "phone_number": null
    },
    "api_urls": [
      {
        "referencing_transaction": [

        ]
      }
    ],
    "payment_method": {
      "token": "UAwoPyWJhwZiYe2ZY1ri6ENAjLG",
      "created_at": "2016-08-11T17:09:30Z",
      "updated_at": "2016-08-11T18:02:50Z",
      "email": null,
      "data": null,
      "storage_state": "retained",
      "test": true,
      "last_four_digits": "1111",
      "first_six_digits": "411111",
      "card_type": "visa",
      "first_name": "Newfirst",
      "last_name": "Newlast",
      "month": 4,
      "year": 2020,
      "address1": null,
      "address2": null,
      "city": null,
      "state": null,
      "zip": null,
      "country": null,
      "phone_number": null,
      "company": null,
      "full_name": "Newfirst Newlast",
      "eligible_for_card_updater": null,
      "shipping_address1": null,
      "shipping_address2": null,
      "shipping_city": null,
      "shipping_state": null,
      "shipping_zip": null,
      "shipping_country": null,
      "shipping_phone_number": null,
      "payment_method_type": "credit_card",
      "errors": [

      ],
      "fingerprint": "b0a45066121c1f1a49b3a6ffc26caf2995db",
      "verification_value": "",
      "number": "XXXX-XXXX-XXXX-1111"
    }
  }
}
<transaction>
  <on_test_gateway type="boolean">true</on_test_gateway>
  <created_at type="dateTime">2016-08-12T15:04:59Z</created_at>
  <updated_at type="dateTime">2016-08-12T15:04:59Z</updated_at>
  <succeeded type="boolean">true</succeeded>
  <state>succeeded</state>
  <token>5sbNnav4HVEUhhcX69klGGDNWNy</token>
  <transaction_type>Purchase</transaction_type>
  <order_id nil="true"></order_id>
  <ip nil="true"></ip>
  <description nil="true"></description>
  <email nil="true"></email>
  <merchant_name_descriptor nil="true"></merchant_name_descriptor>
  <merchant_location_descriptor nil="true"></merchant_location_descriptor>
  <gateway_specific_fields nil="true"></gateway_specific_fields>
  <gateway_specific_response_fields>
  </gateway_specific_response_fields>
  <gateway_transaction_id>45</gateway_transaction_id>
  <amount type="integer">100</amount>
  <currency_code>USD</currency_code>
  <retain_on_success type="boolean">true</retain_on_success>
  <payment_method_added type="boolean">false</payment_method_added>
  <message key="messages.transaction_succeeded">Succeeded!</message>
  <gateway_token>8dvNMSuDr3kTlMIQxbkIaE5KNlX</gateway_token>
  <shipping_address>
    <name>Newfirst Newlast</name>
    <address1 nil="true"></address1>
    <address2 nil="true"></address2>
    <city nil="true"></city>
    <state nil="true"></state>
    <zip nil="true"></zip>
    <country nil="true"></country>
    <phone_number nil="true"></phone_number>
  </shipping_address>
  <response>
    <success type="boolean">true</success>
    <message>Successful purchase</message>
    <avs_code nil="true"></avs_code>
    <avs_message nil="true"></avs_message>
    <cvv_code nil="true"></cvv_code>
    <cvv_message nil="true"></cvv_message>
    <pending type="boolean">false</pending>
    <result_unknown type="boolean">false</result_unknown>
    <error_code></error_code>
    <error_detail nil="true"></error_detail>
    <cancelled type="boolean">false</cancelled>
    <fraud_review nil="true"></fraud_review>
    <created_at type="dateTime">2016-08-12T15:04:59Z</created_at>
    <updated_at type="dateTime">2016-08-12T15:04:59Z</updated_at>
  </response>
  <api_urls>
  </api_urls>
  <payment_method>
    <token>UAwoPyWJhwZiYe2ZY1ri6ENAjLG</token>
    <created_at type="dateTime">2016-08-11T17:09:30Z</created_at>
    <updated_at type="dateTime">2016-08-12T15:04:58Z</updated_at>
    <email nil="true"></email>
    <data nil="true"></data>
    <storage_state>retained</storage_state>
    <test type="boolean">true</test>
    <last_four_digits>1111</last_four_digits>
    <first_six_digits>411111</first_six_digits>
    <card_type>visa</card_type>
    <first_name>Newfirst</first_name>
    <last_name>Newlast</last_name>
    <month type="integer">4</month>
    <year type="integer">2020</year>
    <address1 nil="true"></address1>
    <address2 nil="true"></address2>
    <city nil="true"></city>
    <state nil="true"></state>
    <zip nil="true"></zip>
    <country nil="true"></country>
    <phone_number nil="true"></phone_number>
    <company nil="true"></company>
    <full_name>Newfirst Newlast</full_name>
    <eligible_for_card_updater nil="true"></eligible_for_card_updater>
    <shipping_address1 nil="true"></shipping_address1>
    <shipping_address2 nil="true"></shipping_address2>
    <shipping_city nil="true"></shipping_city>
    <shipping_state nil="true"></shipping_state>
    <shipping_zip nil="true"></shipping_zip>
    <shipping_country nil="true"></shipping_country>
    <shipping_phone_number nil="true"></shipping_phone_number>
    <payment_method_type>credit_card</payment_method_type>
    <errors>
    </errors>
    <verification_value></verification_value>
    <number>XXXX-XXXX-XXXX-1111</number>
    <fingerprint>b0a45066121c1f1a49b3a6ffc26caf2995db</fingerprint>
  </payment_method>
</transaction>

The succeeded element will indicate if the transaction was successful, with the message element providing further details if it was not. Once you have a successful transation, you can show a success page to your customer - you’ve successfully collected payment!

You can also execute other transactions against the gateway with your collected payment information, such as an authorize, capture, void or refund. See the reference docs for additional details.

Retain

By default, when a payment method is added to Spreedly it is placed in a “cached” storage state. This state indicates that, unless told otherwise, the card will be purged from Spreedly after a short period of time (several minutes, not hours). If you are performing a one-time transaction where the workflow is 1) collect payment information 2) immediately charge the card then you don’t need to do anything else. However, if you want to charge the card again in the future, or have a longer period of time between collecting the payment information and executing a purchase, then you will need to “retain” the card.

Since it’s such a common workflow to want to keep valid cards around for future purchases, Spreedly provides a retain_on_success parameter that can be used to automatically retain the card when it was used in a successful transaction. In the example purchase call above, you did just that, setting the retain element to be true.

If you don’t want to automatically retain a card when it’s used in a transaction, you can omit the retain_on_success parameter

Redact

When you no longer need to store a card and want to remove it from Spreedly, you can redact it. This will wipe all sensitive information from the payment method and make the card inoperable (and it will no longer count towards your plan’s storage limits).

Next

You should now be successfully integrated with Spreedly, having created a gateway, collected payment information and executed a purchase. If you would like to explore more advanced topics, we recommend the following: