Skip to content

Instantly share code, notes, and snippets.

@javidjasmine
Last active February 16, 2021 17:20
Show Gist options
  • Save javidjasmine/6ec2d4fa89bc851bcd6f7d02a6bd669f to your computer and use it in GitHub Desktop.
Save javidjasmine/6ec2d4fa89bc851bcd6f7d02a6bd669f to your computer and use it in GitHub Desktop.
Guidance on using a Spreedly receiver to "duplicate" a payment method

In this doc, "source" indicates the customer/merchant that is controlling the user experience and is receiving the payment method data from the customer. "Target" is the partner that wishes to receive this payment method from the source. If you wish to use this method to essentially copy a payment method within your environment, you will act as both the "source" and the partner".

Note: this example uses XML, but the JSON API is recommended.

$ curl https://core.spreedly.com/v1/receivers.xml \
  -u '[SOURCE-ENV-KEY]:[SOURCE-SECRET]' \
  -H 'Content-Type: application/xml' \
  -d '<receiver>
        <receiver_type>spreedly</receiver_type>
      </receiver>'

<receiver>
  <receiver_type>spreedly</receiver_type>
  <token>dl1KRyHtvvfMgPI9gAm6gUd85nG</token>
  <hostnames>https://core.spreedly.com</hostnames>
  <state>retained</state>
  <created_at>2014-09-11T17:45:58Z</created_at>
  <updated_at>2014-09-11T17:45:58Z</updated_at>
</receiver>

Source delivers payment method to target Spreedly environment

Note the url field includes the environment key of the target's destination Spreedly environment. Again, for the purposes of copying a payment method within the same environment you'll mirror the original environment key used.

$ curl https://core.spreedly.com/v1/receivers/dl1KRyHtvvfMgPI9gAm6gUd85nG/deliver.xml \
  -u '[SOURCE-ENV-KEY]:[SOURCE-SECRET]' \
  -H 'Content-Type: application/xml' \
  -d '<delivery>
        <payment_method_token>token</payment_method_token>
        <url>https://core.spreedly.com/v1/payment_methods.json?environment_key=[TARGET-ENV-KEY]</url>
        <headers><![CDATA[Content-Type: application/json]]></headers>
        <body><![CDATA[{
  "payment_method":{
    "credit_card":{
      "first_name": "{{credit_card_first_name}}",
      "last_name": "{{credit_card_last_name}}",
      "number":"{{credit_card_number}}",
      "verification_value": "{{credit_card_verification_value}}",
      "month":"{{credit_card_month}}",
      "year":"{{credit_card_year}}"
    }
  }
}]]></body>
      </delivery>'

<transaction>
  <token>JK5OQV2FsCf3t7Wo16VxouUZk1A</token>
  <transaction_type>DeliverPaymentMethod</transaction_type>
  <state>succeeded</state>
  <created_at>2014-09-11T20:42:03Z</created_at>
  <updated_at>2014-09-11T20:42:03Z</updated_at>
  <succeeded>true</succeeded>
  <message>Succeeded!</message>
  <response>
    <status>201</status>
    <headers>...</headers>
    <body>
      <![CDATA[{"transaction":{"token":"OZF5Gs7r2WSFwxb7Jf7dq5kqefd","created_at":"2014-09-11T20:42:03Z","updated_at":"2014-09-11T20:42:03Z","succeeded":true,"transaction_type":"AddPaymentMethod","retained":false,"message":"Succeeded!","message_key":"messages.transaction_succeeded","payment_method":{"token":"EslhzCK4M4mOFd3pzm4GrsB1caj","data":null,"storage_state":"cached","email":null,"created_at":"2014-09-11T20:42:03Z","updated_at":"2014-09-11T20:42:03Z","errors":[],"payment_method_type":"credit_card","first_name":"Joe","last_name":"Jones","full_name":"Joe Jones","card_type":"master","last_four_digits":"4444","first_six_digits":"555555","month":3,"year":2032,"address1":null,"address2":null,"city":null,"state":null,"zip":null,"country":null,"phone_number":null,"eligible_for_card_updater":true,"verification_value":"","number":"XXXX-XXXX-XXXX-4444"}}}]]>
    </body>
  </response>
</transaction>

Source will need to parse the body of the response here to get at target's payment method token (OZF5Gs7r2WSFwxb7Jf7dq5kqefd), which it can then pass on to target via a direct API between the two systems.

At this point you can call "retain" on the resulting payment method token returned in the response of the deliver call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment