I am developing a webapp using rails 3.2. I am trying to build a callback method for Paymill that will be used to catch Transaction success events.
This is my controller. It´s very simple and it works fine when I make a simulated POST request using either CURL or a Firefox plugin and using the JSON body that Paymill documentation states is the correct one.
Unfortunately when using Paymills own test script (made in PHP) it failes.
class Admin::TransactionsController < ActionController::Base
def create
transaction = Transaction.new
transaction.client_id = params[:event][:event_resource][:client][:id]
transaction.trans_id = params[:event][:event_resource][:id]
transaction.amount = params[:event][:event_resource][:amount]
transaction.currency = params[:event][:event_resource][:currency]
transaction.save
render :nothing => true
end
end
This is the JSON body that the documentation says it will attach to callback event:
{
"event": {
"event_type": "transaction.succeeded",
"event_resource": {
"id": "tran_d06aae1df72af329c39367cde9ed19",
"amount": "12345",
"origin_amount": 12345,
"status": "closed",
"description": "test paymill",
"livemode": false,
"refunds": null,
"currency": "USD",
"created_at": 1375457696,
"updated_at": 1375457696,
"response_code": 20000,
"short_id": null,
"is_fraud": false,
"invoices": [],
"app_id": null,
"fees": [],
"payment": {
"id": "pay_46ee3be859eedb72d325adcb6",
"type": "creditcard",
"client": "client_d94e1e3efbc59073629df",
"card_type": "visa",
"country": null,
"expire_month": "1",
"expire_year": "2014",
"card_holder": null,
"last4": "1111",
"created_at": 1375457695,
"updated_at": 1375457696,
"app_id": null
},
"client": {
"id": "client_d941e3efbc59e073629df",
"email": null,
"description": null,
"created_at": 1375457696,
"updated_at": 1375457696,
"app_id": null,
"payment": [],
"subscription": null
},
"preauthorization": null
}
}
}
When making the request with the PHP test script the body arrives in this shape and Rails throughs errors:
string(1712) ""{\n \"event\": {\n \"event_type\": \"transaction.succeeded\",\n \"event_resource\": {\n \"id\": \"tran_d06aae1f72af329c39367cde9ed19\",\n \"amount\": \"12345\",\n \"origin_amount\": 12345,\n \"status\": \"closed\",\n \"description\": \"test paymill\",\n \"livemode\": false,\n \"refunds\": null,\n \"currency\": \"USD\",\n \"created_at\": 1375457696,\n \"updated_at\": 1375457696,\n \"response_code\": 20000,\n \"short_id\": null,\n \"is_fraud\": false,\n \"invoices\": [],\n \"app_id\": null,\n \"fees\": [],\n \"payment\": {\n \"id\": \"pay_46ee3b85f9eedb72d325adcb6\",\n \"type\": \"creditcard\",\n \"client\": \"client_d941e3effbc59073629df\",\n \"card_type\": \"visa\",\n \"country\": null,\n \"expire_month\": \"1\",\n \"expire_year\": \"2014\",\n \"card_holder\": null,\n \"last4\": \"1111\",\n \"created_at\": 1375457695,\n \"updated_at\": 1375457696,\n \"app_id\": null\n },\n \"client\": {\n \"id\": \"client_d941e3efbc59073629df\",\n \"email\": null,\n \"description\": null,\n \"created_at\": 1375457696,\n \"updated_at\": 1375457696,\n \"app_id\": null,\n \"payment\": [],\n \"subscription\": null\n },\n \"preauthorization\": null\n }\n }\n}""
The error I get from Rails is this now:
Error occurred while parsing request parameters
NoMethodError - undefined method `each' for #<String:0x007fc820a1fc08>:
What is going on? It´s like Rails treats this body like faulty text? If I remove this line from the PHP (and thus does not JSON encode the body before sending) it works fine and I get no errors. And it doesn´t matter if I the create method is empty. Something is happening before this method gets called?
json_encode($http_body);
Thankful for all help! I will soon start to cry.
Where did you get the test script from? The one I have here, does contain the body in plain text and so it does not need json encoding anymore, this should work (replace <YOUR_TARGET_URL> with your URL):