Tuesday, March 23, 2010, 8:29

PHP Class untuk PayPal Website Payment Pro

Setelah konon pernah saya kutip dikit tentang php class untuk PayPal IPN di blog saya, kali ini saya berhadapan dengan proyek yang berurusan dengan PayPal Website Payment Pro.

Asal muasal ceritanya, klien ingin supaya pembayaran dilakukan di websitenya dia (online store) agar tampak profesional.

Nah, untuk paypalnya ini saya prefer pake php class punya AngellEye .

Untuk saat (menulis postingan) ini, php class PayPal website payment pro punya Angelleye itu yang dokumentasinya paling lengkap. Tolong update ya kalau ada yang lebih baru lagi.

Beberapa langkah untuk memulai:

  1. Mula-mula, pastinya kita harus bikin account paypal dulu yang akan dijadikan sebagai merchant (penerima duit). Untuk sandbox, bikinnya di http://developer.paypal.com (daftar beneran dulu untuk bisa bikin account bo’ongan/ tester account setelah login. daftarnya gratis kok).
  2. login pakai account tersebut (kalau pakai sandbox, login sandbox dulu baru login account tester)
  3. My Account » Profile »Get API Credentials . Ikuti petunjuknya untuk mendapatkan PayPal API Signature untuk Website Payment Pro (bukan Payflow Pro) dengan integrasi sendiri (bukan pakai third-party shopping cart)
  4. Anda akan mendapatkan:
    • API Username
    • API Password
    • API Signature

Implementasi:

Tiga data diatas dimasukkan kedalam $ppConfig.

[php]
<?php
# Add PayPal class
require_once(‘../PayPalApps/paypal.nvp.class.php’);

# Setup the PayPal object
$ppConfig = array(‘Sandbox’ => true, //set false untuk non-sandbox
‘APIUsername’ => ‘jbuzz547_api1.ymail.com’,
‘APIPassword’ => ‘AW9KUMJRLYG5XSX3′,
‘APISignature’ => ‘Au4SU-K4fYYF.P79qKJxkZVnRl99Aajgl6JLQDOqRPpDrqFGThC-rB4H’,
‘EndPointURL’ => ‘https://api-3t.sandbox.paypal.com/nvp’); // untuk yang bukan sandbox, alamatnya https://api-3t.paypal.com/nvp
$pp = new PayPal($ppConfig);

# Populate data arrays for API call.
$DPFields = array(
‘paymentaction’ => ‘Sale’,
‘ipaddress’ => ‘192.168.1.34’,
‘returnfmfdetails’ => ‘1’
);

$CCDetails = array(
‘creditcardtype’ => ‘Visa’,
‘acct’ => ‘4635800000835916’,
‘expdate’ => ‘052012’,
‘cvv2′ => ‘123’,
‘startdate’ => ”
);

$PayerInfo = array(
’email’ => ‘tester@testerson.com’,
‘business’ => ‘Testers, LLC’
);

$PayerName = array(
‘salutation’ => ‘Mr.’,
‘firstname’ => ‘Tester’,
‘middlename’ => ‘T.’,
‘lastname’ => ‘Testerson’,
‘suffix’ => ‘Jr.’
);

$BillingAddress = array(
‘street’ => ‘123 Test Ave.’,
‘street2′ => ‘Apt. 3′,
‘city’ => ‘Testersville’,
‘state’ => ‘MO’,
‘countrycode’ => ‘US’,
‘zip’ => ‘64030’,
‘phonenum’ => ‘555-555-5555′
);

$ShippingAddress = array(
‘shiptoname’ => ‘Mr. Tester Testerson Jr.’,
‘shiptostreet’ => ‘123 Test Ave’,
‘shiptostreet2′ => ‘Apt. 3′,
‘shiptocity’ => ‘Testersville’,
‘shiptostate’ => ‘MO’,
‘shiptozip’ => ‘64030’,
‘shiptocountrycode’ => ‘US’,
‘shiptophonenum’ => ‘555-555-5555′
);

$PaymentDetails = array(
‘amt’ => ‘25.00’,
‘currencycode’ => ‘USD’,
‘itemamt’ => ‘15.00’,
‘shippingamt’ => ‘10.00’,
‘handlingamt’ => ”,
‘taxamt’ => ”,
‘desc’ => ‘This is a test order.’,
‘custom’ => ”,
‘invnum’ => ‘1234-ABC’,
‘buttonsource’ => ”,
‘notifyurl’ => ”
);

# Now combine your data arrays into a single nested array to pass into the class.
$DPData = array(
‘DPFields’ => $DPFields,
‘CCDetails’ => $CCDetails,
‘PayerInfo’ => $PayerInfo,
‘PayerName’ => $PayerName,
‘BillingAddress’ => $BillingAddress,
‘ShippingAddress’ => $ShippingAddress,
‘PaymentDetails’ => $PaymentDetails);

# Now we pass the nested array of all our data into the class.
$DPResult = $pp -> DoDirectPayment($DPData);

# Now lets study the result array
echo ‘<pre />';
print_r($DPResult);
exit();
?>
[/php]

Kalau yang ini untuk pembayaran dengan account PayPal

[php]
<?php
# Add PayPal class
require_once(‘../PayPalApps/paypal.nvp.class.php’);

# Setup the PayPal object
$ppConfig = array(‘Sandbox’ => true, // set false untuk non-sandbox
‘APIUsername’ => ‘jbuzz547_api1.ymail.com’,
‘APIPassword’ => ‘AW9KUMJRLYG5XSX3′,
‘APISignature’ => ‘Au4SU-K4fYYF.P79qKJxkZVnRl99Aajgl6JLQDOqRPpDrqFGThC-rB4H’,
‘EndPointURL’ => ‘https://api-3t.sandbox.paypal.com/nvp’); // untuk yang bukan sandbox, alamatnya https://api-3t.paypal.com/nvp

$pp = new PayPal($ppConfig);

# Populate data arrays for API call.
$SECFields = array(
‘returnurl’ => ‘http://www.domain.com/return.php’,
‘cancelurl’ => ‘http://www.domain.com/cancel.php’,
‘paymentaction’ => ‘Sale’
);
$PaymentDetails = array(
‘amt’ => ‘18.00’,
‘currencycode’ => ‘USD’,
‘itemamt’ => ‘10.00’,
‘shippingamt’ => ‘5.00’,
‘handlingamt’ => ‘2.00’,
‘taxamt’ => ‘1.00’,
‘desc’ => ‘This is a test order.’,
‘custom’ => ”,
‘invnum’ => ‘1234-ABC’
);

# Now combine your data arrays into a single nested array to pass into the class.
$SECData = array(
‘SECFields’ => $SECFields,
‘PaymentDetails’ => $PaymentDetails
);

# Now we pass the nested array of all our data into the class.
$SECResult = $pp -> SetExpressCheckout($SECData);

# Now lets study the result array
echo ‘<pre />';
print_r($SECResult);
exit();

# Now we can just use the returned REDIRECTURL field to redirect the user to PayPal based on our input.
header(‘Location: ‘ . $SECResult[‘REDIRECTURL’]);
?>
[/php]

paypal.nvp.clssass download here

Butuh coba-coba sendiri sambil lirik classnya kalau mau buat bayar multiple items. 😉

Sekedar pengingat. Jangan lupa beli hosting dengan dedicated IP dan install SSL.

Terlihat mudah bukan?

Btw, adakah yang sudah dapat sertifikasi PayPal? Atau sering nyambangi www.X.com (PayPal Developer Network)? Ditunggu sharingnya 😉

Postingan Terkait