Files
ms1inscription-v5/paypal_advanced_checkout_in_php/paypal_checkout_init.php
2026-05-13 09:43:32 -04:00

146 lines
5.7 KiB
PHP

<?php
// Include the configuration file
require_once 'config.php';
// Include the database connection file
include_once 'dbConnect.php';
// Include the PayPal API library
require_once 'PaypalCheckout.class.php';
$paypal = new PaypalCheckout;
$response = array('status' => 0, 'msg' => 'Request Failed!');
$api_error = '';
if(!empty($_POST['request_type']) && $_POST['request_type'] == 'create_order'){
$payment_source = $_POST['payment_source'];
$product_data2 = array(
'item_number' => $itemNumber,
'item_name' => $itemName,
'price' => $itemPrice,
'currency' => $currency,
);
$product_data = array(
"intent" => "CAPTURE",
"purchase_units" => array(
array(
"custom_id" => $product_data2['item_number'],
"description" => $product_data2['item_name'],
"amount" => array(
"currency_code" => $product_data2['currency'],
"value" => $product_data2['price']
),
"shipping" => array(
"name" => array(
"full_name" => "stephan leith test"),
"address" => array(
"address_line_1" => "123 Townsend St",
"address_line_2" => "Floor 6",
"admin_area_2" => "San Francisco",
"admin_area_1" => "CA",
"postal_code" => "94107",
"country_code" => "US"
)
)
)
)
);
// Create order with PayPal Orders API
try {
$order = $paypal->createOrder($product_data, $payment_source);
} catch(Exception $e) {
$api_error = $e->getMessage();
}
if(!empty($order)){
$response = array(
'status' => 1,
'data' => $order
);
}else{
$response['msg'] = $api_error;
}
}elseif(!empty($_POST['request_type']) && $_POST['request_type'] == 'capture_order'){
$order_id = $_POST['order_id'];
// Create order with PayPal Orders API
try {
$order = $paypal->captureOrder($order_id);
} catch(Exception $e) {
$api_error = $e->getMessage();
}
if(!empty($order)){
$order_id = $order['id'];
$order_status = $order['status'];
$payment_source = $payment_source_card_name = $payment_source_card_last_digits = $payment_source_card_expiry = $payment_source_card_brand = $payment_source_card_type = '';
if(!empty($order['payment_source'])){
foreach($order['payment_source'] as $key=>$value){
$payment_source = $key;
if($payment_source == 'card'){
$payment_source_card_name = $value['name'];
$payment_source_card_last_digits = $value['last_digits'];
$payment_source_card_expiry = $value['expiry'];
$payment_source_card_brand = $value['brand'];
$payment_source_card_type = $value['type'];
}
}
}
if(!empty($order['purchase_units'][0])){
$purchase_unit = $order['purchase_units'][0];
if(!empty($purchase_unit['payments'])){
$payments = $purchase_unit['payments'];
if(!empty($payments['captures'])){
$captures = $payments['captures'];
if(!empty($captures[0])){
$transaction_id = $captures[0]['id'];
$payment_status = $captures[0]['status'];
$custom_id = $captures[0]['custom_id'];
$amount_value = $captures[0]['amount']['value'];
$currency_code = $captures[0]['amount']['currency_code'];
$create_time = date("Y-m-d H:i:s", strtotime($captures[0]['create_time']));
}
}
}
}
if(!empty($order_id) && $order_status == 'COMPLETED'){
// Check if any transaction data is exists already with the same TXN ID
$sqlQ = "SELECT id FROM transactions WHERE transaction_id = ?";
$stmt = $db->prepare($sqlQ);
$stmt->bind_param("s", $transaction_id);
$stmt->execute();
$stmt->bind_result($row_id);
$stmt->fetch();
$payment_id = 0;
if(!empty($row_id)){
$payment_id = $row_id;
}else{
// Insert transaction data into the database
$sqlQ = "INSERT INTO transactions (item_number,item_name,item_price,item_price_currency,order_id,transaction_id,paid_amount,paid_amount_currency,payment_source,payment_source_card_name,payment_source_card_last_digits,payment_source_card_expiry,payment_source_card_brand,payment_source_card_type,payment_status,created,modified) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,NOW())";
$stmt = $db->prepare($sqlQ);
$stmt->bind_param("ssdsssdsssssssss", $custom_id, $itemName, $itemPrice, $currency, $order_id, $transaction_id, $amount_value, $currency_code, $payment_source, $payment_source_card_name, $payment_source_card_last_digits, $payment_source_card_expiry, $payment_source_card_brand, $payment_source_card_type, $payment_status, $create_time);
$insert = $stmt->execute();
if($insert){
$payment_id = $stmt->insert_id;
}
}
if(!empty($payment_id)){
$ref_id_enc = base64_encode($transaction_id);
$response = array('status' => 1, 'msg' => 'Transaction completed!', 'ref_id' => $ref_id_enc);
}
}
}else{
$response['msg'] = $api_error;
}
}
echo json_encode($response);