function commerce_paypal_ec_refund_form_submit in Commerce PayPal 7.2
Submit handler: process a refund request.
File
- modules/
ec/ includes/ commerce_paypal_ec.admin.inc, line 284 - Administrative forms for the Paypal EC module.
Code
function commerce_paypal_ec_refund_form_submit($form, &$form_state) {
$transaction = $form_state['transaction'];
$payment_method = $form_state['payment_method'];
$order = $form_state['order'];
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$order_total = $order_wrapper->commerce_order_total
->value();
$amount = commerce_currency_decimal_to_amount($form_state['values']['amount'], $order_total['currency_code']);
// Build a name-value pair array for this transaction.
$nvp = array(
'METHOD' => 'RefundTransaction',
'TRANSACTIONID' => $transaction->remote_id,
'REFUNDTYPE' => $order_total['amount'] != $amount ? 'Partial' : 'Full',
'AMT' => commerce_paypal_price_amount($amount, $order_total['currency_code']),
'CURRENCYCODE' => $order_total['currency_code'],
);
// Submit the refund request to Paypal.
$response = commerce_paypal_api_request($form_state['payment_method'], $nvp, $order);
// If the credit succeeded...
if ($response['ACK'] == 'Success') {
drupal_set_message(t('Refund for @amount issued successfully.', array(
'@amount' => commerce_currency_format($amount, $transaction->currency_code),
)));
// Create a new transaction to record the credit.
$credit_transaction = commerce_payment_transaction_new($payment_method['method_id'], $order->order_id);
$credit_transaction->instance_id = $payment_method['instance_id'];
$credit_transaction->remote_id = $response['REFUNDTRANSACTIONID'];
$credit_transaction->amount = $amount * -1;
$credit_transaction->currency_code = $transaction->currency_code;
$credit_transaction->payload[REQUEST_TIME] = $response;
$credit_transaction->status = COMMERCE_PAYMENT_STATUS_SUCCESS;
$credit_transaction->remote_status = COMMERCE_CREDIT_CREDIT;
$credit_transaction->message = t('Refunded to @remote_id.', array(
'@remote_id' => $transaction->remote_id,
));
// Save the credit transaction.
commerce_payment_transaction_save($credit_transaction);
}
else {
// Display a failure message and response reason from Payflow.
drupal_set_message(t('Refund failed: @reason', array(
'@reason' => $response['L_LONGMESSAGE0'],
)), 'error');
// Save the failure response message to the original transaction.
$transaction->payload[REQUEST_TIME . '-refund'] = $response;
commerce_payment_transaction_save($transaction);
}
$form_state['redirect'] = 'admin/commerce/orders/' . $order->order_id . '/payment';
}