Last active
August 29, 2015 14:24
-
-
Save igorbenic/471405ab6070b87a8129 to your computer and use it in GitHub Desktop.
Braintree Primjer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Nekakva funkcija koja će nam dati product ID proizvoda kojeg se kupuje | |
//npr. WordPress Custom Post Type "product", u Loop-u se može koristiti get_the_id(); | |
$productID = get_productID(); | |
//Dohvaćamo library od Braintree-a | |
require_once "putanja/do/braintree/Braintree.php"; | |
Braintree_Configuration::environment('sandbox'); | |
Braintree_Configuration::merchantId('tvoj_id'); | |
Braintree_Configuration::publicKey('tvoj_public_key'); | |
Braintree_Configuration::privateKey('tvoj_private_key'); | |
//Ovo može biti i prazno kako bi se forma slala na isti URL (ili se možete postaviti taj URL ovdje) | |
$putanjaDoPaymenta = "putanja/do/payment/stranice.php"; | |
$clientToken = Braintree_ClientToken::generate(); | |
?> | |
<form id="checkout" method="post" action="<?php echo $putanjaDoPaymenta; ?>"> | |
<input type="email" name="userEmail" placeholder="Unesi svoj email" value=""/> | |
<input type="text" name="cardholderName" placeholder="Unesi svoje ime i prezime" value=""/> | |
<div id="payment-form"></div> | |
<input type="hidden" name="productID" value="<?php echo $productID; ?>" /> | |
<input type="submit" name="braintreeSubmit" value="Kupi"> | |
</form> | |
<script src="https://js.braintreegateway.com/v2/braintree.js"></script> | |
<script> | |
// We generated a client token for you so you can test out this code | |
// immediately. In a production-ready integration, you will need to | |
// generate a client token on your server (see section below). | |
var clientToken = "<?php echo $clientToken; ?>"; | |
braintree.setup(clientToken, "dropin", { | |
container: "payment-form" | |
}); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if( isset($_POST["braintreeSubmit"]) && isset($_POST["payment_method_nonce"]) && $_POST["payment_method_nonce"] != "" ){ | |
//Forma je postavljena i payment method nonce nije prazan pa možemo krenuti dalje sa izvršavanjem koda | |
require_once "putanja/do/braintree/Braintree.php"; | |
Braintree_Configuration::environment('sandbox'); | |
Braintree_Configuration::merchantId('tvoj_id'); | |
Braintree_Configuration::publicKey('tvoj_public_key'); | |
Braintree_Configuration::privateKey('tvoj_private_key'); | |
$buyerEmail = $_POST["userEmail"]; | |
$buyerName = $_POST["cardholderName"]; | |
$productID = $_POST["productID"]; | |
$paymentNonce = $_POST["payment_method_nonce"]; | |
if( $productID != "" ){ | |
//Ako nema ID proizvoda nemamo što prodati | |
//Ako ima, možemo dalje | |
$price = getPriceByID($productID); | |
$result = Braintree_Transaction::sale([ | |
'amount' => $price, | |
'paymentMethodNonce' => $paymentNonce, | |
//Ako hoćemo da ide odmah na uplatu potrebno je postaviti settlement | |
'options' => array( | |
'submitForSettlement' => True | |
), | |
//Customer je opcionalan i mogu se odrediti koji parametri će ići. | |
'customer' => array( | |
'email' => $buyerEmail, | |
'firstName' => getFirstName($buyerName), | |
'lastName' => getLastName($buyerName), | |
) | |
]); | |
if($result->success){ | |
//ako je uspješno prošlo, možemo dalje raditi što hoćemo | |
$transaction = $result->transaction; | |
//Uzimamo ID transakcije | |
$transactionID = $transaction->id; | |
//fiktivna funkcija koja postavlja proizvod kao prodan i prosljeđujemo transaction id | |
//da se ima informacija tko i što je kupio kako bi se mogo pogledati u Braintree | |
setProductAsSold($productID, $transactionID); | |
//fiktivna funkcija koja će poslati korisniku email o uspješnoj kupnji | |
//može se koristiti i kao nekakav e-račun i sl. | |
emailBuyer($buyerEmail, $buyerName, $transactionID, $price); | |
} else { | |
//Prikazjemo poruku koju smo dobili od Braintree-a | |
echo $result->message; | |
} | |
} else { | |
//fiktivna funkcija koja bi postavila neki tekst koji bi obavjestio korisnika da nema product id, što znači da je došao sa neke krive adrese | |
postavi_Error_message("NO_PRODUCTID"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment