0

I am working with the stripe payment system, and essentially what happens is that after a charge is made, I output the json value, and where i would like to store the id in order to capture the charge.

Below is how the charge is conducted

try {
 $charge = \Stripe\Charge::create(array(
      'customer' => $customer->id,
      'amount'   => $amount,
      'currency' => 'cad',
            'capture' => 'false',

      'description'=>  $courseTitle

  ));

 echo $charge;
} catch(\Stripe\Error\Card $e) {
  // The card has been declined
  $stripChargeValid = false;
  echo "failed";

}

below is what is echo

Stripe\Charge JSON: { "id": "ch_15usNKIOMQSe8X844GzjEpyM", "object": "charge", "created": 1429826450, "livemode": false, "paid": true, "status": "succeeded", "amount": 41000, "currency": "cad", "refunded": false, "source": { "id": "card_15usMsIOMQSe8X84foA2Qb69", "object": "card", "last4": "4242", "brand": "Visa", "funding": "credit", "exp_month": 1, "exp_year": 2016, "fingerprint": "GLlDGRtbDBjmZDdb", "country": "US", "name": null, "address_line1": null, "address_line2": null, "address_city": null, "address_state": null, "address_zip": null, "address_country": null, "cvc_check": "pass", "address_line1_check": null, "address_zip_check": null, "dynamic_last4": null, "metadata": [], "customer": "cus_676avKg8aDycIL" }, "captured": false, "balance_transaction": null, "failure_message": null, "failure_code": null, "amount_refunded": 0, "customer": "cus_676avKg8aDycIL", "invoice": null, "description": "Microsoft Office 2013: Transition from Office 2007\/2010", "dispute": null, "metadata": [], "statement_descriptor": null, "fraud_details": [], "receipt_email": "jonathanetienne@live.com", "receipt_number": null, "shipping": null, "application_fee": null, "refunds": { "object": "list", "total_count": 0, "has_more": false, "url": "\/v1\/charges\/ch_15usNKIOMQSe8X844GzjEpyM\/refunds", "data": [] } }

I would like to grab the id value so i can post it into a mysqli query

Update:

object(Stripe\Charge)#25 (5) {
  ["_opts":protected]=>
  object(Stripe\Util\RequestOptions)#22 (2) {
    ["headers"]=>
    array(0) {
    }
    ["apiKey"]=>
    string(32) ""
  }
  ["_values":protected]=>
  array(27) {
    ["id"]=>
    string(27) "ch_15uu0dIOMQSe8X84gJIVis9y"
    ["object"]=>
    string(6) "charge"
    ["created"]=>
    int(1429832731)
    ["livemode"]=>
    bool(false)
    ["paid"]=>
    bool(true)
    ["status"]=>
    string(9) "succeeded"
    ["amount"]=>
    int(338400)
    ["currency"]=>
    string(3) "cad"
    ["refunded"]=>
    bool(false)
    ["source"]=>
    object(Stripe\Card)#26 (5) {
      ["_opts":protected]=>
      object(Stripe\Util\RequestOptions)#22 (2) {
        ["headers"]=>
        array(0) {
        }
        ["apiKey"]=>
        string(32) ""
      }
      ["_values":protected]=>
      array(22) {
        ["id"]=>
        string(29) "card_15utkNIOMQSe8X845N5xbl7y"
        ["object"]=>
        string(4) "card"
        ["last4"]=>
        string(4) "4242"
        ["brand"]=>
        string(4) "Visa"
        ["funding"]=>
        string(6) "credit"
        ["exp_month"]=>
        int(1)
        ["exp_year"]=>
        int(2016)
        ["fingerprint"]=>
        string(16) "GLlDGRtbDBjmZDdb"
        ["country"]=>
        string(2) "US"
        ["name"]=>
        NULL
        ["address_line1"]=>
        NULL
        ["address_line2"]=>
        NULL
        ["address_city"]=>
        NULL
        ["address_state"]=>
        NULL
        ["address_zip"]=>
        NULL
        ["address_country"]=>
        NULL
        ["cvc_check"]=>
        NULL
        ["address_line1_check"]=>
        NULL
        ["address_zip_check"]=>
        NULL
        ["dynamic_last4"]=>
        NULL
        ["metadata"]=>
        object(Stripe\AttachedObject)#29 (5) {
          ["_opts":protected]=>
          object(Stripe\Util\RequestOptions)#22 (2) {
            ["headers"]=>
            array(0) {
            }
            ["apiKey"]=>
            string(32) ""
          }
          ["_values":protected]=>
          array(0) {
          }
          ["_unsavedValues":protected]=>
          object(Stripe\Util\Set)#33 (1) {
            ["_elts":"Stripe\Util\Set":private]=>
            array(0) {
            }
          }
          ["_transientValues":protected]=>
          object(Stripe\Util\Set)#34 (1) {
            ["_elts":"Stripe\Util\Set":private]=>
            array(0) {
            }
          }
          ["_retrieveOptions":protected]=>
          array(0) {
          }
        }
        ["customer"]=>
        string(18) "cus_678HJKOahVnt6v"
      }
      ["_unsavedValues":protected]=>
      object(Stripe\Util\Set)#30 (1) {
        ["_elts":"Stripe\Util\Set":private]=>
        array(0) {
        }
      }
      ["_transientValues":protected]=>
      object(Stripe\Util\Set)#31 (1) {
        ["_elts":"Stripe\Util\Set":private]=>
        array(0) {
        }
      }
      ["_retrieveOptions":protected]=>
      array(0) {
      }
    }
    ["captured"]=>
    bool(false)
    ["balance_transaction"]=>
    NULL
    ["failure_message"]=>
    NULL
    ["failure_code"]=>
    NULL
    ["amount_refunded"]=>
    int(0)
    ["customer"]=>
    string(18) "cus_678HJKOahVnt6v"
    ["invoice"]=>
    NULL
    ["description"]=>
    string(39) "PMP Exam Prep Training (PMBOK Guide V5)"
    ["dispute"]=>
    NULL
    ["metadata"]=>
    object(Stripe\AttachedObject)#32 (5) {
      ["_opts":protected]=>
      object(Stripe\Util\RequestOptions)#22 (2) {
        ["headers"]=>
        array(0) {
        }
        ["apiKey"]=>
        string(32) ""
      }
      ["_values":protected]=>
      array(0) {
      }
      ["_unsavedValues":protected]=>
      object(Stripe\Util\Set)#36 (1) {
        ["_elts":"Stripe\Util\Set":private]=>
        array(0) {
        }
      }
      ["_transientValues":protected]=>
      object(Stripe\Util\Set)#37 (1) {
        ["_elts":"Stripe\Util\Set":private]=>
        array(0) {
        }
      }
      ["_retrieveOptions":protected]=>
      array(0) {
      }
    }
    ["statement_descriptor"]=>
    NULL
    ["fraud_details"]=>
    array(0) {
    }
    ["receipt_email"]=>
    string(24) ""
    ["receipt_number"]=>
    NULL
    ["shipping"]=>
    NULL
    ["application_fee"]=>
    NULL
    ["refunds"]=>
    object(Stripe\Collection)#35 (5) {
      ["_opts":protected]=>
      object(Stripe\Util\RequestOptions)#22 (2) {
        ["headers"]=>
        array(0) {
        }
        ["apiKey"]=>
        string(32) ""
      }
      ["_values":protected]=>
      array(5) {
        ["object"]=>
        string(4) "list"
        ["total_count"]=>
        int(0)
        ["has_more"]=>
        bool(false)
        ["url"]=>
        string(47) "/v1/charges/ch_15uu0dIOMQSe8X84gJIVis9y/refunds"
        ["data"]=>
        array(0) {
        }
      }
      ["_unsavedValues":protected]=>
      object(Stripe\Util\Set)#39 (1) {
        ["_elts":"Stripe\Util\Set":private]=>
        array(0) {
        }
      }
      ["_transientValues":protected]=>
      object(Stripe\Util\Set)#40 (1) {
        ["_elts":"Stripe\Util\Set":private]=>
        array(0) {
        }
      }
      ["_retrieveOptions":protected]=>
      array(0) {
      }
    }
  }
  ["_unsavedValues":protected]=>
  object(Stripe\Util\Set)#27 (1) {
    ["_elts":"Stripe\Util\Set":private]=>
    array(0) {
    }
  }
  ["_transientValues":protected]=>
  object(Stripe\Util\Set)#28 (1) {
    ["_elts":"Stripe\Util\Set":private]=>
    array(0) {
    }
  }
  ["_retrieveOptions":protected]=>
  array(0) {
  }
}

Update:

Below is outputs:

$charge_json = $charge->__toJSON(); 
echo "<pre>";
 var_dump($charge_json); 
 echo "</pre>";

string(1677) "{
    "id": "ch_15vDxQIOMQSe8X84NBckjjil",
    "object": "charge",
    "created": 1429909412,
    "livemode": false,
    "paid": true,
    "status": "succeeded",
    "amount": 41000,
    "currency": "cad",
    "refunded": false,
    "source": {
        "id": "card_15vDwlIOMQSe8X843FPN4gAt",
        "object": "card",
        "last4": "4242",
        "brand": "Visa",
        "funding": "credit",
        "exp_month": 1,
        "exp_year": 2016,
        "fingerprint": "GLlDGRtbDBjmZDdb",
        "country": "US",
        "name": "john",
        "address_line1": "",
        "address_line2": null,
        "address_city": "",
        "address_state": "",
        "address_zip": "",
        "address_country": null,
        "cvc_check": "pass",
        "address_line1_check": "pass",
        "address_zip_check": "pass",
        "dynamic_last4": null,
        "metadata": [],
        "customer": "cus_67StWh5kSlXJWN"
    },
    "captured": false,
    "balance_transaction": null,
    "failure_message": null,
    "failure_code": null,
    "amount_refunded": 0,
    "customer": "cus_67StWh5kSlXJWN",
    "invoice": null,
    "description": "",
    "dispute": null,
    "metadata": [],
    "statement_descriptor": null,
    "fraud_details": [],
    "receipt_email": "",
    "receipt_number": null,
    "shipping": null,
    "application_fee": null,
    "refunds": {
        "object": "list",
        "total_count": 0,
        "has_more": false,
        "url": "\/v1\/charges\/ch_15vDxQIOMQSe8X84NBckjjil\/refunds",
        "data": []
    }
}"
code_legend
  • 3,547
  • 15
  • 51
  • 95

2 Answers2

2

Your echo instruction is calling the __toString method from Object. Your charge variable is however a PHPStripeObject.

That being said, I guess you could do the following to be sure to get an array at the end, containing your data:

try {
  $charge = \Stripe\Charge::create(array(
      'customer' => $customer->id,
      'amount'   => $amount,
      'currency' => 'cad',
      'capture' => 'false',
      'description'=>  $courseTitle
   ));
   $chargeArray = \Stripe\Util\Util::convertStripeObjectToArray($charge);
   $id = $chargeArray['id'];
   ...
} catch(\Stripe\Error\Card $e) {
  // The card has been declined
  $stripChargeValid = false;
  echo "failed";
}
Ninir
  • 441
  • 4
  • 9
  • thanks for the detailed explanation. i received the following error Fatal error: Class 'Util\Util' not found – code_legend Apr 23 '15 at 23:02
  • I've edited my answer to use the required class as a use statement (check the first line). This way, you can use the Util:: convertStripeObjectToArray static method – Ninir Apr 23 '15 at 23:06
  • thanks it unfortunately give this result Fatal error: Class 'Util' not found as well – code_legend Apr 23 '15 at 23:11
  • 1
    Check this: `$chargeArray = \Stripe\Util\Util::convertStripeObjectToArray($charge);` – Ninir Apr 23 '15 at 23:15
  • thanks for all your help. that line ran, but then below i get the following error Notice: Undefined index: id in this line $id = $chargeArray['id']; https://stripe.com/docs/api/java#capture_charge – code_legend Apr 23 '15 at 23:22
  • What does a `var_dump($chargeArray);exit;` returns? By the way,, Semsem is right: accessing $charge->id should work regarding the __get() method implemented in the Object class. – Ninir Apr 23 '15 at 23:24
  • it gives array(0) { } – code_legend Apr 23 '15 at 23:32
1

Simply you can deal with $charge as a JSON object.:

$charge->id;

To know its properties and values use:

echo "<pre>";
var_dump($charge);
echo "</pre>";
Darren
  • 13,050
  • 4
  • 41
  • 79
SaidbakR
  • 13,303
  • 20
  • 101
  • 195
  • I get the following error Parse error: syntax error, unexpected 'echo' (T_ECHO) for echo ""; – code_legend Apr 23 '15 at 23:31
  • @user3907211 look for the line before `echo "
    ";` it should missing a semi column `;`
    – SaidbakR Apr 23 '15 at 23:32
  • thanks. i've made an update to my post, to show the output that var_dump makes. how I only select the id and use it as a variable like $id = .... – code_legend Apr 23 '15 at 23:48
  • It seems that it is protected properties so, for example, `$charge->_values['id']` will not return the `id`. So try [this answer](http://stackoverflow.com/a/27364648/1592845) to convert this protected object to JSON – SaidbakR Apr 23 '15 at 23:57
  • okay so ive converted it to JSON $charge_json = $charge->__toJSON(); echo "
    ";
    var_dump($charge_json);
    echo "
    "; and it prints it out string(1624) "{ "id": "ch_15uvBOIOMQSe8X84sTna1w78", "object": "charge", "created": 1429837242, "livemode": false, "paid": true, how would i pull out that id
    – code_legend Apr 24 '15 at 01:04
  • Please update your question with the `var_dump` of `$charge_json`. @user3907211 – SaidbakR Apr 24 '15 at 08:31
  • thanks. i've updated my question where i would like to grab the following line: "id": "ch_15vDxQIOMQSe8X84NBckjjil", – code_legend Apr 24 '15 at 21:16
  • It should be: `echo $charge_json->id;` @user3907211 – SaidbakR Apr 24 '15 at 23:40
  • thanks, but with that i get the following error Notice: Trying to get property of non-object i – code_legend Apr 24 '15 at 23:42
  • Oh sorry, so try: `$ob = json_decode($charge_json); echo $ob->id;` @user3907211 – SaidbakR Apr 25 '15 at 00:04