Service Call: RefundTransaction
public RefundTransaction ( |
||
$refundTransactionReq | ||
$apiCredential | mixed | - Optional API credential - can either be a username configured in sdk_config.ini or a ICredential object created dynamically |
Résultat |
$refundReqest->Memo = $_REQUEST['memo']; /* * (Optional) Maximum time until you must retry the refund. */ $refundReqest->RetryUntil = $_REQUEST['retryUntil']; $refundReq = new 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 PayPalAPIInterfaceServiceService(Configuration::getAcctAndConfig()); try { /* wrap API method calls on the service object with a try catch */ $refundResponse = $paypalService->RefundTransaction($refundReq); } catch (Exception $ex) { include_once "../Error.php"; exit; } if (isset($refundResponse)) { echo "<table>"; echo "<tr><td>Ack :</td><td><div id='Ack'>{$refundResponse->Ack}</div> </td></tr>"; //echo "<tr><td>RefundStatus :</td><td><div id='RefundStatus'>$refundResponse->RefundInfo->RefundStatus</div> </td></tr>"; echo "</table>"; echo "<pre>"; print_r($refundResponse); echo "</pre>"; } require_once '../Response.php';
/** * 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; } }