Update currency OSCommerce dengan kurs klikBCA

November 25th, 2008 by int2k Leave a reply »

Setelah pembuatan post sebelum nya tentang widget kurs rate BCA, ada muncul permintaan agar script widget ini bisa di gunakan untuk melakukan proses update currency pada situs yang berbasis oscommerce. Standar awal dari OSCommerce untuk proses update currency adalah dengan melalui website OANDA atau XE, ini bisa dilihat pada file admin/includes/application_top yang berupa :


// Define how do we update currency exchange rates
// Possible values are 'oanda' 'xe' or ''
define('CURRENCY_SERVER_PRIMARY', 'oanda');
define('CURRENCY_SERVER_BACKUP', 'xe');

Disini artinya update currency akan melalui situs OANDA terlebih dahulu, dan kalo gagal baru dilanjutkan dengan menggunakan situs XE. Proses update currency ini dilakukan dengan mengambil nilai yang dihasilkan oleh fungsi quote_oanda_currency dan quote_xe_currency yang ada di dalam file admin/includes/functions/localization.php. Didalam file inilah nantinya akan kita tambahkan fungsi untuk mengambil nilai kurs yang ada di situs klikbca.

Baiklah kita mulai proses modifikasi nya. Pertama-tama tambahkan fungsi di bawah ini ke dalam file localization.php.

function quote_bca_currency($to, $from = DEFAULT_CURRENCY) {
if( (($to=='IDR')&&($from==$to))||($to==$from) ) {
return '1';
} elseif (($to=='IDR')||($from=='IDR')) {

// start curl
$url = "http://www.klikbca.com/individual/silver/ind/rates.html";

if(function_exists('curl_init')) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($curl, CURLOPT_ENCODING, 'ISO-8859-1');
curl_setopt($curl, CURLOPT_URL, $url);
$page = curl_exec($curl);
curl_close($curl);
} elseif (ini_get('allow_url_fopen')) {
error_reporting(0);
$page = file_get_contents($url);
}

$match = array();

$content = explode ('<table border="0" cellspacing="0" cellpadding="0" class="testL">', $page);
$content = explode ('</table>', $content[1]);
$content[0] .= '';

$periode = explode ('<font color="black" size="1" face="Arial">', $content[0]);
$periode = explode ('WIB', $periode[1]);
$periode[0] .= 'WIB';

$periode = str_replace("  ","", str_replace("\r\n","",$periode[0]));

$content = explode ("<TR BGCOLOR='#f0f0f0'>", $content[0]);

$list = 0;
for($i=1;$i<=count($content);$i++){
$negara = explode ("<TR bgcolor='#e0e0e0'>", $content[$i]);
if($negara[0]){
$list++;
$kurs[$list] = str_replace("&nbsp;","", str_replace("::","", str_replace(" ","", str_replace("                        ",":", str_replace("\r\n","",strip_tags($negara[0]))))))."<br>";
list($id1, $jual1, $beli1) = split(':', $kurs[$list]);
$match[$id1]['jual'] = str_replace(".00","",$jual1);
$match[$id1]['beli'] = str_replace(".00","",$beli1);
}
if($negara[1]){
$list++;
$kurs[$list] = str_replace("&nbsp;","", str_replace("::","", str_replace(" ","", str_replace("                        ",":", str_replace("\r\n","",strip_tags($negara[1]))))))."<br>";
list($id1, $jual1, $beli1) = split(':', $kurs[$list]);
$match[$id1]['jual'] = str_replace(".00","",$jual1);
$match[$id1]['beli'] = str_replace(".00","",$beli1);
}

}

if(isset($match[$to])) {
return ( sprintf("%08.8f",1 / $match[$to]['beli']));
} elseif(isset($match[$from])) {
return ( $match[$from]['jual']);
} else {
return false;
}
} else {
return false;
}
}

Dan selanjutnya lakukan perubahan pada file admin/includes/application_top.php pada bagian yang telah di sebutkan di atas menjadi seperti ini.

// Define how do we update currency exchange rates
// Possible values are 'oanda' 'xe' or ''
define('CURRENCY_SERVER_PRIMARY', 'bca');
define('CURRENCY_SERVER_BACKUP', 'oanda');

Setelah langkah diatas harusnya proses update currency dengan kurs yang berasal dari website klikBCA harusnya sudah bisa dilakukan.

Advertisement

30 comments

  1. pnyet says:

    thanks banget infonya, akhirnya status solved :).
    btw mas gmn klo modul ini di upload di add ons community oscommerce? :ngacir:

  2. syahli says:

    Mas mau tanya klo data kursnya mau kita simpen ke database lokal misal mysql gimana mas?mohon pencerahannya yah.

    • int2k says:

      bisa aja koq, itu kan ada loop untuk memparsing kurs nya
      dengan keluaran loop berupa array yang berisi kode kurs, nilai jual dan nilai beli. tinggal di loop aja isi array nya untuk di lakukan proses penyimpanan ke dalam database

  3. Neverwinter says:

    mas, boleh tanya,…
    kenapa waktu di copy langsung ke PHPed masih banyak syntax yang error..
    saat copy paste diatas langsung ke PHPed Editor

    boleh ngga mas, kirim raw php ke email saya mas ?

    Thanks sebelumnya
    Yosias R.N
    ( Surabaya )

    • int2k says:

      sorry kemaren kena update post nya
      jadi hilang sebagian isi code nya :hammer:

      harusnya sekarang sih udah bisa koq :ngacir:

  4. Neverwinter says:

    ngga bisa mas,

    sudah saya coba script diatas, hasilnya seperti ini :

    Warning: The primary exchange rate server (bca) failed for US Dollar (USD) – trying the secondary exchange rate server.
    Success The exchange rate for US Dollar (USD) was updated successfully via xe.
    Success The exchange rate for Rupiah (IDR) was updated successfully via xe.

    mohon petunjuk
    Thanks

    Yosias R.N

    • int2k says:

      coba rubah di line 23 jadi

      wah gara2 ke-edit isinya jadi berantakan :nohope:

      maaf udah bikin susah :peace:

      wah di comment gak bisa kasih kode, udah di benerin lagi kode nya
      perubahan nya pada baris line 24
      variabel nya harusnya $page

  5. Neverwinter says:

    ngga papa mas,
    namanya saling membantu, saya malah terima kasih ada bantuan secepat kilat dari mas heri :tabrakan:

    saya coba kedua kali ( sebelom upload ke main website, saya gunakan XAMPP )
    keluar crash seperti ini :

    Warning: explode() [function.explode]: Empty delimiter in C:\xampp\htdocs\quadcomp\admin\includes\functions\localization.php on line 32

    Warning: explode() [function.explode]: Empty delimiter in C:\xampp\htdocs\quadcomp\admin\includes\functions\localization.php on line 33

    Warning: explode() [function.explode]: Empty delimiter in C:\xampp\htdocs\quadcomp\admin\includes\functions\localization.php on line 35

    Warning: explode() [function.explode]: Empty delimiter in C:\xampp\htdocs\quadcomp\admin\includes\functions\localization.php on line 39

    Warning: explode() [function.explode]: Empty delimiter in C:\xampp\htdocs\quadcomp\admin\includes\functions\localization.php on line 42

    Warning: Cannot modify header information – headers already sent by (output started at C:\xampp\htdocs\quadcomp\admin\includes\functions\localization.php:32) in C:\xampp\htdocs\quadcomp\admin\includes\functions\general.php on line 22

    My system :
    HTTP Server: Apache/2.2.9 (Win32) DAV/2 mod_ssl/2.2.9 OpenSSL/0.9.8h mod_autoindex_color PHP/5.2.6
    PHP Version: 5.2.6 (Zend: 2.2.0)

    • int2k says:

      aneh kode yang terakhir diatas udah dicoba
      dan udah bisa jalan hmm

      waktu proses copy paste kode nya mungkin ada yang hilang
      coba klik plain, baru di copy dari situ kodenya

  6. Neverwinter says:

    maaf mas heri, saya copy paste nya dari file di email…
    bukan dari file yang telah diperbaiki di atas …

    sudah copy paste code diatas, udah ngga crash lagi ..

    tapi koq hasilnya begini
    http://quad-comp.com/hasil.jpg

    • int2k says:

      loh kan udah bener
      1 USD = 10400 IDR
      jadi kalo dibalik
      1 IDR = 1/10400 USD = 0.000096154

  7. risa says:

    Mas kalau di pasang Status YM gimana Mas… :tabrakan:

    • int2k says:

      lho artikel nya apa tanya nya apa :p

      status YM di mana ? wordpress kah ?
      kalo gak salah ada widget nya koq

  8. Edi Nasution says:

    Error: The exchange rate for US Dollar (USD) was not updated via oanda. Is it a valid currency code?
    Warning: The primary exchange rate server (bca) failed for Euro (EUR) – trying the secondary exchange rate server.
    Error: The exchange rate for Euro (EUR) was not updated via oanda. Is it a valid currency code?
    The exchange rate for Indonesia Rupiah (IDR) was updated successfully via oanda.

    • int2k says:

      hmm udah di ikutin langkah2 yang ada di post ?

      currency default yang di pake IDR kan ?

  9. Sip thx script nya. Sekali coba langsung bisa :D

    Mau nanya donk, gimana caranya biar update currency bisa otomatis? Misalnya dibuat auto update setiap 3 jam sekali?

    • int2k says:

      mungkin bisa di buat cron job
      dan script update nya di jalanin dari luar

  10. yoyox says:

    Mas Masih bingung nih, blog aku wordpress. ketika aku masukin kodenya malah kodenya keluar di blog aku, bingung nih, tolong bikinkan script yang support wordpress.
    kasihan kan bagi yang newbie seperti aku ini.
    Request ya mas..
    Artikele kasih judul “Kurs Dolar di WordPress”
    aku udah 3 hari 3 malam begadang, sampai magh aku kambuh.
    Please…..!!!!!!!!!!
    yoyoxcomputer@gmail.com
    http://yoyoxcomputer.wordpress.com
    kabarin aku ya….

  11. cepi86 says:

    mas saya mau tanya nich…cara ngrubah halaman depannya gimana yah ko yg sy ad tulisan di bawah ini sih?

    This is a default setup of osCommerce Online Merchant. Products shown are for demonstrational purposes. Any products purchased will not be delivered nor will the customer be billed. Any information seen on these products is to be treated as fictional.

    Error Messages

    If there are any error or warning messages shown above, please correct them first before proceeding.

    Error messages are displayed at the very top of the page with a complete background color.

    Several checks are performed to ensure a healthy setup of your online store – these checks can be disabled by editing the appropriate parameters at the bottom of the includes/application_top.php file.
    Editing Page Texts

    The text shown here can be modified in the following file, on each language basis:

    [path to catalog]/includes/languages/english/index.php

    That file can be edited manually, or via the Administration Tool with the Languages->English->Define or Tools->File Manager modules.

    The text is set in the following manner:

    define(‘TEXT_MAIN’, ‘This is a default setup of the osCommerce project…’);

    The text highlighted in green may be modified – it is important to keep the define() of the TEXT_MAIN keyword. To remove the text for TEXT_MAIN completely, the following example is used where only two single quote characters exist:

    define(‘TEXT_MAIN’, ”);

    More information concerning the PHP define() function can be read here.
    Online Documentation

    Online documentation can be read at the osCommerce Knowledge Base site.

    Support is available at the osCommerce Support Site.

    If you wish to download the solution powering this shop, or if you wish to contribute to the osCommerce project, please visit the support site of osCommerce. This shop is running on osCommerce Online Merchant v2.2 RC2a

  12. hadingrh says:

    Makasih banyak mas..
    Saya baru nyoba2 OSC nih, trus saya coba skrip update currency dgn klikbca dari mas herry ini.
    Codes diatas sudah saya lakukan (ganti xe menjadi bca, lalu menjadi primary. Yang oanda jadi backup server), lalu functionnya juga sudah saya copy-paste ke file admin/includes/functions/localization.php. Berhasil tanpa error (justru krn gak ada error malah jadi masalah krn gak ketahuan masalahnya dimana…hehe). Harga produk yg sudah ada di katalog saya dalam bentuk dollar. Harusnya ketika saya switch ke rupiah, si klikbca mengganti nominal ke rupiah, kan? nah ini kagak… Jadi gimana nih? salah saya apa? …(..hihi..hopeless).
    Apa ada proses localization backend admin yg terlewat?

    maaf ngeroptin, ma’lum nuwbie :peace:
    p.s: currency rupiah sudah saya tambahkan di backend admin (localization currency).

  13. bardi says:

    thk bgt nih gan.. :ck

  14. hicomtech says:

    mohon bantuannya…
    untuk bagian ini saya rada binggung
    file di edit apa di tambahi scriptnya?
    bantu di perjelas yah…thanks

    Dan selanjutnya lakukan perubahan pada file admin/includes/application_top.php pada bagian yang telah di sebutkan di atas menjadi seperti ini.

    // Define how do we update currency exchange rates

    // Possible values are ‘oanda’ ‘xe’ or ”

    define(‘CURRENCY_SERVER_PRIMARY’, ‘bca’);

    define(‘CURRENCY_SERVER_BACKUP’, ‘oanda’);

    • int2k says:

      kayaknya dah cukup jelas :P ada beberapa file yang memang perlu di edit
      1. yang localization itu di tambahin kode nya
      2. yang applicaton_top.php itu di edit