mirror of
https://github.com/Bigherollc/wticreatorstudio.git
synced 2026-07-31 05:16:49 -04:00
418 lines
14 KiB
PHP
418 lines
14 KiB
PHP
<?php
|
|
|
|
namespace Modules\Subscription\Services;
|
|
|
|
use Carbon\Carbon;
|
|
use Cache;
|
|
|
|
class PaypalPaymentService
|
|
{
|
|
protected $apiBaseUrl;
|
|
protected $accessToken;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->apiBaseUrl = 'https://api-m.paypal.com'; // live
|
|
//$this->apiBaseUrl = 'https://api-m.sandbox.paypal.com'; // sandbox
|
|
}
|
|
|
|
private function makeAccessToken() {
|
|
|
|
$credential = getPaymentInfoViaSellerId(1, 3);
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $this->apiBaseUrl.'/v1/oauth2/token');
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
|
|
curl_setopt($ch, CURLOPT_USERPWD, $credential->perameter_2 . ':' . $credential->perameter_3);
|
|
|
|
$headers = array();
|
|
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
$result = curl_exec($ch);
|
|
curl_close($ch);
|
|
|
|
$result = json_decode($result);
|
|
if (!empty($result->access_token)) {
|
|
$this->accessToken = $result->access_token;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
public function createSubscription($post_data)
|
|
{
|
|
if(!$this->makeAccessToken()){
|
|
return false;
|
|
}
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $this->apiBaseUrl.'/v1/billing/subscriptions');
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
|
|
|
|
$headers = array();
|
|
$headers[] = 'Content-Type: application/json';
|
|
$headers[] = 'Authorization: Bearer '.$this->accessToken;
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
$result = json_decode(curl_exec($ch));
|
|
if (curl_errno($ch)) {
|
|
$response->msg = curl_error($ch);
|
|
curl_close($ch);
|
|
return $response;
|
|
}
|
|
curl_close($ch);
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function updateSubscription($subscription_id, $trial_days)
|
|
{
|
|
if(!$this->makeAccessToken()){
|
|
return false;
|
|
}
|
|
|
|
|
|
$post_data = json_encode([
|
|
[
|
|
'op' => 'replace',
|
|
'path' => '/start_time',
|
|
'value' => Carbon::now()->addDays($trial_days)->toDateTimeString()
|
|
]
|
|
]);
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $this->apiBaseUrl.'/v1/billing/subscriptions/'.$subscription_id);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
|
|
|
|
$headers = array();
|
|
$headers[] = 'Content-Type: application/json';
|
|
$headers[] = 'Authorization: Bearer '.$this->accessToken;
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
$result = json_decode(curl_exec($ch));
|
|
if (curl_errno($ch)) {
|
|
$response->msg = curl_error($ch);
|
|
curl_close($ch);
|
|
return $response;
|
|
}
|
|
curl_close($ch);
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function showSubscription($subscription_id)
|
|
{
|
|
if(!$this->makeAccessToken()){
|
|
return false;
|
|
}
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $this->apiBaseUrl.'/v1/billing/subscriptions/'.$subscription_id);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
$headers = array();
|
|
$headers[] = 'Content-Type: application/json';
|
|
$headers[] = 'Authorization: Bearer '.$this->accessToken;
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
$result = json_decode(curl_exec($ch));
|
|
if (curl_errno($ch)) {
|
|
$response->msg = curl_error($ch);
|
|
curl_close($ch);
|
|
return $response;
|
|
}
|
|
curl_close($ch);
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function activateSubscription($subscription_id)
|
|
{
|
|
if(!$this->makeAccessToken()){
|
|
return false;
|
|
}
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $this->apiBaseUrl.'/v1/billing/subscriptions/'.$subscription_id.'/activate');
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['reason' => 'Approve Subscription']));
|
|
|
|
$headers = array();
|
|
$headers[] = 'Content-Type: application/json';
|
|
$headers[] = 'Authorization: Bearer '.$this->accessToken;
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
$result = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
if ((int) $httpCode === 204) {
|
|
return true;
|
|
}
|
|
return json_decode($result);
|
|
}
|
|
|
|
public function cancelSubscription($subscription_id)
|
|
{
|
|
if(!$this->makeAccessToken()){
|
|
return false;
|
|
}
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $this->apiBaseUrl.'/v1/billing/subscriptions/'.$subscription_id.'/cancel');
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['reason' => 'Upgrade or Downgrade Subscription']));
|
|
|
|
$headers = array();
|
|
$headers[] = 'Content-Type: application/json';
|
|
$headers[] = 'Authorization: Bearer '.$this->accessToken;
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
$result = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
if ((int) $httpCode === 204) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function showPayment($payment_id)
|
|
{
|
|
if(!$this->makeAccessToken()){
|
|
return false;
|
|
}
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $this->apiBaseUrl.'/v2/payments/captures/'.$payment_id);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
$headers = array();
|
|
$headers[] = 'Content-Type: application/json';
|
|
$headers[] = 'Authorization: Bearer '.$this->accessToken;
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
$result = json_decode(curl_exec($ch));
|
|
if (curl_errno($ch)) {
|
|
$response->msg = curl_error($ch);
|
|
curl_close($ch);
|
|
return $response;
|
|
}
|
|
curl_close($ch);
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function createProduct($title,$type)
|
|
{
|
|
if(!$this->makeAccessToken()){
|
|
return false;
|
|
}
|
|
|
|
$post_data = json_encode([
|
|
'name' => $title,
|
|
'type' => $type
|
|
]);
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $this->apiBaseUrl.'/v1/catalogs/products');
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
|
|
|
|
$headers = array();
|
|
$headers[] = 'Content-Type: application/json';
|
|
$headers[] = 'Authorization: Bearer '.$this->accessToken;
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
$result = json_decode(curl_exec($ch));
|
|
if (curl_errno($ch)) {
|
|
$response->msg = curl_error($ch);
|
|
curl_close($ch);
|
|
return $response;
|
|
}
|
|
curl_close($ch);
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function createProductPrice($product, $data)
|
|
{
|
|
if(!$this->makeAccessToken()){
|
|
return false;
|
|
}
|
|
|
|
$custom_intervals = [
|
|
'Months' => 'Month',
|
|
'Years' => 'Year'
|
|
];
|
|
|
|
$post_data = json_encode([
|
|
'product_id' => $product->paypal_product_id,
|
|
'name' => $product->title.' - '.$data['custom_interval'].' '.$data['custom_interval_type']. ( $data['payment'] == 'monthly' ? ' ($'.number_format($data['price'] / $data['custom_interval'],2).' per month)' : '($'.number_format($data['price'],2).')'),
|
|
'status' => 'ACTIVE',
|
|
'billing_cycles' => [
|
|
[
|
|
'frequency' => [
|
|
'interval_unit' => $data['payment'] == 'monthly' ? 'MONTH' : 'YEAR',
|
|
'interval_count' => 1
|
|
],
|
|
'tenure_type' => 'REGULAR',
|
|
'sequence' => 1,
|
|
'total_cycles' => ($data['payment'] == 'upfront' ? 0 : $data['custom_interval']),
|
|
'pricing_scheme' => [
|
|
'fixed_price' => [
|
|
'currency_code' => getCurrencyCode(),
|
|
'value' => $data['payment'] == 'monthly' ? number_format($data['price'] / $data['custom_interval'], 2, '.', '') : number_format($data['price'], 2, '.', '')
|
|
]
|
|
],
|
|
]
|
|
],
|
|
'payment_preferences' => [
|
|
'setup_fee_failure_action' => 'CONTINUE'
|
|
]
|
|
]);
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $this->apiBaseUrl.'/v1/billing/plans');
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
|
|
|
|
$headers = array();
|
|
$headers[] = 'Content-Type: application/json';
|
|
$headers[] = 'Authorization: Bearer '.$this->accessToken;
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
$result = json_decode(curl_exec($ch));
|
|
if (curl_errno($ch)) {
|
|
$response->msg = curl_error($ch);
|
|
curl_close($ch);
|
|
return $response;
|
|
}
|
|
curl_close($ch);
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function createProductPriceWithDiscount($planPrice, $duration, $discounted_price_monthly, $discounted_price_all, $payment_details)
|
|
{
|
|
if(!$this->makeAccessToken()){
|
|
return false;
|
|
}
|
|
|
|
$custom_intervals = [
|
|
'Months' => 'Month',
|
|
'Years' => 'Year'
|
|
];
|
|
|
|
$post_data = [
|
|
'product_id' => $planPrice->plan->paypal_product_id,
|
|
'name' => $payment_details,
|
|
'status' => 'ACTIVE',
|
|
'payment_preferences' => [
|
|
'setup_fee_failure_action' => 'CONTINUE'
|
|
]
|
|
];
|
|
|
|
if($duration == 'Once' && $planPrice->payment == 'monthly'){
|
|
|
|
$post_data['billing_cycles'] = [
|
|
[
|
|
'frequency' => [
|
|
'interval_unit' => $custom_intervals[$planPrice->custom_interval_type],
|
|
'interval_count' => ($planPrice->payment != 'upfront' ? 1 : $planPrice->custom_interval)
|
|
],
|
|
'tenure_type' => 'TRIAL',
|
|
'sequence' => 1,
|
|
'total_cycles' => 1,
|
|
'pricing_scheme' => [
|
|
'fixed_price' => [
|
|
'currency_code' => getCurrencyCode(),
|
|
'value' => (float)($planPrice->payment == 'monthly' ? $discounted_price_monthly : $discounted_price_all)
|
|
]
|
|
],
|
|
],
|
|
[
|
|
'frequency' => [
|
|
'interval_unit' => $custom_intervals[$planPrice->custom_interval_type],
|
|
'interval_count' => ($planPrice->payment != 'upfront' ? 1 : $planPrice->custom_interval)
|
|
],
|
|
'tenure_type' => 'REGULAR',
|
|
'sequence' => 2,
|
|
'total_cycles' => ($planPrice->custom_interval - 1),
|
|
'pricing_scheme' => [
|
|
'fixed_price' => [
|
|
'currency_code' => getCurrencyCode(),
|
|
'value' => (float)($planPrice->price / $planPrice->custom_interval)
|
|
]
|
|
],
|
|
]
|
|
];
|
|
|
|
} else {
|
|
|
|
$post_data['billing_cycles'] = [
|
|
[
|
|
'frequency' => [
|
|
'interval_unit' => $custom_intervals[$planPrice->custom_interval_type],
|
|
'interval_count' => ($planPrice->payment != 'upfront' ? 1 : $planPrice->custom_interval)
|
|
],
|
|
'tenure_type' => 'REGULAR',
|
|
'sequence' => 1,
|
|
'total_cycles' => ($planPrice->payment == 'upfront' ? 1 : $planPrice->custom_interval),
|
|
'pricing_scheme' => [
|
|
'fixed_price' => [
|
|
'currency_code' => getCurrencyCode(),
|
|
'value' => ($planPrice->payment == 'monthly' ? $discounted_price_monthly : $discounted_price_all)
|
|
]
|
|
],
|
|
]
|
|
|
|
];
|
|
}
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $this->apiBaseUrl.'/v1/billing/plans');
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data,true));
|
|
|
|
$headers = array();
|
|
$headers[] = 'Content-Type: application/json';
|
|
$headers[] = 'Authorization: Bearer '.$this->accessToken;
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
$result = json_decode(curl_exec($ch));
|
|
if (curl_errno($ch)) {
|
|
$response->msg = curl_error($ch);
|
|
curl_close($ch);
|
|
return $response;
|
|
}
|
|
curl_close($ch);
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function updateProductPrice($price,$data)
|
|
{
|
|
//lets archive first the price then create new one
|
|
//$this->archivePrice($price->stripe_price_id);
|
|
return $this->createProductPrice($price->plan,$data);
|
|
}
|
|
|
|
|
|
}
|