0

I have integrated the Wepay payment gateway. But I have facing a problem to pass json object to wepay. It always shows a incorrect json format. Please look at the below code.

$forca_a = array(
  'debit_opt_in'=>true
);
$forca = json_encode($forca_a,JSON_FORCE_OBJECT);
$wepay_create_array = array(
  'name' =>"xxxx",
  'description' => "xxxxxxxxx xxxx",
  'callback_uri' => "xxxxxxx",
  'country' => "CA",
  'currencies' => array('CAD'),
  'country_options' => $forca,
  'rbits'=> array(
               array(
                  'receive_time'=>strtotime("now"),
                  'type' =>'website_uri',
                  'source' => 'partner_database',
                  'properties'=> array('uri'=>xxxxx)
               )
            )
);

If I won't pass the country_options, its seems to working but If I pass this parameter, it always give me an error says "Incorrect JSON format".

I sent an email to wepay help center. They told me that, you are passing the string "country_options":"{"debit_opt_in":true}" <--- this is a string Instead of "country_options":{"debit_opt_in":true} <--- this is a JSON object. So I'm confused. I have no idea how do I pass the JSON object. There is only way and is json_encode($object).

Vinoth Krishnan
  • 2,925
  • 6
  • 29
  • 34
Rahul Patel
  • 81
  • 2
  • 10

2 Answers2

0

hey use below code to get proper json

<?php
$forca_a = array(
                  'debit_opt_in'=>true
           );
    // $forca = json_encode($forca_a);
    $wepay_create_array = array(
                  'name' =>"xxxx",
                  'description' => "xxxxxxxxx xxxx",
                  'callback_uri' => "xxxxxxx",
                  'country' => "CA",
                  'currencies' => array('CAD'),
                  'country_options' => $forca_a,
                  'rbits'=> array(
                               array(
                                  'receive_time'=>strtotime("now"),
                                  'type' =>'website_uri',
                                  'source' => 'partner_database',
                                  'properties'=> array('uri'=>'xxxxx')
                               )
                            )
                  );


print_r(json_encode($wepay_create_array));
                  ?>

this code will give following json output

{
        "name": "xxxx",
        "description": "xxxxxxxxx xxxx",
        "callback_uri": "xxxxxxx",
        "country": "CA",
        "currencies": ["CAD"],
        "country_options": {
            "debit_opt_in": true
        },
        "rbits": [{
            "receive_time": 1461561030,
            "type": "website_uri",
            "source": "partner_database",
            "properties": {
                "uri": "xxxxx"
            }
        }]
    }
ram singh
  • 117
  • 5
0

You have no need to make:

$forca = json_encode($forca_a,JSON_FORCE_OBJECT);

before you put it to $wepay_create_array. Before sending request, i think, you make json_encode($wepay_create_array), and yes, after that you will have 'string' for country_options key.

  • https://www.wepay.com/developer/reference/account#create. Please look this link. I just need to pass 'country_options' into json object, not the whole array. – Rahul Patel Apr 25 '16 at 06:30
  • See the difference between jsonObject and jsonArray in this answer: http://stackoverflow.com/a/16145788/5686207 You just have no need json_encode($forca) before pass it to the main request array. – Sergey Kovtun Apr 25 '16 at 07:10
  • Thank you anyway but I just passed the array and it seems to be working. Strange! In the above my code I just removed the json_encode and pass the array of the country_options and it's working for me. May be something coded in the libraries. By the way thanks for valuable effort. – Rahul Patel Apr 25 '16 at 07:58