Reputation: 1
I build eCommerce in PHP and this page should authorize the credit card. If it is valid, I will store it on the customer page.
I wrote this code for it, but I still did not get how to authorize the card not to charge it.
the code
<?php
if($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST['card_number']) && !empty($_POST['card_name']) && !empty($_POST['expiry_month']) && !empty($_POST['expiry_year']) && !empty($_POST['cvv']))
{
$card_number=str_replace("+","",$_POST['card_number']);
$card_name=$_POST['card_number'];
$expiry_month=$_POST['expiry_month'];
$expiry_year=$_POST['expiry_year'];
$cvv=$_POST['cvv'];
$expirationDate=$expiry_month.'/'.$expiry_year;
require_once 'braintree/Braintree.php';
Braintree_Configuration::environment('production'); /* this is sandbox or production */
Braintree_Configuration::merchantId('id');
Braintree_Configuration::publicKey('public');
Braintree_Configuration::privateKey('secrit ');
$result = Braintree_Transaction::sale(array(
'amount' => 0,
'creditCard' => array(
'number' => $card_number,
'cardholderName' => $card_name,
'expirationDate' => $expirationDate,
'cvv' => $cvv
)
));
//echo "<pre>";
echo $result->message;
die;
if ($result->success)
{
//print_r("success!: " . $result->transaction->id);
if($result->transaction->id)
{
$braintreeCode=$result->transaction->id;
echo "<h2>Your payment successfully done ".$braintreeCode."</h2>";
}
}else if ($result->transaction){
echo "<pre>";
print_r($result->transaction);
//echo '{"OrderStatus": [{"status":"2"}]}';
}else{
echo "<h2>Your payment is not completed</h2>";
}
}
?>
Upvotes: 0
Views: 725
Reputation: 41
Full disclosure: I work at Braintree. If you have any further questions, feel free to contact support.
The easiest fix for what you are trying to do is to pass the storeInVault
parameter into your Transaction::sale()
call. This saves the customer information into your vault as long as the payment is successful.
Additionally, you can create a customer by integrating our Customer::create()
call into your code with the verifyCard
parameter.
Upvotes: 4
Reputation: 5213
I recommend following the official guide at https://developers.braintreepayments.com/guides/transactions/php
The way you gather credit card data (such as number or expiry date) is not compliant with the requirements Braintree imposes and may lead to suspension or termination of your account.
Credit card data shall not be available to your server. You should rather use Drop-in UI or Hosted fields.
Upvotes: 1