注入到form中
function paymentCreate($uid, $amount, $finish_callback, \PaymentMethod $payment_method = NULL) {
$payment_method = $payment_method ? $payment_method : new \PaymentMethodUnavailable;
if($payment_method instanceof \PaymentMethodUnavailable) {
$payment_method_id = 4;
$entities = entity_load('payment_method', array($payment_method_id));
$payment_method = reset($entities);
}
$payment = new \Payment(array(
'currency_code' => 'HKD',
'description' => 'This is the payment description',
'finish_callback' => $finish_callback,
'method' => $payment_method,
'uid' => $uid,
));
$payment->setLineItem(new \PaymentLineItem(array(
'name' => 'Donation',
'amount' => $amount,
//'tax_rate' => 0.1,
)));
return $payment;
}
function xxx_form($form, &$form_state) {
$payment = paymentCreate(1, $total, 'xxx_finish_callback');
$form = payment_form_standalone($form, $form_state, $payment);
$form['#submit'][] = 'xxx_form_submit';
}
function xxx_form_submit($form, &$form_state) {
// set payment
if (isset($form_state['values']['payment_status'])) {
/** @var Payment $payment */
$payment = $form_state['payment'];
dpm($payment->pid); // payment item id
}
}
function xxx_finish_callback(Payment $payment) {
if(payment_status_is_or_has_ancestor($payment->getStatus()->status, PAYMENT_STATUS_SUCCESS)) {
drupal_set_message(t('Thank you'));
drupal_goto('<front>');
} else {
drupal_set_message(t('Your payment failed.'));
drupal_goto('<front>');
}
}
直接支付
$payment = paymentCreate(1, $total, 'xxx_finish_callback');
entity_save('payment', $payment);
dpm($payment->pid);
$payment->execute(); // payment redirect
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。