Service Call: ManageRecurringPaymentsProfileStatus
public ManageRecurringPaymentsProfileStatus ( |
||
$manageRecurringPaymentsProfileStatusReq | ||
$apiCredential | mixed | - Optional API credential - can either be a username configured in sdk_config.ini or a ICredential object created dynamically |
return | PayPal\PayPalAPI\ManageRecurringPaymentsProfileStatusResponseType |
/** * Clear a payment profile * * @param array $subscription * @param array $paymentProfile * @throws Exception */ public function cancelPaymentProfile(array $subscription, array $paymentProfile) { $subService = SubscriptionsService::instance(); $orderService = OrdersService::instance(); // PPService $paypalService = new PayPalAPIInterfaceServiceService(); $getRPPDetailsReqest = new GetRecurringPaymentsProfileDetailsRequestType(); $getRPPDetailsReqest->ProfileID = $paymentProfile['paymentProfileId']; $getRPPDetailsReq = new GetRecurringPaymentsProfileDetailsReq(); $getRPPDetailsReq->GetRecurringPaymentsProfileDetailsRequest = $getRPPDetailsReqest; $getRPPDetailsResponse = $paypalService->GetRecurringPaymentsProfileDetails($getRPPDetailsReq); if (empty($getRPPDetailsResponse) || $getRPPDetailsResponse->Ack != 'Success') { throw new Exception('Error retrieving payment profile status'); } $profileStatus = $getRPPDetailsResponse->GetRecurringPaymentsProfileDetailsResponseDetails->ProfileStatus; // Active profile, send off the cancel if (strcasecmp($profileStatus, PaymentProfileStatus::ACTIVEPROFILE) === 0 || strcasecmp($profileStatus, PaymentProfileStatus::CANCELLEDPROFILE) === 0) { if (strcasecmp($profileStatus, PaymentProfileStatus::ACTIVEPROFILE) === 0) { // Do we have a payment profile, we need to cancel it with paypal $manageRPPStatusReqestDetails = new ManageRecurringPaymentsProfileStatusRequestDetailsType(); $manageRPPStatusReqestDetails->Action = 'Cancel'; $manageRPPStatusReqestDetails->ProfileID = $paymentProfile['paymentProfileId']; $manageRPPStatusReqest = new ManageRecurringPaymentsProfileStatusRequestType(); $manageRPPStatusReqest->ManageRecurringPaymentsProfileStatusRequestDetails = $manageRPPStatusReqestDetails; $manageRPPStatusReq = new ManageRecurringPaymentsProfileStatusReq(); $manageRPPStatusReq->ManageRecurringPaymentsProfileStatusRequest = $manageRPPStatusReqest; $manageRPPStatusResponse = $paypalService->ManageRecurringPaymentsProfileStatus($manageRPPStatusReq); if (!isset($manageRPPStatusResponse) || $manageRPPStatusResponse->Ack != 'Success') { throw new Exception($manageRPPStatusResponse->Errors[0]->LongMessage); } } $orderService->updatePaymentProfileState($paymentProfile['profileId'], PaymentProfileStatus::CANCELLEDPROFILE); } }
/** * Refund the transaction */ static function refund($id, $force_refund = FALSE) { $transaction = Transaction::find($id); // Get Plan data $plan = Plan::where('id', '=', $transaction->plan_id)->first(); // Get purchase data $purchase = Purchase::where('id', '=', $transaction->purchase_id)->first(); if ($transaction->purchase->pay_method == 1) { // Add Stripe library require_once app_path() . "/libraries/stripe-php-1.9.0/lib/Stripe.php"; // Add Stripe library Stripe::setApiKey(Config::get('project.stripe_secret_key')); try { $ch = Stripe_Charge::retrieve($transaction->pay_id); $ch->refund(); } catch (Exception $e) { $error = TRUE; } // If Split pay then cancel subscription as well if ($plan->has_split_pay) { $at_period_end = FALSE; $customer = $purchase->stripe_token; $subscription_id = NULL; try { $cu = Stripe_Customer::retrieve($customer); $subscriptions = $cu->subscriptions->all(array('count' => 100)); foreach ($subscriptions->data as $subscription) { if ($subscription->plan->id == $plan_id) { if ($subscription->status == 'active') { $subscription_id = $subscription->id; break; } } } $cu->subscriptions->retrieve($subscription_id)->cancel(array('at_period_end' => $at_period_end)); } catch (Exception $e) { $error = TRUE; } } } elseif ($transaction->purchase->pay_method == 2) { $config = array('mode' => Config::get('project.paypal_mode'), 'acct1.UserName' => Config::get('project.paypal_api_username'), 'acct1.Password' => Config::get('project.paypal_api_password'), 'acct1.Signature' => Config::get('project.paypal_api_signature')); /* * The RefundTransaction API operation issues a refund to the PayPal account holder associated with a transaction. This sample code uses Merchant PHP SDK to make API call */ $refundReqest = new PayPal\PayPalAPI\RefundTransactionRequestType(); /* * Type of refund you are making. It is one of the following values: * `Full` - Full refund (default). * `Partial` - Partial refund. * `ExternalDispute` - External dispute. (Value available since version 82.0) * `Other` - Other type of refund. (Value available since version 82.0) */ $refundReqest->RefundType = 'Full'; /* * Either the `transaction ID` or the `payer ID` must be specified. PayerID is unique encrypted merchant identification number For setting `payerId`, `refundTransactionRequest.setPayerID("A9BVYX8XCR9ZQ");` Unique identifier of the transaction to be refunded. */ $refundReqest->TransactionID = $transaction->pay_id; /* * (Optional)Type of PayPal funding source (balance or eCheck) that can be used for auto refund. It is one of the following values: any – The merchant does not have a preference. Use any available funding source. default – Use the merchant's preferred funding source, as configured in the merchant's profile. instant – Use the merchant's balance as the funding source. eCheck – The merchant prefers using the eCheck funding source. If the merchant's PayPal balance can cover the refund amount, use the PayPal balance. */ //$refundReqest->RefundSource = $_REQUEST['refundSource']; $refundReqest->Memo = "Refunded from Digital Kickstart App"; /* * (Optional) Maximum time until you must retry the refund. */ //$refundReqest->RetryUntil = $_REQUEST['retryUntil']; $refundReq = new PayPal\PayPalAPI\RefundTransactionReq(); $refundReq->RefundTransactionRequest = $refundReqest; /* * ## Creating service wrapper object Creating service wrapper object to make API call and loading Configuration::getAcctAndConfig() returns array that contains credential and config parameters */ $paypalService = new PayPal\Service\PayPalAPIInterfaceServiceService($config); try { /* wrap API method calls on the service object with a try catch */ $refundResponse = $paypalService->RefundTransaction($refundReq); } catch (Exception $ex) { $error = TRUE; } // If Split pay then cancel subscription as well if ($plan->has_split_pay) { $paypal_sub_id = $purchase->paypal_sub_id; /* * The ManageRecurringPaymentsProfileStatus API operation cancels, suspends, or reactivates a recurring payments profile. */ $manageRPPStatusReqestDetails = new ManageRecurringPaymentsProfileStatusRequestDetailsType(); /* * (Required) The action to be performed to the recurring payments profile. Must be one of the following: Cancel – Only profiles in Active or Suspended state can be canceled. Suspend – Only profiles in Active state can be suspended. Reactivate – Only profiles in a suspended state can be reactivated. */ $manageRPPStatusReqestDetails->Action = 'Cancel'; /* * (Required) Recurring payments profile ID returned in the CreateRecurringPaymentsProfile response. */ $manageRPPStatusReqestDetails->ProfileID = $paypal_sub_id; $manageRPPStatusReqest = new ManageRecurringPaymentsProfileStatusRequestType(); $manageRPPStatusReqest->ManageRecurringPaymentsProfileStatusRequestDetails = $manageRPPStatusReqestDetails; $manageRPPStatusReq = new ManageRecurringPaymentsProfileStatusReq(); $manageRPPStatusReq->ManageRecurringPaymentsProfileStatusRequest = $manageRPPStatusReqest; /* * ## Creating service wrapper object Creating service wrapper object to make API call and loading Configuration::getAcctAndConfig() returns array that contains credential and config parameters */ $paypalService = new PayPalAPIInterfaceServiceService($config); try { /* wrap API method calls on the service object with a try catch */ $manageRPPStatusResponse = $paypalService->ManageRecurringPaymentsProfileStatus($manageRPPStatusReq); } catch (Exception $ex) { $error = TRUE; } if (isset($manageRPPStatusResponse) and $manageRPPStatusResponse->Ack == 'Success') { // Do nothing } else { $error = TRUE; } } } if (empty($error) or $force_refund) { self::completeRefund($transaction); return TRUE; } }
/** * Cancel Subscription process */ private function _cancelSubscription($pay_method, $params) { if ($pay_method == 'Stripe') { $at_period_end = FALSE; $customer = $params['stripe_customer_id']; $plan_id = $params['plan_id']; $subscription_id = NULL; // Add Stripe library require_once app_path() . "/libraries/stripe-php-1.9.0/lib/Stripe.php"; // Add Stripe library Stripe::setApiKey(Config::get('project.stripe_secret_key')); try { $cu = Stripe_Customer::retrieve($customer); $subscriptions = $cu->subscriptions->all(array('count' => 100)); foreach ($subscriptions->data as $subscription) { if ($subscription->plan->id == $plan_id) { if ($subscription->status == 'active') { $subscription_id = $subscription->id; break; } } } $cu->subscriptions->retrieve($subscription_id)->cancel(array('at_period_end' => $at_period_end)); Log::info('Stripe Split Sub Cancelled', $params); } catch (Exception $e) { Log::info('Stripe cancel error', array('msg' => $e->getMessage())); header('HTTP/1.1 400 Bad Request', true, 400); echo "Unsuccessful event"; return; } } if ($pay_method == 'PayPal') { $config = array('mode' => Config::get('project.paypal_mode'), 'acct1.UserName' => Config::get('project.paypal_api_username'), 'acct1.Password' => Config::get('project.paypal_api_password'), 'acct1.Signature' => Config::get('project.paypal_api_signature')); $purchase = $params['purchase']; //$transaction->purchase; $paypal_sub_id = $purchase->paypal_sub_id; /* * The ManageRecurringPaymentsProfileStatus API operation cancels, suspends, or reactivates a recurring payments profile. */ $manageRPPStatusReqestDetails = new ManageRecurringPaymentsProfileStatusRequestDetailsType(); /* * (Required) The action to be performed to the recurring payments profile. Must be one of the following: Cancel – Only profiles in Active or Suspended state can be canceled. Suspend – Only profiles in Active state can be suspended. Reactivate – Only profiles in a suspended state can be reactivated. */ $manageRPPStatusReqestDetails->Action = 'Cancel'; /* * (Required) Recurring payments profile ID returned in the CreateRecurringPaymentsProfile response. */ $manageRPPStatusReqestDetails->ProfileID = $paypal_sub_id; $manageRPPStatusReqest = new ManageRecurringPaymentsProfileStatusRequestType(); $manageRPPStatusReqest->ManageRecurringPaymentsProfileStatusRequestDetails = $manageRPPStatusReqestDetails; $manageRPPStatusReq = new ManageRecurringPaymentsProfileStatusReq(); $manageRPPStatusReq->ManageRecurringPaymentsProfileStatusRequest = $manageRPPStatusReqest; /* * ## Creating service wrapper object Creating service wrapper object to make API call and loading Configuration::getAcctAndConfig() returns array that contains credential and config parameters */ $paypalService = new PayPalAPIInterfaceServiceService($config); try { /* wrap API method calls on the service object with a try catch */ $manageRPPStatusResponse = $paypalService->ManageRecurringPaymentsProfileStatus($manageRPPStatusReq); } catch (Exception $ex) { header('HTTP/1.1 400 Bad Request', true, 400); echo "Unsuccessful event"; return; } if (isset($manageRPPStatusResponse) and $manageRPPStatusResponse->Ack == 'Success') { //Session::flash('alert_message', '<strong>Done!</strong> You successfully have cancelled the subscription.'); //return Redirect::to("admin/transactions/detail/$transaction->id"); } else { header('HTTP/1.1 400 Bad Request', true, 400); echo "Unsuccessful event"; return; } } }
/** * Cancel Subscription process */ public function postCancelSubscription($id, $at_period_end = FALSE) { $transaction = Transaction::find($id); if (Input::get('pay_method') == 'Stripe') { $at_period_end = !$at_period_end ? FALSE : TRUE; $customer = Input::get('stripe_customer_id'); $subscription_id = Input::get('subscription_id'); // Add Stripe library require_once app_path() . "/libraries/stripe-php-1.9.0/lib/Stripe.php"; // Add Stripe library Stripe::setApiKey(Config::get('project.stripe_secret_key')); try { $cu = Stripe_Customer::retrieve($customer); $cu->subscriptions->retrieve($subscription_id)->cancel(array('at_period_end' => $at_period_end)); Session::flash('alert_message', '<strong>Done!</strong> You successfully have cancelled the subscription.'); return Redirect::to("admin/transactions/detail/{$transaction->id}"); } catch (Exception $e) { Session::flash('alert_error', '<strong>Ooops!</strong> Subscription was not cancelled, try again.'); return Redirect::to("admin/transactions/cancel-subscription/{$transaction->id}/{$at_period_end}"); } } if (Input::get('pay_method') == 'PayPal') { $config = array('mode' => Config::get('project.paypal_mode'), 'acct1.UserName' => Config::get('project.paypal_api_username'), 'acct1.Password' => Config::get('project.paypal_api_password'), 'acct1.Signature' => Config::get('project.paypal_api_signature')); $purchase = $transaction->purchase; $paypal_sub_id = $purchase->paypal_sub_id; /* * The ManageRecurringPaymentsProfileStatus API operation cancels, suspends, or reactivates a recurring payments profile. */ $manageRPPStatusReqestDetails = new ManageRecurringPaymentsProfileStatusRequestDetailsType(); /* * (Required) The action to be performed to the recurring payments profile. Must be one of the following: Cancel – Only profiles in Active or Suspended state can be canceled. Suspend – Only profiles in Active state can be suspended. Reactivate – Only profiles in a suspended state can be reactivated. */ $manageRPPStatusReqestDetails->Action = 'Cancel'; /* * (Required) Recurring payments profile ID returned in the CreateRecurringPaymentsProfile response. */ $manageRPPStatusReqestDetails->ProfileID = $paypal_sub_id; $manageRPPStatusReqest = new ManageRecurringPaymentsProfileStatusRequestType(); $manageRPPStatusReqest->ManageRecurringPaymentsProfileStatusRequestDetails = $manageRPPStatusReqestDetails; $manageRPPStatusReq = new ManageRecurringPaymentsProfileStatusReq(); $manageRPPStatusReq->ManageRecurringPaymentsProfileStatusRequest = $manageRPPStatusReqest; /* * ## Creating service wrapper object Creating service wrapper object to make API call and loading Configuration::getAcctAndConfig() returns array that contains credential and config parameters */ $paypalService = new PayPalAPIInterfaceServiceService($config); try { /* wrap API method calls on the service object with a try catch */ $manageRPPStatusResponse = $paypalService->ManageRecurringPaymentsProfileStatus($manageRPPStatusReq); } catch (Exception $ex) { Session::flash('alert_error', '<strong>Ooops!</strong> Subscription was not cancelled, try again.'); return Redirect::to("admin/transactions/cancel-subscription/{$transaction->id}/{$at_period_end}"); } if (isset($manageRPPStatusResponse) and $manageRPPStatusResponse->Ack == 'Success') { Session::flash('alert_message', '<strong>Done!</strong> You successfully have cancelled the subscription.'); return Redirect::to("admin/transactions/detail/{$transaction->id}"); } else { Session::flash('alert_error', '<strong>Ooops!</strong> Subscription was not cancelled, try again.'); return Redirect::to("admin/transactions/cancel-subscription/{$transaction->id}/{$at_period_end}"); } } }
/* * (Required) Recurring payments profile ID returned in the CreateRecurringPaymentsProfile response. */ $manageRPPStatusReqestDetails->ProfileID = $_REQUEST['profileID']; $manageRPPStatusReqest = new ManageRecurringPaymentsProfileStatusRequestType(); $manageRPPStatusReqest->ManageRecurringPaymentsProfileStatusRequestDetails = $manageRPPStatusReqestDetails; $manageRPPStatusReq = new ManageRecurringPaymentsProfileStatusReq(); $manageRPPStatusReq->ManageRecurringPaymentsProfileStatusRequest = $manageRPPStatusReqest; /* * ## Creating service wrapper object Creating service wrapper object to make API call and loading Configuration::getAcctAndConfig() returns array that contains credential and config parameters */ $paypalService = new PayPalAPIInterfaceServiceService(Configuration::getAcctAndConfig()); try { /* wrap API method calls on the service object with a try catch */ $manageRPPStatusResponse = $paypalService->ManageRecurringPaymentsProfileStatus($manageRPPStatusReq); } catch (Exception $ex) { include_once "../Error.php"; exit; } if (isset($manageRPPStatusResponse)) { echo "<table>"; echo "<tr><td>Ack :</td><td><div id='Ack'>{$manageRPPStatusResponse->Ack}</div> </td></tr>"; echo "<tr><td>ProfileID :</td><td><div id='ProfileID'>" . $manageRPPStatusResponse->ManageRecurringPaymentsProfileStatusResponseDetails->ProfileID . "</div> </td></tr>"; echo "</table>"; echo "<pre>"; print_r($manageRPPStatusResponse); echo "</pre>"; } require_once '../Response.php';