0

I can't seem to find an answer for this online, but I think it's because I'm not too sure what to search for because I'm very new to xml and json.

If I have this xml

$postData = '<PaymentNotification Stage="false">
  <MerchantReference>100004117</MerchantReference>
  <TransactionType>PAYMENT</TransactionType>
  <TransactionState>SUCCESSFUL</TransactionState>
  <ResultCode>00</ResultCode>
  <ResultMessage>Successful</ResultMessage>
  <PayUReference>1167986976014</PayUReference>
  <Basket>
    <Description>Store Order Number:100004117</Description>
    <AmountInCents>100</AmountInCents>
    <CurrencyCode>ZAR</CurrencyCode>
    <Products/>
  </Basket>
  <PaymentMethodsUsed>
    <Eft BankName="ABSA" AmountInCents="100" Reference="CUMVSIUPFG" AccountNumber="4077920871" BranchNumber="632005" AccountType="Cheque" TimeLimit="168" Currency="ZAR"/>
  </PaymentMethodsUsed>
  <IpnExtraInfo>
    <ResponseHash></ResponseHash>
  </IpnExtraInfo>
</PaymentNotification>';

And then json decode it

$returnData = json_decode(json_encode(simplexml_load_string($postData)),true);

I can retrieve the objects eg.

$cost_amount = $returnData['Basket']['AmountInCents'];

But how do I get the attributes in Eft eg. BankName?

I have tried these with no success

$paid_amount = $returnData['PaymentMethodsUsed']['CreditCard']['@AmountInCents'];
and
$paid_amount = $returnData[0]->PaymentMethodsUsed[0]->CreditCard->AmountInCents;
David North
  • 437
  • 1
  • 4
  • 17
  • 2
    thats why sometimes this method doesn't cut it, its not a perfect method to use because it doesn't preserve the data perfectly, you will just have to traverse it the normal way – Kevin Sep 27 '14 at 14:07
  • It's also not so bad - next to the many duplicates this has on site - to first read about it in the PHP manual: [Basic SimpleXML usage](http://php.net/manual/en/simplexml.examples-basic.php) – hakre Sep 28 '14 at 08:38

2 Answers2

0

For this case, why not traverse the nodes normally, and use ->attributes() method:

$postData = '<PaymentNotification Stage="false">
  <MerchantReference>100004117</MerchantReference>
  <TransactionType>PAYMENT</TransactionType>
  <TransactionState>SUCCESSFUL</TransactionState>
  <ResultCode>00</ResultCode>
  <ResultMessage>Successful</ResultMessage>
  <PayUReference>1167986976014</PayUReference>
  <Basket>
    <Description>Store Order Number:100004117</Description>
    <AmountInCents>100</AmountInCents>
    <CurrencyCode>ZAR</CurrencyCode>
    <Products/>
  </Basket>
  <PaymentMethodsUsed>
    <Eft BankName="ABSA" AmountInCents="100" Reference="CUMVSIUPFG" AccountNumber="4077920871" BranchNumber="632005" AccountType="Cheque" TimeLimit="168" Currency="ZAR"/>
  </PaymentMethodsUsed>
  <IpnExtraInfo>
    <ResponseHash></ResponseHash>
  </IpnExtraInfo>
</PaymentNotification>';

$xml = simplexml_load_string($postData);

$cents = (string) $xml->PaymentMethodsUsed->Eft->attributes()->AmountInCents;
echo $cents; // 100
Kevin
  • 41,694
  • 12
  • 53
  • 70
-1

Enjoyed solving this. Totally used the hit and trial method. My approach : i tried to json_encode the PaymentMethodUsed array by :

 $x = $returnData['PaymentMethodsUsed'];
 echo json_encode($x);

And the result was :

{"Eft":{"@attributes":{"BankName":"ABSA","AmountInCents":"100","Reference":"CUMVSIUPFG","AccountNumber":"4077920871","BranchNumber":"632005","AccountType":"Cheque","TimeLimit":"168","Currency":"ZAR"}}}

So, guessed the approach and I hope you can get your desired value using :

echo $returnData['PaymentMethodsUsed']['Eft']['@attributes']['BankName'];