This class file using for one time payment management.
/
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Jobs\ChangeLocale;
use Auth;
use Hash;
use DB;
use Session;
use Validator;
use Input;
use App\Requestors;
use Mail;
use App\Mailers\AppMailer;
use Srmklive\PayPal\Services\ExpressCheckout;
use Srmklive\PayPal\Services\AdaptivePayments;
use Stripe\Stripe;
use App\Models\Membership\Plans;
use App\Models\Membership\Member;
use App\Models\Membership\MembershipPlan;
use App\Models\Membership\Transations;
use Carbon\Carbon;
class OneTimePaymentController extends Controller
{
/
intialize objects
_/
public function __construct()
{
\Stripe\Stripe::setApiKey(config('services.stripe.secret'));
$this->express_checkout_provider = new ExpressCheckout; // To use express checkout.
$this->adaptive_paymentprovider = new AdaptivePayments; // To use adaptive payments.
}
/
_function for create customer
_/
public function createCardCustomer($customer_details, $form_data){
try{
/Create customer/
$customer = \Stripe\Customer::create([
'card' => $form_data['stripeToken'],
'email' => $customer_details['email'],
'metadata' => [
"First Name" => $customer_details['first_name'],
"Last Name" => $customer_details['last_name'],
],
]);
Session::put('decs',$data['invoice_description']);
// Use the following line when creating recurring payment profiles (subscriptions)
if($response['ACK'] == 'Success'){
Session::put('token',$response['TOKEN']);
// This will redirect user to PayPal
return redirect($response['paypal_link']);
}else{
return false;
}
}
/Get express checkout for single payment/
public function GetExpressCheckoutDetails(){
$token = Session::get('token');
$response = $this->express_checkout_provider->getExpressCheckoutDetails($token);
$data = $this->setDataPaypal();
$get_express_checkout_details_response = $this->doExpressCheckoutPayment($data, $response, $response['TOKEN'], $response['PAYERID']);
$payment_plan_id = Session::get('paypal_plan_id');
$payment_plan_type = Session::get('paypal_plan_type');
/*get the user plan details*/
$plan_obj = new Plans;
$plan_details = $plan_obj->getPlanDetails($payment_plan_id);
/*update transaction table and membership table*/
$plan_controller_obj = new \App\Http\Controllers\PlanController;
$plan_controller_obj->addNewMembership($response, $plan_details, $payment_plan_type);
$plan_controller_obj->addTransactionDetails($get_express_checkout_details_response, $payment_plan_type, $response);
$plan_controller_obj->markUserAsPaidUser($response, $plan_details, $payment_plan_type);
/add new taransaction detial for user/
Session::put('message','You have successfully subscribe to'.$plan_details->name);
return redirect('dashboard');
}
}
/This function use for transcation from paypal/
public function doExpressCheckoutPayment($data, $response_exp, $token, $PayerID){
$response = $this->express_checkout_provider->doExpressCheckoutPayment($data, $token, $PayerID);
//////
/* public function setRecurrinProfilePaypal(){
// The $token is the value returned from SetExpressCheckout API call
$token = Session::get('token');
$data = $this->setDataPaypal();
$profile_desc = !empty($data['subscription_desc']) ?
$data['subscription_desc'] : $data['invoice_description'];
$startdate=date("Y-m-d",time());
// $data['subscription_desc'];
$data['invoice_description'];
$data = [
'PROFILESTARTDATE' => $startdate,
'DESC' => $profile_desc,
'BILLINGPERIOD' => 'Month', // Can be 'Day', 'Week', 'SemiMonth', 'Month', 'Year'
'BILLINGFREQUENCY' => 12, // set 12 for monthly, 52 for yearly
'AMT' => 10, // Billing amount for each billing cycle
'CURRENCYCODE' => 'USD', // Currency code
'TRIALBILLINGPERIOD' => 'Day', // (Optional) Can be 'Day', 'Week', 'SemiMonth', 'Month', 'Year'
'TRIALBILLINGFREQUENCY' => 10, // (Optional) set 12 for monthly, 52 for yearly
'TRIALTOTALBILLINGCYCLES' => 1, // (Optional) Change it accordingly
'TRIALAMT' => 0, // (Optional) Change it accordingly
];
$response = $this->express_checkout_provider->createRecurringPaymentsProfile($data, $token);
print_r($response);
die();
}*/
here is my code
<?php /**
This class file using for one time payment management. / namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use App\Jobs\ChangeLocale; use Auth; use Hash; use DB; use Session; use Validator; use Input; use App\Requestors; use Mail; use App\Mailers\AppMailer; use Srmklive\PayPal\Services\ExpressCheckout; use Srmklive\PayPal\Services\AdaptivePayments; use Stripe\Stripe; use App\Models\Membership\Plans; use App\Models\Membership\Member; use App\Models\Membership\MembershipPlan; use App\Models\Membership\Transations; use Carbon\Carbon; class OneTimePaymentController extends Controller { /
}catch (Exception $e) { Session::flash('exception-message', $e->getMessage() ); return redirect()->back(); }
} /**
}else{ Session::flash('exception-message', 'Payment failed' ); return redirect()->back(); }
} public function calculateDaysLeft(){ /calculate number of days left/ $currnt_day = date("d",time()); $current_month = date("m",time()); $current_year = date("y",time()); $total_days = cal_days_in_month(CAL_GREGORIAN,$current_month,$current_year); $total_days_left = $total_days - $currnt_day; return $total_days_left; } public function calculateAmount($plan_details){ /Get the number of days left in current month/ $total_days_left = $this->calculateDaysLeft(); //echo 'currnt_day '.$currnt_day.'current_month '.$current_month.'current_year '.$current_year.'total_days '.$total_days.'total_days_left '.$total_days_left;die; /calculate one time amout/ if( $total_days_left != 0 ){ if($plan_details->frequency == 1){ $amount = ( $total_daysleft )( (float)env('ONE_DAY_CHARGE_FOR_MONTHLY_PLAN') ); }else{ $amount = ( $total_daysleft )( (float)env('ONE_DAY_CHARGE_FOR_YEARLY_PLAN') ); } return $amount; }else{ return false; } } /**
$response = $this->express_checkout_provider->setExpressCheckout($data, true);
Session::put('decs',$data['invoice_description']); // Use the following line when creating recurring payment profiles (subscriptions) if($response['ACK'] == 'Success'){
}else{ return false; } } /Get express checkout for single payment/ public function GetExpressCheckoutDetails(){ $token = Session::get('token'); $response = $this->express_checkout_provider->getExpressCheckoutDetails($token); $data = $this->setDataPaypal(); $get_express_checkout_details_response = $this->doExpressCheckoutPayment($data, $response, $response['TOKEN'], $response['PAYERID']);
if($get_express_checkout_details_response['ACK'] == 'Success'){
$plan_controller_obj->markUserAsPaidUser($response, $plan_details, $payment_plan_type); /add new taransaction detial for user/ Session::put('message','You have successfully subscribe to'.$plan_details->name); return redirect('dashboard');
} } /This function use for transcation from paypal/ public function doExpressCheckoutPayment($data, $response_exp, $token, $PayerID){ $response = $this->express_checkout_provider->doExpressCheckoutPayment($data, $token, $PayerID);
$createBill = $this->express_checkout_provider->createBillingAgreement($response['TOKEN']);
// The $token is the value returned from SetExpressCheckout API call $data=$this->setDataPaypal(); $profile_desc=Session::get('decs');
$profile_desc = !empty($data['subscription_desc']) ? $data['subscription_desc'] : $data['invoicedescription']; /$profile_desc = !empty($data['subscription_desc']) ? $data['subscription_desc'] : $data['invoicedescription'];/
$startdate= Carbon::now()->toAtomString();//date("Y-m-d",time()); $data = [ 'PROFILESTARTDATE' => $startdate, 'DESC' => $profile_desc, 'BILLINGPERIOD' => 'Month', // Can be 'Day', 'Week', 'SemiMonth', 'Month', 'Year' 'BILLINGFREQUENCY' => 12, // set 12 for monthly, 52 for yearly 'AMT' => 9, // Billing amount for each billing cycle 'CURRENCYCODE' => 'USD' // Currency code //'TRIALBILLINGPERIOD' => 'Day', // (Optional) Can be 'Day', 'Week', 'SemiMonth', 'Month', 'Year' //'TRIALBILLINGFREQUENCY' => 10, // (Optional) set 12 for monthly, 52 for yearly //'TRIALTOTALBILLINGCYCLES' => 5, // (Optional) Change it accordingly //'TRIALAMT' => 9, // (Optional) Change it accordingly ]; $recurring = $this->express_checkout_provider->createRecurringPaymentsProfile($data, $token); print_r($recurring);die; return $recurring; } /set paypal setExpress checkout detail/ public function setDataPaypal(){ $token = Session::get('token'); $payment_plan_id = Session::get('paypal_plan_id'); /get the user plan details/ $plan_obj = new Plans; $plan_details = $plan_obj->getPlanDetails($payment_plan_id); $data = []; if(!empty($plan_details->plan_name)){ $data['items'] = [ [ 'name' => $plan_details->plan_name, 'price' => $plan_details->plan_amount ] ]; $data['subscription_desc'] = "test123"; //$data['DESC'] = urlencode('test form'); $data['invoice_id'] = 'abc1xyz12'; //rand(); $data['invoice_description'] = "Order$data[invoice_id]Invoice";
}else{ return false; }
}
////// /* public function setRecurrinProfilePaypal(){ // The $token is the value returned from SetExpressCheckout API call $token = Session::get('token'); $data = $this->setDataPaypal();
}
------------- response is---------------------- Array ( [TIMESTAMP] => 2016-08-17T07:31:11Z [CORRELATIONID] => 947e6c20944d5 [ACK] => Failure [VERSION] => 123 [BUILD] => 000000 [L_ERRORCODE0] => 11581 [L_SHORTMESSAGE0] => Invalid Data [L_LONGMESSAGE0] => Profile description is invalid [L_SEVERITYCODE0] => Error )